打字稿在vscode中找不到名称'test'
Posted
技术标签:
【中文标题】打字稿在vscode中找不到名称\'test\'【英文标题】:typescript cannot find name 'test' in vscode打字稿在vscode中找不到名称'test' 【发布时间】:2020-01-10 13:54:07 【问题描述】:我正在尝试在我的打字稿应用程序中使用 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.
测试代码是这样的
// 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));
但是 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)
我已经安装了开玩笑的类型。
我怀疑 tsconfig 可能有问题。 我根据Answer更改配置
但还是不行。
仓库是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"
]
【问题讨论】:
你有没有想过这个问题? 对我来说我是这样修复的***.com/a/59463822/4298218 【参考方案1】:我无法通过修改我的 tsconfig.json 和 jest.config.js、更改 jest 版本以及参考这篇文章来实现此功能:cannot find name it in jest typescript
我可以通过像这样显式导入方法来消除错误:
import expect, test from '@jest/globals';
【讨论】:
这行得通,并由此处第一段中的文档支持:jestjs.io/docs/en/api【参考方案2】:您似乎已按照错误消息中的建议安装了类型 (@types/jest
) 并安装了 ts-jest
。配置似乎并不完全正确。您是否尝试过使用 jest.config.js
文件配置 Jest,尤其是使用 transform
键?
module.exports =
"roots": [
"<rootDir>/src"
],
"transform":
"^.+\\.tsx?$": "ts-jest"
,
本文档还提供了使用 TypeScript 设置 Jest 的良好概述:https://basarat.gitbooks.io/typescript/docs/testing/jest.html
【讨论】:
以上是关于打字稿在vscode中找不到名称'test'的主要内容,如果未能解决你的问题,请参考以下文章
打字稿错误使用googlemaps javascript API时在ionic2中找不到名称'google'