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

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

jest fix-order-test

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

jest 'works with nested children'

当前回答

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

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

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

其他回答

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

对于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
},

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

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

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

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

npx 是 TheNameOfYourFile

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

npx 是 TheNameOfYourFile --watchAll

在检查了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选项