【发布时间】:2021-10-05 16:58:53
【问题描述】:
我正在尝试将我的服务移动到 Docker 容器中。在本地构建项目可以正常工作,但是当我尝试将其构建为 docker build 的一部分时,出现以下错误:
node_modules/jest-extended/types/index.d.ts(135,39): error TS2694: Namespace 'jest' has no exported member 'Mock'.
请找到我的 dockerfile:
FROM node:12
COPY package.json /opt/service/
WORKDIR /opt/service/
RUN npm install
COPY . .
RUN npm run build
...
相关的package.json部分
"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/*"]
编辑:
在我的 compilerOptions 中添加 "skipLibCheck": true 修复了 Namespace 'jest' has no exported member 'Mock' error.
虽然我之前没有提到,但我仍然遇到错误,我希望通过修复第一个错误来解决。
我仍然从 *.test.ts 文件中得到的错误是:
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'.
【问题讨论】:
-
在
compilerOptions中添加"skipLibCheck": true会有影响吗? -
@StefanGolubović 确实有所作为。命名空间错误现在消失了。我仍然收到我之前没有提到的错误,“不能使用命名空间'jest'作为值”和“找不到名称'beforeAll'”以及与beforeAll相同的错误,用于期望、测试和描述。如果我设法修复命名空间错误,我希望它们会自动解决:) 你是否也知道如何修复这些错误?
-
Typescript 不是我的强项。也许您可以尝试将
types添加到compilerOptions。我的猜测是tsconfig.json出了点问题,这在本地设置中无法重现(例如,类型信息缓存在某处或类似的地方)。