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

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

jest fix-order-test

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

jest 'works with nested children'

当前回答

我花了一段时间才找到这个,所以我想在这里为像我这样使用纱线的人添加它:

yarn test -i "src/components/folderX/folderY/.../Filename.ts" -t "name of test"

文件名在-i后面,testname在-t后面。

其他回答

您还可以使用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);
});

在命令行中,使用——testnameppattern或-t标志:

jest -t 'fix-order-test'

这将只运行与您提供的测试名称模式匹配的测试。它在Jest文档中。

另一种方法是在监视模式下运行测试,jest—watch,然后按P来过滤测试,输入测试文件名或T来运行单个测试名称。


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

jest -t '<describeString> <itString>'

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

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

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

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

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

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

npm test -- -t "fix order test"

对于Windows中的VSCode,我在我的启动中使用这些。json文件。注意使用${pathSeparator}来处理Win和Mac中的差异。在调试下拉菜单中选择一个并按F5运行。

 {
  "name": "Debug Selected Jest Test",
  "type": "node",
  "request": "launch",
  "runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand"],
  "args": ["--", "-i", "${fileDirnameBasename}${pathSeparator}${fileBasename} ", "-t", "${selectedText}"],
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "port": 9229
},
{
  "name": "Debug Named Jest Test",
  "type": "node",
  "request": "launch",
  "runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand"],
  "args": ["--", "-i", "${fileDirnameBasename}${pathSeparator}${fileBasename} ", "-t", "filename.test.js"],
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "port": 9229
},