2

I know there are some threads about a similar topic but tried various suggested solutions and neither worked.

The problem: When running the jest tests written in TS on docker as part of a Jenkins pipeline, I got: Cannot find module '../../../common/src/SomeType' from 'dist/src/services/foo.services.js', where foo.services.js is what being tested.

This is my project structure; don't know why it was initially structured like this. I joint the party way too late.

MyProject123
    client
       package.json
       tsconfig.json
       ...
    common
       tsconfig.json
       src
         SomeType.ts  (NOTE: this is what can't be found by jest!)
         Others.ts
    server
       dist
       src
          foo.services.ts (NOTE: this is what being tested)
       tests
          unit
            services
               foo.services.spec.ts (NOTE: this is the test!)
       tsconfig.json
       jest.conf.js

Inside foo.services.ts, it references SomeType as:

import { SomeType } from '../../../common/src/SomeType';

Inside server/tsconfig.json, it set the references in order to reference the common folder:

"references": [
    {
      "path": "../common"
    }
  ],
  "include": [
    "src/**/*.ts",
    "*.ts",
    "src/**/*.json",
    "tests/**/*.ts"
    ],

In jest.conf.js under server folder, it has:

moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/src/$1'
    },`

Inside server/package.json, it has:

"jest": {
    "testEnvironment": "node",
    "rootDir": "./",
    "modulePaths": [
      "<rootDir>"
    ]
  },

What's odd is all tests work fine locally on my machine. But it doesn't work when running in docker.

Guess I am missing some jest config setting somewhere?

----------------------------EDIT 1 --------------------------- Here is our dockerfile that is relevant for the part:

FROM company.com/nodejs:16
ARG BUILD_MODE
USER root

ARG http_proxy=******
ARG https_proxy=$http_proxy
ARG no_proxy=******
ARG CI=true
ARG NPM_CONFIG__AUTH

WORKDIR /app
COPY . .

RUN npm cache clean --force

RUN npm install npm -g
WORKDIR /app/server
COPY server/package.json .
COPY server/package-lock.json .
COPY server/.npmrc .
RUN npm ci --loglevel verbose
RUN npm run build-ts
RUN rm -rf tests/coverage
RUN npm run test:unit //NOTE: runs the server unit tests
2
  • Hey, can you also provide your Dockerfile please ? Commented Nov 2, 2022 at 12:40
  • @lafusew. Dockerfile added. Commented Nov 2, 2022 at 13:00

1 Answer 1

1

From the error you have

Cannot find module '../../../common/src/SomeType' from 'dist/src/services/foo.services.js'

looks like you are running Jest on your bundle files. (from dist/...)

As a reminder, Jest runs on source files, not on bundled files.

A quick fix, would be to run your test suite before bundling your App in your dockerfile :

# ...
RUN npm run test:unit
RUN npm run build-ts
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.