2

I'm trying to move my service into a Docker container. Building the project works fine locally, but when I try building it as part of docker build, I get the following error:

node_modules/jest-extended/types/index.d.ts(135,39): error TS2694: Namespace 'jest' has no exported member 'Mock'.

Please find my dockerfile:

FROM node:12
COPY package.json /opt/service/
WORKDIR /opt/service/
RUN npm install
COPY . .
RUN npm run build
...

package.json parts that are relevant

"scripts": {
  "build": "rm -rf lib && graphql-codegen && tsc -p tsconfig.json && copyfiles -u 1 ./src/**/*.graphql lib && copyfiles -u 1 ./src/**/*.proto lib"
},
"devDependencies": {
  "@graphql-codegen/cli": "1.17.8",
  "@graphql-codegen/typescript": "1.17.8",
  "@graphql-codegen/typescript-resolvers": "^1.18.1",
  "@types/jest": "^27.0.2",
  "@types/node": "^16.10.2",
  "copyfiles": "^2.3.0",
  "graphql": "14.7.0",
  "jest": "27.2.4",
  "jest-cli": "27.2.4",
  "jest-extended": "^0.11.5",
  "nodemon": "^2.0.4",
  "ts-jest": "^27.0.5",
  "ts-node": "^10.2.1",
  "typescript": "^3.9.7"
},

tsconfig.json

"compilerOptions": {
  "target": "es2016",
  "module": "commonjs",
  "lib": ["es2019", "dom"],
  "strict": true,
  "noImplicitAny": true,
  "noUnusedLocals": true,
  "noUnusedParameters": true,
  "noImplicitReturns": true,
  "typeRoots": ["node_modules/@types"],
  "esModuleInterop": true,
  "resolveJsonModule": true,
  "outDir": "lib",
  "rootDir": "src",
  "resolveJsonModule": true,
  "allowJs": true
},
"ts-node": {
  "transpileOnly": true,
  "files": true
},
"include": ["**/*"],
"exclude": ["node_modules", "lib", "jest.*.ts", "jest.*.js", "**/generated/*"]

Edit:

Adding "skipLibCheck": true in my compilerOptions fixed the Namespace 'jest' has no exported member 'Mock' error.

I'm still getting errors though that I didn't mention before that I hoped would get resolved by fixing the first error.

The Errors I'm still getting from *.test.ts files are:

error TS2708: Cannot use namespace 'jest' as a value.
error TS2304: Cannot find name 'expect'.
error TS2304: Cannot find name 'beforeAll'.
error TS2582: Cannot find name 'test'.
  • 1
    Does it make a difference if you add `"skipLibCheck": true` in your `compilerOptions`? – Stefan Golubović Oct 04 '21 at 15:12
  • @StefanGolubović it does make a difference indeed. The Namespace error is now gone. I'm still getting errors I didn't mention before though, "Cannot use namespace 'jest' as a value" and "Cannot find name 'beforeAll'" and same error as beforeAll for expect, test and describe. I was hoping they would get resolved automagically if I managed to fix the Namespace error :) Any chance you know how to fix those also? – Finnur Eiríksson Oct 04 '21 at 16:25
  • 1
    Typescript is not my strongest suit. Perhaps you could try adding [`types`](https://stackoverflow.com/a/57241607/4778343) to `compilerOptions`. My guess is that something is off with the `tsconfig.json` which is somehow not reproducible in the local setup (e.g. type information is cached somewhere or something like that). – Stefan Golubović Oct 05 '21 at 07:14

2 Answers2

0

I have an idea that maybe could resolve your issue, as I had a similar problem that was caused because of version conflicts. Could you try to also copy your package-lock.json file to your docker image?

...
COPY package.json /opt/service/
COPY package-lock.json /opt/service/
...
Tom
  • 89
  • 6
  • Thank you for the suggestion. I did try doing that but still getting the same errors. When you run `npm install` a new package-lock.json is generated. If you however use `npm ci` package-lock.json is used, but it needs to be in sync with package.json. I tried copying both those files over and run `npm ci`but, like I said before, still getting same errors. – Finnur Eiríksson Oct 04 '21 at 23:06
0

This turned out to be a problem with tsconfig.json and my folder structure. I have multiple services and packages in this project. In the root folder (two folders up from each service) I have a tsconfig.json file that other services and packages are extending. Because I'm only copying the tsconfig.json file from the root and not node_modules, and the root tsconfig.json file is declaring "typeRoots": ["node_modules/@types"], services expect @types to be found in ../../node_modules.