Hey there, I'm currently working on a project which includes TypeScript and Express.js. Right now, my main focus is on setting up the tests. However, when I try to run yarn test
(which essentially runs jest
without any additional flags), I encounter this specific error:
PS C:\Users\joaov\Documents\gittin> yarn test
yarn run v1.22.10
$ jest
Error: Jest: Failed to parse the TypeScript config file C:\Users\joaov\Documents\gittin\jest.config.ts
TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:1:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
1 module.exports = {
~~~~~~
at readConfigFileAndSetRootDir (C:\Users\joaov\Documents\gittin\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:118:13)
at readConfig (C:\Users\joaov\Documents\gittin\node_modules\jest-config\build\index.js:227:18)
at readConfigs (C:\Users\joaov\Documents\gittin\node_modules\jest-config\build\index.js:412:26)
at runCLI (C:\Users\joaov\Documents\gittin\node_modules\@jest\core\build\cli\index.js:220:59)
at Object.run (C:\Users\joaov\Documents\gittin\node_modules\jest-cli\build\cli\index.js:163:37)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I've been informed that the reason behind this error is the inclusion of the property typeRoots
in my tsconfig.json
. By commenting out that line, the tests do work as intended, but I am curious to find out how I can make the typeRoots functionality compatible with jest in order to retain that necessary local type.
To expedite things, here are the contents of my tsconfig.json
and jest.config.ts
files respectively:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"removeComments": true,
"strict": true,
"strictPropertyInitialization": false,
"typeRoots": ["./src/@types"],
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": [
"src/**/*.spec.ts",
"src/**/*.test.ts",
"jest.config.ts",
"ormconfig.js",
"node_modules"
]
}
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};