ERROR node-builder RUN npx tsc on attemt to setup Jest

Hello!

I’m not able to setup an empty nakama project following this testing guide

I googled a lot and tormented a ChatGPT without luck.

My setup is basically an empty starter project with a TS. Nothing fancy just created the project with official guide.

On the command docker compose up --build nakama it was up and running.

Then I decided to setup a testing environment for a more robust development and already lost about 6 hours. Not very productive I should say. Buy the way, back to the problem.

What I did is just followed a guide up to the import 'jest-ts-auto-mock'; step. And after that I tried to execute docker compose up --build nakama again and the output looks like this

➜  nakama-a git:(master) ✗ docker compose up --build nakama
[+] Building 111.5s (14/16)                                                                                                                                                                                                                                                                   
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                     0.0s
 => => transferring dockerfile: 32B                                                                                                                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                          0.0s
 => [internal] load metadata for registry.heroiclabs.com/heroiclabs/nakama:3.15.0                                                                                                                                                                                                       10.6s
 => [internal] load metadata for docker.io/library/node:alpine                                                                                                                                                                                                                           4.3s
 => [node-builder 1/8] FROM docker.io/library/node:alpine@sha256:37750e51d61bef92165b2e29a77da4277ba0777258446b7a9c99511f119db096                                                                                                                                                        0.0s
 => [internal] load build context                                                                                                                                                                                                                                                        0.2s
 => => transferring context: 212.43kB                                                                                                                                                                                                                                                    0.2s
 => CACHED [stage-1 1/3] FROM registry.heroiclabs.com/heroiclabs/nakama:3.15.0@sha256:8015dc03045829e4ec28d9f73895b774a33dfba07373bc447ed559e6903fee28                                                                                                                                   0.0s
 => CACHED [node-builder 2/8] WORKDIR /backend                                                                                                                                                                                                                                           0.0s
 => CACHED [node-builder 3/8] RUN apk add --no-cache git                                                                                                                                                                                                                                 0.0s
 => [node-builder 4/8] COPY package*.json .                                                                                                                                                                                                                                              0.0s
 => [node-builder 5/8] RUN npm install                                                                                                                                                                                                                                                  98.3s
 => [node-builder 6/8] COPY tsconfig.json .                                                                                                                                                                                                                                              0.0s
 => [node-builder 7/8] COPY src ./src                                                                                                                                                                                                                                                    0.0s
 => ERROR [node-builder 8/8] RUN npx tsc                                                                                                                                                                                                                                                 2.3s
------                                                                                                                                                                                                                                                                                        
 > [node-builder 8/8] RUN npx tsc:                                                                                                                                                                                                                                                            
#0 2.205 error TS2688: Cannot find type definition file for '@ampproject'.                                                                                                                                                                                                                    
#0 2.205   The file is in the program because:
#0 2.205     Entry point for implicit type library '@ampproject'
#0 2.205 error TS2688: Cannot find type definition file for '@babel'.
#0 2.205   The file is in the program because:
#0 2.205     Entry point for implicit type library '@babel'
#0 2.207   The file is in the program because:
#0 2.207     Entry point for implicit type library 'y18n'
#0 2.207 error TS2688: Cannot find type definition file for 'yallist'.
... A lot of other similar staff
#0 2.207   The file is in the program because:
#0 2.207     Entry point for implicit type library 'yargs'
#0 2.207 error TS2688: Cannot find type definition file for 'yargs-parser'.
#0 2.207   The file is in the program because:
#0 2.207     Entry point for implicit type library 'yargs-parser'
------
failed to solve: executor failed running [/bin/sh -c npx tsc]: exit code: 2
  1. Versions: Nakama {3.15}, Mac, Docker, node v20.7.0, npm 10.1.0
  2. Server Framework Runtime language: TS/JS

Dockerfile

FROM node:alpine AS node-builder

WORKDIR /backend

COPY package*.json .
RUN npm install

