TypeScript: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`
TypeScript Version: 3.4.5
Code
package.json dev dependencies:
"devDependencies": {
"@types/classnames": "^2.2.7",
"@types/cookie": "^0.3.2",
"@types/facebook-js-sdk": "^3.2.1",
"@types/graphql": "^14.2.0",
"@types/jest": "^24.0.12",
"@types/material-ui": "^0.21.6",
"@types/next": "^8.0.4",
"@types/nookies": "^1.1.0",
"@types/react": "^16.8.15",
"@types/react-dom": "^16.8.4",
"@types/styled-jsx": "^2.2.8",
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
"@zeit/next-typescript": "^1.1.1",
"apollo-cache-inmemory": "^1.5.1",
"dotenv-webpack": "^1.7.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-jest": "^22.5.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-react-hooks": "^1.6.0",
"jest": "^24.7.1",
"nookies": "^2.0.5",
"prettier": "^1.17.0",
"react-testing-library": "^7.0.0",
"ts-jest": "^24.0.2",
"typescript": "^3.4.5"
}
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"lib": [
"dom",
"esnext"
],
"module": "esnext",
"target": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"removeComments": false,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
},
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.test.ts",
"**/*.test.tsx",
]
}
file.test.tsx:
test()
Expected behavior:
No issue. @types/jest is already installed. All other @types/* are working fine.
Actual behavior:
Error:
Cannot find name 'test'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.ts(2582)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 31
- Comments: 20
I solved by adding in tsconfig.json “types”: [ “@types/jest” ]
or more ugly way add in one of the type files: /// <reference path="../node_modules/@types/jest/index.d.ts" />
Solved this by adding to compilerOptions:
You can explicitly add the imports
import { describe, test } from '@jest/globals';I solved this by adding
to
tsconfig.app.jsonof the app I tried to buildhope this helps…
This work for me!
This ☝️ is the answer. Some tutorials/articles instruct you to add “**/*.spec.ts” and similar to “excludes” but that makes VS Code to be unable to find the jest types. Removing the exclude solves the problem.
But removing these from
excludeswill deploy tests in outDir while building…Nevermind, the example I was provided added the excludes to tsconfig but if I remove it then it works fine. I don’t know why they would exclude the test files but whatever, problem solved.
In my case, I am using
vitestand I have imported them explicitly which worked for me.import { describe, it, expect } from "vitest";I solved by adding in
tsconfig.jsonAdding
"types": [ "@types/jest"]didn’t work to me, and I don’t want to remove the exclude because otherwise it will deploy tests to my outDir while building.Still showing me
Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try 'npm i --save-dev @types/jest' or 'npm i --save-dev @types/mocha'.ts(2582)None of suggested configurations above worked for me.
I solved by adding to tsconfig.json
"exclude": ["**/tests/*.test.ts"]My scripts are located inside of tests folder.
you can import types/jest in test file
import "@types/jest";For me, I need install jest in my project. After install, this not appears to me anymore.
npm i jest @types/jest ts-jest typescript -D.The latter comment is the solution.
Jest’s error message is misleading Since it’s
@types/jestwhich should be added and notjestSee below:
error TS2593: Cannot find name 'test'. Do you need to install type definitions for a test runner? Trynpm i --save-dev @types/jestornpm i --save-dev @types/mochaand then addjestormochato the types field in your tsconfig.这个真的很棒
@AshwaryaSmridhi thank you for posting your fix here - it worked for me 🙌🏼