我在fix-order-test.js文件中有一个测试“works with nested children”。

运行下面的代码将运行文件中的所有测试。

jest fix-order-test

如何只运行一个测试?下面的方法无法工作,因为它搜索指定的正则表达式的文件。

jest 'works with nested children'

当前回答

Use:

npm run test -- test-name

只有当您的测试规范名称是唯一的时,这才会起作用。

上面的代码将引用一个名为test-name.component.spec.ts的文件

其他回答

以下是我的看法:

./node_modules/.bin/jest --config test/jest-unit-config.json --runInBand src/components/OpenForm/OpenForm.spec.js -t 'show expanded'

注:

./node_modules/.bin/... is a wonderful way, to access the locally installed Jest (or Mocha or...) binary that came with the locally installed package. (Yes, in your npm scripts you can jest with nothing before, but this is handy on command line... (that's also a good start for your debugging config, whichever IDE you are using...) Your project might not have a set of configuration options. But if it does (peek into the scripts in package.json), this is, what you need. --runInBand – as said, don't know about your configuration, but if you concentrate on developing/fixing a single test, you rather do not want to deal with web workers... Yes, you can give the whole, explicit path to your file Optionally, you can use -t to not run all tests in that file, but only a single one (here: the one, that has something with ‘show expanded’ in its name). Same effect can be achieved by glueing .only() into that file.

Use:

npm run test -- test-name

只有当您的测试规范名称是唯一的时,这才会起作用。

上面的代码将引用一个名为test-name.component.spec.ts的文件

如果有人试图使用joke -t '<testName>'并想知道为什么它不起作用,那么值得注意的是-t参数实际上是一个正则表达式模式,而不是字符串字面量。

如果您的测试名称中没有特殊字符,那么它就会像预期的那样工作(使用来自它的字符串或描述或它们的组合)。

如果您的测试名称确实有特殊字符,如括号,只需用反斜杠转义它们。例如,这样的测试:

it("/ (GET)", () => {
  return request(app.getHttpServer())
    .get("/health")
    .expect(200)
    .expect("Hello World");
});

可以用jest -t "\/ \(GET\)"作为目标。

regex也不需要匹配整个字符串,因此如果您希望基于一致的命名约定运行一个子集,则可以匹配公共部分。

对于VSCode,你可以使用joke -run-it扩展,它将帮助你从编辑器中运行和调试Jest测试。

在最新版本的jest中,您可以以多种方式运行任何单个测试。

fit('only this test will run', () => {});

it.only('only this test will run',() => {});