我从 Jest 单元测试开始。
在运行 Jest 单元测试时,我不断收到“未找到测试”。
错误描述
yarn test
yarn run v1.3.2
$ jest
No tests found
In E:\Course\Testing JavaScript\Jest Demo
4 files checked.
testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 3 matches
testPathIgnorePatterns: \\node_modules\\ - 4 matches
Pattern: - 0 matches
error Command failed with exit code 1.
操作系统:窗口 7,
节点 v:8.6,
npm v:5.4.2
folder structure :
Demo
package.json
__tests__
sum.js
sum.test.js
sum.js 文件:
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
const sum = require("./sum");
test("adds 1 + 2 to equal 3", () => {
expect(sum(1, 2)).toBe(3);
});
包.json
{
"name": "JestDemo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"jest": "^22.4.3"
},
"scripts": {
"test": "jest"
}
}
到目前为止,我已经引用了这些文章
Stackoverflow-Jest No Tests found
在 github 上还有一些关于相同问题的帖子,但没有任何帮助。
到目前为止我做了什么:
1)将脚本更改为指向包含测试文件的文件夹:
"scripts": {
"test": "jest __test__" //
from "test": "jest"
}
2)尝试更改文件夹结构。
有人可以帮我解决问题吗?
最佳答案
您似乎已将代码和测试文件放在同一目录中( __tests__/sum.js
和 __tests__/sum.test.js
)。我认为这不是您所看到的失败的具体原因,但它不是惯例。
除非您的项目已完成,否则不必指定要搜索的文件夹。运行jest
没有任何参数,除了必要的选项,它会搜索当前目录下面。
约定是:
/root
/src
sum.js
/__tests__
sum.js
或者:
/root
/src
sum.js
sum.test.js
您可以使用
test
或 spec
在后一个例子中。 root
和 src
由你决定,root
通常是 package.json
的位置并且是您从中调用 jest 的地方(例如 myApp
)。 src
帮助区分其他应用程序组件,例如构建和 Assets 。
关于javascript - 开 Jest 单元测试 - 未找到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584364/