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
dragonfly02
  • 3,403
  • 32
  • 55

1 Answers1

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
mlegrix
  • 799
  • 5
  • 12