作者:倾城不了倾了你 | 来源:互联网 | 2022-12-11 15:29
我正在尝试使用Jest来测试我的节点API.我有一个简单的npm脚本调用jest
{
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest",
"precommit": "lint-staged"
},
"dependencies": {
"@koa/cors": "^2.2.1",
"apollo-link-http": "^1.5.4",
"apollo-server-koa": "^1.3.6",
"axios": "^0.18.0",
"dotenv": "^6.0.0",
"graphql": "^0.13.2",
"graphql-tools": "^3.0.4",
"koa": "^2.5.1",
"koa-bodyparser": "^4.2.1",
"koa-router": "^7.4.0",
"node-fetch": "^2.1.2"
},
"devDependencies": {
"husky": "^0.14.3",
"jest": "^23.3.0",
"lint-staged": "^7.2.0",
"nodemon": "^1.17.5",
"prettier": "^1.13.7"
},
"lint-staged": {
"gitDir": "./",
"*.{js,json}": [
"prettier --no-semi --print-width 140 --tab-width 2 --write",
"git add"
]
},
"jest": {
"bail": true,
"verbose": true,
"testMatch": [
"/**/*.test.js"
]
}
}
我在我的jest配置中package.json
告诉jest要测试什么:
"jest": {
"bail": true,
"verbose": true,
"testMatch": [
"/**/*.test.js"
]
}
在我的项目中,我根本没有使用babel,但出于某种原因,当我尝试运行我的一个测试时 ./test.test.js
test("Example test", () => {
expect(true).toBe(true)
})
我收到以下错误:
$ jest
FAIL ./test.test.js
? Test suite failed to run
Couldn't find preset "env" relative to directory "/Users//Projects"
at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map ()
at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 7.563s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
我尝试添加一个babel env预设但仍然得到相同的错误.我认为我不应该只为测试添加一个babel配置.
我正在运行节点版本8.11.3
和纱线,1.7.0
并使用版本在项目中本地安装了jest^23.2.0
我怎么告诉开玩笑不要担心babel env?
1> 小智..:
babel-jest
安装Jest时会自动添加.如果你不打算转换ES6代码(看起来像你的情况)可能只是重置配置:
// package.json
{
"jest": {
"transform": {}
}
}