尝试使用Jest运行测试时出现此错误:
FAIL src/__tests__/jokeGenerator.test.tsx
● Test suite failed to run
TypeError: environment.teardown is not a function
at node_modules/jest-runner/build/run_test.js:230:25
我在这里遇到了可能的解决方案:How to solve TypeError: environment.teardown is not a function?
但是在执行建议后:删除了yarn.lock文件,node_modules文件夹,从package.json中删除了Jest,并再次使用yarn重新安装了所有内容-我遇到了一个新问题:
● Test suite failed to run
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
at assertPath (path.js:39:11)
at Object.relative (path.js:1173:5)
at Object.getCacheKey (node_modules/ts-jest/dist/utils/get-cache-key.js:15:16)
我很直觉,以前的解决方案对其他人有用的原因是因为他们使用了create-react-app
,然后在它旁边安装了一个相互矛盾的玩笑版本。如果是这种情况,则上述解决方案不适用于我的问题,因为我没有使用create-react-app
。
所以我重新安装了jest和@ types / jest,现在遇到了相同的初始问题...
这是我的webpack配置:
module.exports = {
entry: './src/index.tsx',
devServer: {
contentBase: __dirname + '/dist/',
},
module : {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: {
loader: 'ts-loader'
}
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
'style-loader',
{
loader: 'typings-for-css-modules-loader?modules?named',
options: {
modules: true,
namedExport: true
}
}
]
}
]
}
}
这是我的package.json:
{
"name": "practice-testing",
"private": true,
"version": "1.0.0",
"description": "",
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^23.3.1",
"@types/react": "^16.4.9",
"@types/react-dom": "^16.0.7",
"babel-core": "^6.26.3",
"babel-jest": "^23.4.2",
"css-loader": "^1.0.0",
"css-modules": "^0.3.0",
"jest": "^23.5.0",
"react-testing-library": "^5.0.0",
"style-loader": "^0.22.1",
"ts-jest": "^23.1.3",
"ts-loader": "^4.4.2",
"typescript": "^3.0.1",
"typings-for-css-modules-loader": "^1.7.0",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2"
},
"jest": {
"testEnvironment": "node"
}
}
这是我开玩笑的配置:
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
}
最后,这是我的tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"jsx": "react",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
},
"include": [
"./src/**/*",
],
"exclude": [
"node_modules",
]
}
答案 0 :(得分:14)
TLDR
此错误通常意味着在jest-environment-jsdom
的根目录中安装了jest-environment-node
和/或node_modules
,与用于运行Windows XP的Jest
的版本不兼容。测试。
详细信息
这很有趣。
问题是css-modules
。它具有对jest@20.0.4
的依赖关系(应该在其devDependencies下)。
jest@20.0.4
最后安装了jest-environment-jsdom@20.0.3
和jest-environment-node@20.0.3
,它们安装在node_modules
的根目录中。
jest@^23.5.0
已安装,它在jest-environment-jsdom@^23.4.0
内的多个位置安装jest-environment-node@^23.4.0
和node_modules/jest
,但自{{1 }}版本。
为node_modules
指定testEnvironment
时,解析过程将查找环境。 The first place it tries is within the project,在这种情况下将解析为20.0.3
版本。
早期版本的测试环境并未包含Jest
更高版本所要求的所有内容,包括20.0.3
的定义。
从Jest
删除teardown()
,删除css-modules
和/或package.json
和package-lock.json
并运行yarn.lock
,这应该清除所有内容。
(请注意,node_modules
only has 133 weekly downloads and no listed github site是我误将其添加为依赖项,它与CSS Modules不相关)
答案 1 :(得分:2)
以防万一其他人在使用纱线工作区的项目中偶然发现此问题,我通过not hoisting jest
解决了项目根目录中的相同问题:
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/jest/**",
"**/jest"
]
},