[英]ts-jest failing in Docker container only, passes locally. "cannot find module or corresponding type declarations"
I get the following error when I try to run a test inside a Docker container:当我尝试在 Docker 容器内运行测试时出现以下错误:
//test/Client-mock.spec.ts:1:27 - error TS2307: Cannot find module '../src/Client' or its corresponding type declarations.
import {client} from '../src/Client'
I've been following some sites similar to this too and I feel like I'm very close but keep missing the exact right configuration:我也一直在关注一些与此类似的网站,我觉得我已经很接近了,但一直缺少正确的配置:
Jest + Typescript + Absolute paths (baseUrl) gives error: Cannot find module Jest + Typescript + 绝对路径 (baseUrl) 给出错误:找不到模块
My file structure:我的文件结构:
-src
-- Client.ts
-test
-- Client-mock.spec.ts
Locally, my test passes with only this in the jest.config.js:在本地,我的测试仅在 jest.config.js 中通过:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
My local yarn test
also passed with this in the package.json initially:我的本地
yarn test
最初也通过了 package.json:
"jest": {
"roots": [
"<rootDir>/test/"
],
"preset": "ts-jest"
Locally my tests also pass with only this configured in tsconfig.json:在本地,我的测试也通过了 tsconfig.json 中的配置:
{
"compilerOptions": {
"target": "ES6",
"moduleResolution": "node",
"resolveJsonModule": true,
"types": ["@types/jest", "node", "@types/node"],
"esModuleInterop": true
}
}
I also have this in my Dockerfile:我的 Dockerfile 中也有这个:
FROM node:16.3.0-alpine
COPY ./src ./src
COPY ./expectations ./expectations
COPY ./package.json ./package.json
COPY ./tsconfig.json ./tsconfig.json
COPY ./jest.config.js ./jest.config.js
COPY ./test ./test
RUN yarn install
# Run as post start script to load expectations
CMD ["yarn", "start", "test"]
Inside my container if I ls
, I see my test file and config file and in my node_modules folder I see ts-jest.如果我在我的容器中
ls
,我会看到我的测试文件和配置文件,在我的 node_modules 文件夹中我会看到 ts-jest。
Try using WORKDIR /app
just after FROM *
of your dockerfile, that will make the /app
directory as current directory for all other commands (so your app would be held in it) and then executed from it尝试在您的 dockerfile 的
FROM *
之后使用WORKDIR /app
,这将使/app
目录成为所有其他命令的当前目录(因此您的应用程序将保存在其中),然后从中执行
(So when any command is execute it is also used as a base directory) (所以当执行任何命令时,它也被用作基本目录)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.