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`

Created on 3 May 2019  路  8Comments  路  Source: microsoft/TypeScript


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)

Most helpful comment

I solved by adding in tsconfig.json
"types": [
"@types/jest"
]

or more ugly way add in one of the type files:
///

All 8 comments

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.

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 excludes will deploy tests in outDir while building...

@ilgazil You should not have tests folder in your specified rootDir in the first place.

I solved this by adding

to tsconfig.app.json of the app I tried to build

,
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]

hope this helps...

I solved by adding to tsconfig.json

"exclude": ["**/tests/*.test.ts"]

My scripts are located inside of tests folder.

I solved by adding in tsconfig.json
"types": [
"@types/jest"
]

or more ugly way add in one of the type files:
///

Solved this by adding to compilerOptions:

"compilerOptions": {
    ...,
    "types": ["...", "@types/jest"],
    ....
  },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

wmaurer picture wmaurer  路  3Comments

dlaberge picture dlaberge  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments