Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript cannot find name 'test' in vscode

I'm trying to use jest in my typescript application.

When I run the command jest, it work fine.

yarn run v1.17.3
$ jest --coverage --verbose

C:\Users\hasee\Documents\Repository\c0-compiler>"node"  "C:\Users\hasee\Documents\Repository\c0-compiler\node_modules\.bin\\..\[email protected]@jest\bin\jest.js" 
--coverage --verbose
 PASS  tests/sum.test.ts (6.432s)
  √ add (3ms)

----------|----------|----------|----------|----------|-------------------|    
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |    
----------|----------|----------|----------|----------|-------------------|    
All files |      100 |      100 |      100 |      100 |                   |    
 sum.ts   |      100 |      100 |      100 |      100 |                   |    
----------|----------|----------|----------|----------|-------------------|    
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        6.896s
Ran all test suites.
Done in 9.73s.

The test code is like

// sum.ts
export default (a: number, b: number): number => a + b;

// sum.test.ts
import sum from './sum';

test('test sum', () => expect(sum(1, 2)).toBe(3));

But vscode tell me it cannot find the name 'test'.

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)

I have installed types for jest.

I doubt there may be something wrong in tsconfig. And I change the config based on Answer

But it still doesn't work.

The repository is https://github.com/Zx55/c0-compiler

package.json

{
  "name": "c0-compiler",
  "version": "0.0.1",
  "private": true,
  "main": "./dist/js/main.js",
  "scripts": {
    "cli": "ts-node ./src/c0/c0-cli",
    "packapp": "webpack --config ./config/webpack.config.ts",
    "start": "electron .",
    "test": "jest --coverage --verbose"
  },
  "author": "Zx55",
  "license": "MIT",
  "devDependencies": {
    "@types/classnames": "^2.2.9",
    "@types/html-webpack-plugin": "^3.2.1",
    "@types/jest": "^24.0.18",
    "@types/node": "^12.7.3",
    "@types/react": "^16.9.2",
    "@types/react-dom": "^16.9.0",
    "@types/react-router-dom": "^4.3.5",
    "cache-loader": "^4.1.0",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^3.2.0",
    "electron": "^6.0.7",
    "electron-packager": "^14.0.5",
    "fork-ts-checker-webpack-plugin": "^1.5.0",
    "html-webpack-plugin": "^3.2.0",
    "jest": "^24.9.0",
    "json5": "^2.1.0",
    "object-keys": "^1.1.1",
    "object.getownpropertydescriptors": "^2.0.3",
    "omit.js": "^1.0.2",
    "source-map-loader": "^0.2.4",
    "style-loader": "^1.0.0",
    "thread-loader": "^2.1.3",
    "ts-jest": "^24.0.2",
    "ts-loader": "^6.0.4",
    "ts-node": "^8.3.0",
    "typescript": "3.5.3",
    "webpack": "^4.39.3",
    "webpack-cli": "^3.3.7"
  },
  "dependencies": {
    "antd": "^3.23.1",
    "classnames": "^2.2.6",
    "rc-animate": "^2.10.0",
    "rc-queue-anim": "^1.8.2",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-router-dom": "^5.0.1",
    "unstated-next": "^1.1.0"
  },
  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/Zx55/c0-compiler.git"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "outDir": "./dist/",
        "sourceMap": true,
        "jsx": "react",
        "allowSyntheticDefaultImports": true,
        "allowJs": false,
        "noUnusedLocals": true,
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "types": ["node", "jest"],
        "typeRoots": [
            "../node_modules/@types"
        ]
    },
    "include": [
        "../src/**/*.ts",
        "../src/**/*.tsx",
        "../tests/*.test.ts",
        "../tests/*.spec.ts"
    ],
    "exclude": [
        "../coverage",
        "../dist",
        "../node_modules"
    ]
}
like image 868
zen chen Avatar asked Sep 08 '19 15:09

zen chen


People also ask

Can t find name jest?

To fix the “cannot find name 'it'” Jest error, install the type definitions for Jest with npm i -D @types/jest and add them to the types array in your tsconfig. json file.

How do I test a TypeScript code?

For TypeScript, unit tests are run against the generated JavaScript code. In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing Debug.


1 Answers

I was unable to get this to work by modifying my tsconfig.json and jest.config.js, changing jest versions, and also referencing this post: cannot find name it in jest typescript

I was able to remove the error by importing the methods explicitly as so:

import { expect, test } from '@jest/globals';
like image 93
user3291025 Avatar answered Sep 24 '22 08:09

user3291025