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'.
CodePudding user response:
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/
...
CodePudding user response:
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
.