我在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也不需要匹配整个字符串,因此如果您希望基于一致的命名约定运行一个子集,则可以匹配公共部分。

其他回答

您还可以使用f或x来集中或排除一个测试。例如

fit('only this test will run', () => {
   expect(true).toBe(false);
});

it('this test will not run', () => {
   expect(true).toBe(false);
});

xit('this test will be ignored', () => {
   expect(true).toBe(false);
});

在检查了Jest CLI文档之后,我发现这就是我们在特定文件中运行特定测试的方式。

jest --findRelatedTests path/to/fileA.js path/to/fileB.js -t "test name"

纱,

yarn test --findRelatedTests path/to/fileA.js path/to/fileB.js -t "test name"

npm,

npm test -- --findRelatedTests path/to/fileA.js path/to/fileB.js -t "test name"

如需参考,请检查Jest Cli选项

现在有一个很好的Jest插件,叫做joke -watch-typeahead,它使这个过程更加简单。

我发现最快/最简单的方法是用

npx 是 TheNameOfYourFile

此外,您可以“订阅”以继续阅读更改

npx 是 TheNameOfYourFile --watchAll

如果你把jest作为脚本命令运行,比如npm test,你需要使用下面的命令让它工作:

npm test -- -t "fix order test"