简体   繁体   English

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

[英]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.

相关问题 Typescript:在 Docker 容器中找不到模块 XWZ 或其对应的类型声明 - Typescript: Cannot find module XWZ or its corresponding type declarations in Docker Container 找不到模块“。” 或其相应的类型声明 - Cannot find module '.' or its corresponding type declarations TS2307 错误:找不到模块“../constants”或其相应的类型声明 - TS2307 Error: Cannot find module '../constants' or its corresponding type declarations 错误 TS2307:找不到模块或其对应的类型声明。 在 Github 操作 - error TS2307: Cannot find module or its corresponding type declarations. on Github Actions src/index.ts:12:22 - 错误 TS2307:找不到模块“./schema.graphql”或其相应的类型声明 - src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations 在打字稿节点项目中使用 monorepo 样式测试目录时,ts-jest 找不到类型信息 - ts-jest cannot find type information when using monorepo style tests directory in a typescript node project 找不到模块“./App.svelte”或其对应的类型声明 - Cannot find module './App.svelte' or its corresponding type declarations 找不到模块“hardhat/config”或其相应的类型声明 - Cannot find module 'hardhat/config' or its corresponding type declarations 错误:找不到模块“jsonwebtoken”或其对应的类型声明 NODE.JS 和 TYPESCRIPT - ERROR: Cannot find module 'jsonwebtoken' or its corresponding type declarations with NODE.JS and TYPESCRIPT ts-jest:将“@”字符映射到 /src 文件夹 - ts-jest: map '@' char to /src folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM