Issue
I've reached a dead end trying to fix this issue. I am using the following TypeScript configuration:
{
"compilerOptions": {
"module": "es2022",
"moduleResolution": "nodenext",
"target": "es2017",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"noImplicitAny": true,
"outDir": "./dist",
"rootDir": "./src",
"typeRoots": [
"./src/types", "./node_modules/@types"],
"allowJs": true,
"strictFunctionTypes": true,
"noImplicitReturns": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules"],
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": true
}
}
As you see the moduleResolution is set to nodenext, and because of that I have to explicitly add a file extension when importing, like this: import ServerError from '../models/Errors/ServerError.js';
. Otherwise, I get an error that the module was not found.
Everything is working fine, but when I launch my tests I get an error: Cannot find module '../models/Errors/ServerError.js' from '../src/services/usersService.ts'
. So basically jest is trying to find the file ServerError.js, but it does not exist, because all files have a .ts extension, so it should be ServerError.ts. If I try to change .js to .ts in my files I also will get an error.
I can't finish my task because of this problem, so I would appreciate any help.
Solution
Finally, I've managed to solve this problem after doing some research and updating my jest config file.
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
import type { Config } from 'jest';
const config: Config = {
transform: {
'\\.[jt]sx?$': 'ts-jest'
},
globals: {
'ts-jest': {
useESM: true
}
},
moduleNameMapper: {
'(.+)\\.js': '$1'
},
extensionsToTreatAsEsm: ['.ts']
};
With this configuration, everything is working fine. I hope that it will help somebody.
Answered By - YulianKh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.