I'm seeking guidance on how to set up TypeScript with Jest. I've scoured numerous resources but haven't found a solution that works for me.
Below is my jest.config.js
:
module.exports = {
roots: [
'<rootDir>'
],
transform: {
"^.+\.ts$": "ts-jest"
},
testMatch: ['<rootDir>/tests/**/*.ts'],
moduleFileExtensions: ['ts', 'js'],
modulePaths: [
'<rootDir>/src/js/'
],
moduleNameMapper: {
'Services(.*)': '<rootDir>/src/js/$1'
},
};
The issue lies with the module name mapper, as it doesn't seem to work despite trying both modulePaths
and moduleNameMapper
. The error message simply states: "Cannot find module ...".
Furthermore, when attempting to mock the module name mapper (e.g.,
'i': '<rootDir>/src/js/State.ts'
) and get it functioning, I encounter an "Unexpected token export" error. This is perplexing considering I followed the guide at https://basarat.gitbooks.io/typescript/docs/testing/jest.html and have replicated the exact code from that example. Even after resorting to using Babel for Jest, the problem persists.
Despite exhaustive googling and multiple attempts, no breakthrough has been made. I've invested close to two hours in this troubleshooting process, fully aware that there ought to be a straightforward solution given the popularity of Jest and TypeScript.
Update: Listed below is my tsconfig.js file:
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es2015",
"target": "es5",
"jsx": "react",
"allowJs": true
}
}