Error in executing jest test cases for nodejs application with mongodb

When I run jest test cases for nodejs application. I am facing this error after tests execution succeed.

Error: ENOENT: no such file or directory, open '/var/lib/jenkins/workspace/xx-yy-zz/backend/node_modules/mongodb-connection-string-url/src/index.ts'

However this error doesn’t come on my local system. This file doesn;t exist neither on local system nor on jenkin workspace but still it give this error. I am not getting the reason. Due to which coverage folder is not generated on jenkin workspace however on local it works.

This is my jest.config.js

const config: Config.InitialOptions = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    verbose: true,
    coveragePathIgnorePatterns: [ "\\\\node_modules\\\\"],
    coverageProvider: "v8",
    testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'],
    setupFilesAfterEnv: ['./jest.setup.ts'],
    collectCoverage: true,
    coverageDirectory: "<rootDir>/coverage",
}
export default config;

Hey @Mayank_Sharma1,

Welcome to the MongoDB Community!

I suspect there may be a few things that could be causing this error:

  • Could you confirm that Jenkins and your local machine have the same environments such as Node.js/npm versions, dependencies installed, etc?

  • Could you also double check your Jest config on Jenkins matches your local config, especially around test matches and module name mapper which controls what files get processed?

  • The caching issue on jest can sometimes cache files between runs which could cause unexpected behavior. Try clearing Jest’s cache folder on Jenkins between builds.

  • Also the error indicates an absolute path that may not resolve correctly on Jenkins. Try using relative paths in your config.

The root cause is probably an environmental difference between the two systems. Doing some troubleshooting can help identify the specifics of where the issue is occurring.

Regards,
Kushagra

Hi @Kushagra_Kesav thanks for your comments. To further find out the issue, I run the jest test cases using docker on local machine and able to replicate issue. When I run unit-test.sh script on local bash terminal.

Build the Docker image

docker build -f DockerfileUnitTest -t my-node-app-back .

Run tests inside the Docker container

docker run --rm my-node-app-back

Check the exit code of the test command

if [[ $? -ne 0 ]]; then
echo “Tests failed. Exiting pipeline.”
exit 1
fi

I got error "Error: ENOENT: no such file or directory, open ‘/usr/src/app/node_modules/mongodb-connection-string-url/src/index.ts’

This was my simple docker file.

Use the official Node.js image as the base image

FROM node:18.14-alpine
WORKDIR /usr/src/app
COPY ./studio-backend/package*.json ./
RUN npm install
COPY ./studio-backend .
CMD [“npm”, “test”]

If I run npm test on window terminal then no such error came