Home > front end >  ts-jest failing in Docker container only, passes locally. "cannot find module or corresponding
ts-jest failing in Docker container only, passes locally. "cannot find module or corresponding

Time:02-16

I get the following error when I try to run a test inside a Docker container:

//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

My file structure:

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

Locally, my test passes with only this in the 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:

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

Locally my tests also pass with only this configured in tsconfig.json:

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

I also have this in my 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.

CodePudding user response:

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

(So when any command is execute it is also used as a base directory)

  • Related