當我嘗試在 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。
uj5u.com熱心網友回復:
嘗試在您的 dockerfileWORKDIR /app之后使用FROM *,這將使該/app目錄成為所有其他命令的當前目錄(因此您的應用程式將保存在其中),然后從中執行
(所以當任何命令被執行時,它也被用作基本目錄)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/424027.html
