打字稿在 vscode 中找不到名称“test”

zen*_*hen 7 typescript jestjs visual-studio-code

我正在尝试在我的打字稿应用程序中使用 jest。

当我运行命令 jest 时,它工作正常。

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.
Run Code Online (Sandbox Code Playgroud)

测试代码就像

// 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));
Run Code Online (Sandbox Code Playgroud)

但是 vscode 告诉我它找不到名称“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)
Run Code Online (Sandbox Code Playgroud)

我已经安装了开玩笑的类型。

我怀疑 tsconfig 中可能有问题。我根据答案更改配置

但它仍然不起作用。

存储库是https://github.com/Zx55/c0-compiler

包.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"
  }
}
Run Code Online (Sandbox Code Playgroud)

配置文件

{
    "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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

use*_*025 13

我无法通过修改我的 tsconfig.json 和 jest.config.js、更改 jest 版本以及引用这篇文章来使其工作:在 jest 打字稿中找不到名称

我能够通过显式导入方法来消除错误,如下所示:

import { expect, test } from '@jest/globals';
Run Code Online (Sandbox Code Playgroud)


sko*_*ovy -1

看起来您已经@types/jest按照错误消息中的建议安装了 types() 并安装了ts-jest。该配置似乎可能不完全正确。您是否尝试过使用jest.config.js文件(尤其是transform密钥)来配置 Jest?

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
}
Run Code Online (Sandbox Code Playgroud)

本文档还提供了使用 TypeScript 设置 Jest 的良好概述:https://basarat.gitbooks.io/typescript/docs/testing/jest.html