在我的项目文件夹中,我有很多包含js代码和test.js文件的子文件夹。我想要能够测试特定的文件。例如,假设在我们的项目文件夹中有'fib‘文件夹:
C:.
└───exercises
└───fib
fib-test.js
index.js现在,在习题文件夹中执行jest命令:
jest fib\fib-test.js我得到了:
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\exercises
62 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches
testPathIgnorePatterns: \\node_modules\\ - 62 matches
testRegex: - 0 matches
Pattern: fib\fib-test.js - 0 matches如果我只是开玩笑的话,我会把所有的测试都做出来。如果我将fib文件夹移出“练习”文件夹,它就会像预期的那样工作。以下是所有文件的代码:
index.js:
function fib(n) {}
module.exports = fib;test.js:
const fib = require('./index');
test('Fib function is defined', () => {
expect(typeof fib).toEqual('function');
});
test('calculates correct fib value for 1', () => {
expect(fib(1)).toEqual(1);
});
test('calculates correct fib value for 2', () => {
expect(fib(2)).toEqual(1);
});
test('calculates correct fib value for 3', () => {
expect(fib(3)).toEqual(2);
});
test('calculates correct fib value for 4', () => {
expect(fib(4)).toEqual(3);
});
test('calculates correct fib value for 15', () => {
expect(fib(39)).toEqual(63245986);
});package.json
{
"name": "dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"jest": {
"testEnvironment": "node"
},
"author": "",
"license": "ISC"
}我尝试过所有这些解决方案,但都没有成功:
但是能够达到预期的结果,运行jest命令,使用-- fib\test.js标志,然后在regex菜单中输入到该的相对路径。问题是如何在不输入“观看”菜单的情况下做到这一点?
发布于 2021-07-11 16:49:10
TL;博士:
将您的测试文件重命名为fib.test.js
jest fib.test.js
jest的jest XXX.test.js YYY.test.js测试您指定的only的任意数量的测试文件
长篇小说
我看到你的问题含蓄地有一些假设。如果明确列出这些问题,则可以使问题更加具体:
运行在Windows机器上的C:\exercises
jest.config.js位于
对于(1),我手头没有Windows机器,请运行并验证我的解决方案。
撇开假设不说,错误发生在您的测试文件名:fib-test.js。jest正在寻找XXX.test.js,并且不会匹配和测试XXX-test.js。您可以在错误消息中找到线索:
testMatch: ... **/?(*.)+(spec|test).[tj]s?(x) ...将文件重命名为fib.test.js后,jest fib.test.js将搜索项目根目录中的任何子文件夹或jest.config.js所在的位置;并匹配和测试特定的测试文件。
我的jest版本:"ts-jest": "^27.0.3"
小把戏#1
再次查看完整的错误消息:
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches实际上,您可以将所有测试用例分组到<rootDir>/__tests__/中,并将__test__放到.gitignore中,这样您的测试用例就可以保密,而不是被推到Git中。
小把戏#2
因为jest查看每个子文件夹,所以实际上可以将文件夹名放在测试文件之前:
jest fib/fib.test.js本质上等同于jest fib.test.js
不过,开玩笑说,看不出有什么理由这么麻烦自己。
https://stackoverflow.com/questions/63190593
复制相似问题