我在fix-order-test.js文件中有一个测试“works with nested children”。
运行下面的代码将运行文件中的所有测试。
jest fix-order-test
如何只运行一个测试?下面的方法无法工作,因为它搜索指定的正则表达式的文件。
jest 'works with nested children'
我在fix-order-test.js文件中有一个测试“works with nested children”。
运行下面的代码将运行文件中的所有测试。
jest fix-order-test
如何只运行一个测试?下面的方法无法工作,因为它搜索指定的正则表达式的文件。
jest 'works with nested children'
当前回答
如果有人试图使用joke -t '<testName>'并想知道为什么它不起作用,那么值得注意的是-t参数实际上是一个正则表达式模式,而不是字符串字面量。
如果您的测试名称中没有特殊字符,那么它就会像预期的那样工作(使用来自它的字符串或描述或它们的组合)。
如果您的测试名称确实有特殊字符,如括号,只需用反斜杠转义它们。例如,这样的测试:
it("/ (GET)", () => {
return request(app.getHttpServer())
.get("/health")
.expect(200)
.expect("Hello World");
});
可以用jest -t "\/ \(GET\)"作为目标。
regex也不需要匹配整个字符串,因此如果您希望基于一致的命名约定运行一个子集,则可以匹配公共部分。
其他回答
Jest文档建议如下:
如果测试失败,首先要检查的事情之一应该是 当测试是唯一运行的测试时,测试是否失败。在开玩笑 只运行一个测试很简单——只是临时更改该测试 命令到test.only
test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
or
it.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
在命令行中,使用——testnameppattern或-t标志:
jest -t 'fix-order-test'
这将只运行与您提供的测试名称模式匹配的测试。它在Jest文档中。
另一种方法是在监视模式下运行测试,jest—watch,然后按P来过滤测试,输入测试文件名或T来运行单个测试名称。
如果你在描述块中有一个it,你必须运行
jest -t '<describeString> <itString>'
在Visual Studio Code中,这让我只能运行/调试一个带有断点的Jest测试:在Visual Studio Code中调试测试
我的发射。Json文件里面有这个:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
]
}
在package.json文件中:
"scripts": {
"test": "jest"
}
要运行一个测试,在该测试中,将test(或it)更改为test。只有(或it.only)。若要运行一个测试套件(多个测试),请将describe更改为description .only。 如果需要,可以设置断点。 在Visual Studio Code中,转到调试视图(Shift + Cmd + D或Shift + Ctrl + D)。 从顶部的下拉菜单中,选择笑话当前文件。 单击绿色箭头以运行该测试。
正如在其他答案中提到的,测试。仅过滤掉同一文件中的其他测试。因此其他文件中的测试仍将运行。
因此,要运行单个测试,有两种方法:
选项1:如果您的测试名称是唯一的,您可以在监视模式下输入t,并输入您想要运行的测试的名称。 选项2: 在监视模式下点击p,为您想运行的文件名输入一个正则表达式。(在监视模式下运行Jest时会显示类似的相关命令)。 换成它。只在您想要运行的测试中使用。
使用上述任何一种方法,Jest都将只运行您指定的文件中的单个测试。
只是一个小插件,因为如果使用。/node_modules/.bin/jest -i…或者只是开玩笑-我…或者NPM测试-我…
如果你在全局安装了jest(如npm install -g jest),那么只调用jest就可以了,这是一种不太干净的处理依赖关系的方式 如果Jest只安装在包的本地,并且想调用Jest脚本而不需要绕道npm脚本,你可以使用npx Jest -i…=>这正是NPX的目的。它可以节省编写。/node_modules/.bin/....的时间