Currently, I am utilizing Jest in conjunction with TypeScript. Although my code functions properly and I can successfully build my project, Visual Studio Code consistently displays an error pertaining to all Jest methods (describe()
, test()
...):
Cannot find name 'describe'. Have you considered installing type definitions for a test runner? Give `npm i @types/jest` or `npm i @types/mocha` a try.ts(2582)
I have separate directories named src
and tests
. Despite following configurations sourced from the internet, none of it seems to make a difference. What exactly am I overlooking here? So far, the only workaround has been to include my tests
folder within the include
setting in tsconfig
, which is less than ideal as it resides in the dist
directory.
Here are the development dependencies that have been installed: jest ts-jest @types/jest
My tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true,
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"allowJs": true,
"jsx": "react",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": "./",
"paths": {
"*": ["src/*"]
},
"typeRoots": ["./node_modules/@types"],
"types": ["node", "jest"]
},
"strict": true,
"compileOnSave": false,
"include": ["src"]
}
Furthermore, my jest.config.js
:
module.exports = {
roots: ['<rootDir>'],
preset: 'ts-jest',
testRegex: 'tests/src/.*\\.test.(js|jsx|ts|tsx)$',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
transformIgnorePatterns: [],
snapshotSerializers: ['enzyme-to-json/serializer'],
moduleDirectories: ['node_modules', 'src', 'tests'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'\\.(css|scss|jpg|png|svg)$': 'mocks/empty.ts',
},
setupFilesAfterEnv: ['<rootDir>/tests/jest.setup.ts'],
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{js{,x},ts{,x}}', '!src/index.tsx', '!src/custom.d.ts'],
}