繁体   English   中英

ts-jest 仅在 Docker 容器中失败,在本地通过。 “找不到模块或相应的类型声明”

[英]ts-jest failing in Docker container only, passes locally. "cannot find module or corresponding type declarations"

当我尝试在 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'

我也一直在关注一些与此类似的网站,我觉得我已经很接近了,但一直缺少正确的配置:

Jest + Typescript + 绝对路径 (baseUrl) 给出错误:找不到模块

我的文件结构:

-src
 -- Client.ts
-test
 -- Client-mock.spec.ts

在本地,我的测试仅在 jest.config.js 中通过:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

我的本地yarn test最初也通过了 package.json:

 "jest": {
    "roots": [
      "<rootDir>/test/"
    ],
    "preset": "ts-jest"

在本地,我的测试也通过了 tsconfig.json 中的配置:

{
  "compilerOptions": {
    "target": "ES6",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "types": ["@types/jest", "node", "@types/node"],
    "esModuleInterop": true
  }
}

我的 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"]

如果我在我的容器中ls ,我会看到我的测试文件和配置文件,在我的 node_modules 文件夹中我会看到 ts-jest。

尝试在您的 dockerfile 的FROM *之后使用WORKDIR /app ,这将使/app目录成为所有其他命令的当前目录(因此您的应用程序将保存在其中),然后从中执行

(所以当执行任何命令时,它也被用作基本目录)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM