12

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\\..\_jest@24.9.0@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"
    ]
}
zen chen
  • 131
  • 1
  • 4

2 Answers2

28

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';
user3291025
  • 997
  • 13
  • 20
  • 1
    This worked, and is backed up by documentation in the first paragraph here: https://jestjs.io/docs/en/api – mfperzel Dec 06 '20 at 19:42
-1

It looks like you have installed the types (@types/jest) as suggested in the error message and installed ts-jest. The config seems like it may not be entirely correct. Have you tried to configure Jest with a jest.config.js file, particularly with a transform key?

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
}

This documentation also provides a good overview to setup Jest with TypeScript: https://basarat.gitbooks.io/typescript/docs/testing/jest.html

skovy
  • 5,430
  • 2
  • 20
  • 34