I have been making an app in Typescript with Express. Now I want to make unit tests (a bit late, I know) and found this problem:
- To run the tests, imports inside of .ts files need not to have the file extension .js
- To compile the code tested, imports inside of .ts files need to have the file extension .js
I’m pretty sure the is some config that I’m missing, but I would aprecciate if you told me.
jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} */
// module.exports = {
// preset: 'ts-jest',
// testEnvironment: 'node',
// };
export default {
preset:"ts-jest",
testEnvironment:"node",
testMatch: [
'**/*.test.ts' // Solo ejecutar archivos .test.js
]
}
tsconfig.json
{
"compilerOptions":{
"target": "es2016",
"module": "Node16",
"rootDir": "./src",
"sourceMap": true,
"outDir": "./dist",
"removeComments": false,
"noEmitOnError": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
ps: formatting may be wrong in tsconfig.json because I wrote it by hand instead of copypasting
What I did:
- Eliminating the .js extension inside .ts files
- Changing the moduleResolution value to “bundle” given what I read here https://www.typescriptlang.org/tsconfig/#moduleResolution but the compiler said that moduleResolution couldn’t have that value
New contributor
Mariana Arona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.