I have some setup problems that result in the TypeScript error below.
Cannot find name 'describe'. Do you need to install type definitions for a test runner?
I installed yarn packages in docker:
I have tried the following suggested solutions from Cannot find name 'describe'. Do you need to install type definitions for a test runner? but none of them worked at my end.
- Commenting out prop
types
intsconfig.json
, and created atsconfig.spec.json
- @Melvin Sy - Did the checklist of @Greg Wozniak
- Added
@types/jest
in proptypes
intsconfig.json
- @Stevo - Creating
tests
folder - @Jels Boulangier - etc.
Are there any other solutions?
Structure:
- src
- tests
-- sample.test.ts
- babel.config.js
- jest.config.ts
- tsconfig.json
- package.json
sample.test.ts
describe('utils', () => { // <----------------------typescript error here and below
describe('utils#getMessage()', () => {
it.todo('should return a message');
});
});
jest.config.ts
export default {
clearMocks: true,
verbose: true,
preset: 'ts-jest',
collectCoverage: true,
collectCoverageFrom: ['src/tests/*.ts', '**/*.ts', '!**/*.d.ts'],
testEnvironment: 'node',
testRegex: '(src/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
};
tsconfig.json
{
"compilerOptions": {
"module": "esnext",
"target": "es2017",
"strict": true,
"sourceMap": true,
"declaration": true,
"types": ["@types/jest", "jest", "node"],
"resolveJsonModule": true,
"outDir": "./dist",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src", "tests", "src/tests"]
}
tsconfig.spec.json
{
"compilerOptions": {
"types": ["@types/jest", "jest", "node"]
}
}
babel.config.js
module.exports = {
presets: [
'@babel/preset-env',
'@babel/preset-react',
'@babel/preset-typescript',
],
};
package.json
{
"dependencies": {
"@types/jest": "^28.1.3",
"jest": "26.6.0",
"ts-jest": "^28.0.5",
... more code here
},
... more code here
}