我在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的文件

其他回答

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

npx 是 TheNameOfYourFile

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

npx 是 TheNameOfYourFile --watchAll

正如前面的回答所述,您可以运行该命令

jest -t 'fix-order-test'

如果你在描述块中有一个it,你必须运行

jest -t '<describeString> <itString>'

对于最新的Jest版本,您可以使用以下方法之一仅运行一个测试,对于测试套件也是如此。

it.only('test 1', () => {})

test.only('test 1', () => {})

fit('test 1', () => {})

如果测试名称是唯一的,开玩笑的“test 1”也可以工作。

https://jestjs.io/docs/cli#--testnamepatternregex

你的测试是这个文件my。test。js

test("My Sum", () => {
  const sum = 3;
  expect(sum).toBe(3);
});

以测试名在CLI上运行

jest -t Sum

使用npm测试与regex匹配部分文件名的例子:my.test.js

npm test -t my

以下是我的看法:

./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.