RUN ls
COPY tsconfig.json .
RUN mkdir src 
COPY src/*.ts ./src
RUN ls
RUN npx tsc

FROM registry.heroiclabs.com/heroiclabs/nakama:3.15.0

COPY --from=node-builder /backend/build/*.js /nakama/data/modules/build/
RUN ls /nakama/data/modules
RUN ls /nakama/data/modules/build/
COPY local.yml ./data
RUN ls
RUN ls /nakama/data/

package.json

{
  "name": "nakama-a",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "globals": {
      "ts-jest": {
        "compiler": "ttypescript"
      }
    },
    "transform": {
      ".(ts|tsx)": "ts-jest"
    },
    "setupFiles": [
      "<rootDir>jest-config.ts"
    ]
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nakama-runtime": "github:heroiclabs/nakama-common"
  },
  "devDependencies": {
    "@types/jest": "^29.5.5",
    "jest": "^29.7.0",
    "jest-ts-auto-mock": "^2.1.0",
    "ts-auto-mock": "^3.7.1",
    "ts-jest": "^29.1.1",
    "ttypescript": "^1.5.15"
  }
}

I googled and tried to apply a lot of different solutions for ERROR RUN npx tsc and error TS2688: Cannot find type definition file for without success.

Can someone help me with Jest setup, please? When I remove all Jset dependencies and setup Nakama works fine, but I want to have a proper testing environment for a quicker development iteration. Thank you in advance.

Hello @georgeneversleep, try to fix the path for jest-config.ts in your package.json:

1 Like

Hello. Isn’t <rootDir> a dirrective of package.json pointing to a root dir? Looks like I get it wrong. Will try it right now.

Thanks for the suggestion. Making this

"setupFiles": [
      "./jest-config.ts"
]

But now on npm run test I get another error

 => ERROR [node-builder 11/12] RUN npm run test                                                                                                                 0.9s
------                                                                                                                                                               
 > [node-builder 11/12] RUN npm run test:                                                                                                                            
#0 0.373                                                                                                                                                             
#0 0.373 > nakama-a@1.0.0 test                                                                                                                                       
#0 0.373 > jest                                                                                                                                                      
#0 0.373 
#0 0.704 ts-jest[ts-jest-transformer] (WARN) Define `ts-jest` config under `globals` is deprecated. Please do
#0 0.704 transform: {
#0 0.704     <transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],
#0 0.704 },
#0 0.840 ts-jest[Importer] (ERROR) failed loading module 'ttypescript' Cannot set property createProgram of #<Object> which has only a getter
#0 0.853 FAIL src/rpc-add-item.test.ts
#0 0.853   ● Test suite failed to run
#0 0.853 
#0 0.853     Loading module ttypescript failed with error: Cannot set property createProgram of #<Object> which has only a getter
#0 0.853 
#0 0.853       at exports.Importer.Importer._import (node_modules/ts-jest/dist/utils/importer.js:158:19)
#0 0.853       at exports.Importer.Importer.typescript (node_modules/ts-jest/dist/utils/importer.js:87:21)
#0 0.853       at new ConfigSet (node_modules/ts-jest/dist/legacy/config/config-set.js:209:51)
#0 0.853       at exports.TsJestTransformer.TsJestTransformer._createConfigSet (node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:174:16)
#0 0.853       at exports.TsJestTransformer.TsJestTransformer._configsFor (node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:150:34)
#0 0.853       at exports.TsJestTransformer.TsJestTransformer.getCacheKey (node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:311:28)
#0 0.853       at ScriptTransformer._getCacheKey (node_modules/@jest/transform/build/ScriptTransformer.js:228:41)
#0 0.853       at ScriptTransformer._getFileCachePath (node_modules/@jest/transform/build/ScriptTransformer.js:289:27)
#0 0.853       at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:525:32)
#0 0.853       at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:674:40)
#0 0.853 
#0 0.854 Test Suites: 1 failed, 1 total
#0 0.854 Tests:       0 total
#0 0.854 Snapshots:   0 total
#0 0.854 Time:        0.187 s
#0 0.854 Ran all test suites.
------
failed to solve: executor failed running [/bin/sh -c npm run test]: exit code: 1

I wonder if someone has a complete project template with a proper Jest setup. The official Nakama template is not featured with testing suddenly.

For a not a node js guy like me it looks impossible to understand whats’s going on with all these dances around dependencies and deprecated configurations described in the guides.

Did you add this to tsconfig.json as per the guide instructions?

In your `tsconfig.json` file add the following to your `compilerOptions` section:
{
  "compilerOptions": {
    "plugins": [
      {
        "transform": "ts-auto-mock/transformer",
        "cacheBetweenTests": false
      }
    ]
  }
}
1 Like

Yes, this is my config

{
  "compilerOptions": {
    "plugins": [
      {
        "transform": "ts-auto-mock/transformer",
        "cacheBetweenTests": false
      }
    ]
  },
  "files": ["./node_modules/nakama-runtime/index.d.ts"],
  "include": ["src"],
  "exclude": ["node_modules", "build"]
}

I’ve looked into this and had success with ts-patch instead of ttypescript:

npm uninstall ttypescript
npm i -D ts-patch

on package.json change the compiler:

 "jest": {
    "globals": {
      "ts-jest": {
        "compiler": "ts-patch/compiler"
      }
    },

I’ll list here the rest of the steps I took to make it work locally, you may not need all of these but they will be patched into the testing guide later:

  • Adjust import path: import AddItemRpc from "./rpc-add-item";
  • Use @jest/globals instead of @types/jest:

npm install --save-dev @jest/globals

  • Add needed imports to rpc-add-item.test.ts:
import { describe, expect, beforeEach, test } from '@jest/globals';
import 'jest'
  • If you have strict type-checking enabled:

On rpc-add-item.test.ts:

let mockCtx: any, mockLogger: any, mockNk: any, mockLoggerError: any, mockNkStorageRead: any, mockNkStorageWrite: any, mockStorageWriteAck: any;

On rpc-add-item.ts:

type Inventory = {
    [key: string]: any;
};

const AddItemRpc = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string | null): string {
...
let inventory: Inventory = {};

Let me know if that works for you.

1 Like

Fernando, thanks a lot. Your solutions and explanation give me a good starting point to make it up and running.

After replacing the typescript with the ts-patch and adding other changes you are listed, I faced another problem. It was impossible to use import and export declarations.

error TS6131: Cannot compile modules using option 'outFile' unless the '--module' flag is 'amd' or 'system'

To overcome it I was forced to make additional changes in the local.yml

  1. Changing
js_entrypoint: "build/index.js"

with

path: "/nakama/data/modules/build"
  1. Replacing "outFile": "./build/index.js", with "outDir": "./build" in tsconfig,json
  2. Replacing
COPY --from=node-builder /backend/build/*.js /nakama/data/modules/build/

with

COPY --from=node-builder /backend/build/ /nakama/data/modules/build/

in the Dockerfile.

Thanks a lot for the help.

1 Like