【发布时间】:2019-01-19 10:52:23
【问题描述】:
当我尝试使用 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,然后在其旁边安装了一个有冲突的 jest 版本。如果是这样,那么上述解决方案不适用于我的问题,因为我没有使用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",
]
}
【问题讨论】:
标签: javascript node.js typescript jestjs