Charleskimani
Charleskimani

Reputation: 510

Problem mapping path in ts-jest (TypeScript Jest), module cannot be found

I want to implement some jest tests in my backend and so I was trying to map my paths that I have configured in tsconfig.json via the moduleNameMapper of jest.config.js but when I run the tests I find the file is still not imported and I am shown this error on line 8 ⬇
error received from running test

Please assist me to map my paths correctly, I would highly appreciate any help.
To help you assist me here are the important files.
jest.config.js (where jest is usually configured) ⬇

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testMatch: ["**/***.test.ts"],
  verbose: true,
  forceExit: true,
  
  moduleNameMapper: {
    '@util/(.*)': '<rootDir>/src/util/$1'
  }
};

tsconfig.json (normal config file for typescript) ⬇

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "baseUrl": "src",
    "paths": {
      "@util/*": ["util/*"]
    },
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

Upvotes: 2

Views: 8490

Answers (2)

Charleskimani
Charleskimani

Reputation: 510

I created a file named .babelrc with this contents ⬇ :

{
  "presets": ["@babel/preset-env"]
}

I then configured jest.config.js as shown below ⬇

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  transform: {
    '^.+\\.ts$': 'ts-jest',
    '^.+\\.js$': 'babel-jest',
    '^.+\\.mjs$': 'babel-jest',
  },
  moduleDirectories: ['node_modules', '<rootDir>/src'],
  moduleNameMapper: {
    '@controllers/(.*)': '<rootDir>/src/controllers/$1',
    '@middleware/(.*)': '<rootDir>/src/middleware/$1',
    '@models/(.*)': '<rootDir>/src/models/$1',
    '@routes/(.*)': '<rootDir>/src/routes/$1',
    '@types/(.*)': '<rootDir>/src/types/$1',
    '@util/(.*)': '<rootDir>/src/util/$1',
  }
};

I then configured tsconfig.json as shown below ⬇:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "baseUrl": "src",
    "paths": {
      "@util/*": ["util/*"]
    },
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "types": ["jest"]
  }
}

Vote of thanks to Benjamin Drury & @Ashok for the much helpful support.

Upvotes: 3

Ashok
Ashok

Reputation: 2706

This issue is occuring due to absolute import. If you look closely, you can observe that your import statement is util/logger. This kind of imports are not properly resolved.

In case you are using VSCode, then this is a common occurence, since VSCode tries to reduce the length of your import statement.

To fix this, use relative import. So your import should look like as follow:

import logger from '../util/logger'

(Note: Above import is relative to the path src/middleware/authenticateUser.ts, please update the import based on in which file you are using the import)

Upvotes: 0

Related Questions