I have hard time getting jest to work with typescript project that use ES modules with import
syntax.
My project was initially written for commonjs, jest test run fine. But then I decided to switch to ES Modules(for learning purpose), jest is not happy ヽ(`Д´)ノ
The tools that I am using: typescript, jest, ts-jest
The issue start with import
syntax.
The following are codes that I had tried.
// projectRoot/src/app.ts
export default someFunction = (): void => {
// some code
}
If
// projectRoot/__tests__/app.test.ts
import someFunction from '../src/app'; // without file extension
/* This execute perfectly fine */
But
// projectRoot/__tests__/app.test.ts
import someFunction from '../src/app.ts' // with .ts
/*
● Test suite failed to run
__tests__/app.test.ts:1:25 - error TS2691: An import path cannot end with a '.ts' extension. Consider importing '../src/app' instead.
1 import information from '../src/app.ts';
*/
And
// projectRoot/__tests__/app.test.ts
import someFunction from '../src/app.js'; // with .js
/*
● Test suite failed to run
Cannot find module '../src/app.js' from '__tests__/app.test.ts'
*/
As above example, jest(or maybe ts-jest?) is not happy if I import the module with extension(which is a must for ES Modules). I did some searching online, but seem like jest doc is not very supportive for ES Modules. Same goes to ts-jest by this reading
My Project structure:
/projectRoot
├── /src/app.ts
├── /__tests__/app.test.ts
Inside package.json file has value "type": "module"
tsconfig.json:
{
"compilerOptions": {
"target": "ES2015",
"module": "ESNEXT",
"outDir": "./build",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["./src"],
"exclude": ["node_modules", "**/*.test.ts"]
}
jest.config.js
export default {
"roots": [
//"<rootDir>/src"
"<rootDir>"
],
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"preset": "ts-jest",
"testEnvironment": 'node'
}
Please help. Thank you.