如何使用Jest运行单个测试?

我在文件中有一个测试“与嵌套的孩子一起工作”fix-order-test.js.

运行下面的命令会运行文件中的所有测试。

jest fix-order-test

如何只运行单个测试?下面的命令在搜索指定的正则表达式文件时不起作用。

jest 'works with nested children'
605984 次浏览

Jest留档建议如下:

如果测试失败,首先要检查的事情之一应该是 当它是唯一运行的测试时,测试是否失败。在开玩笑 只运行一个测试很简单-只需暂时更改test 命令test.only

test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});

it.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});

在命令行中,使用--testNamePattern-t标志:

jest -t 'fix-order-test'

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

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


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

jest -t '<describeString> <itString>'

正如其他答案中提到的,test.only仅过滤掉同一文件中的其他测试。因此,其他文件中的测试仍将运行。

因此,要运行单个测试,有两种方法:

  • 选项1:如果您的测试名称是唯一的,您可以在监视模式下输入t并输入您要运行的测试名称。

  • 选项2:

    1. 在观看模式下点击p以输入要运行的文件名的正则表达式。(在观看模式下运行Jest时会显示类似的相关命令)。
    2. 在您要运行的测试中将it更改为it.only

使用上述任何一种方法,Jest只会在您指定的文件中运行单个测试。

您还可以使用fx来集中或排除测试

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 -t 'fix-order-test'

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

jest -t '<describeString> <itString>'

以下是我的看法:

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

备注:

  • ./node_modules/.bin/...是一种很好的方式,可以访问本地安装的包附带的本地安装的Jest(或摩卡或…)二进制文件。(是的,在您的npm脚本中,您可以在以前什么都没有的情况下jest,但这在命令行上很方便……(这对于您的调试配置来说也是一个很好的开始,无论您使用的是哪个IDE……)
  • 您的项目可能没有一组配置选项。但如果有(查看package.json中的脚本),这就是您需要的。
  • --runInBand-如上所述,不知道您的配置,但如果您专注于开发/修复单个测试,您宁愿不想与Web工作人员打交道…
  • 是的,您可以为您的文件提供完整的显式路径
  • 可选,您可以使用-t来运行该文件中的所有测试,而不是仅运行一个测试(此处:名称中带有show expanded的测试)。将.only()粘合到该文件中也可以达到相同的效果。

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

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


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


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

如果测试名称是唯一的,jest 'test 1'也可以工作。

在Visual Studio Code中,这只允许我运行/调试一个Jest测试,带断点:在Visual Studio Code调试测试

我的launch.json文件里面有这个:

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
]
}

在文件package.json中:

  "scripts": {
"test": "jest"
}
  • 要运行一个测试,在该测试中,将test(或it)更改为test.only(或it.only)。要运行一个测试套件(多个测试),请将describe更改为describe.only
  • 如果需要,请设置断点。
  • 在Visual Studio Code中,转到调试视图(Shift+Cmd+DShift+Ctrl+D)。
  • 从顶部的下拉框中,选择Jest当前文件
  • 单击绿色箭头以运行该测试。

现在有一个很好的Jest插件,称为jest-watch-typeahead,它使这个过程简单得多。

运行单个Jest测试的完整命令

命令:

node <path-to-jest> -i <your-test-file> -c <jest-config> -t "<test-block-name>"

  • <path-to-jest>
    • Windows:node_modules\jest\bin\jest.js
    • 其他:node_modules/.bin/jest
  • -i <you-test-file>:带有测试的文件路径(jsts
  • -c <jest-config>:单独的Jest配置文件(JSON)的路径,如果您将Jest配置保留在package.json中,则不必指定此参数(Jest将在没有您帮助的情况下找到它)
  • -t <the-name-of-test-block>:实际上它是describe(...)it(...)test(...)块的名称(第一个参数)。

示例:

describe("math tests", () => {


it("1 + 1 = 2", () => {
expect(1 + 1).toBe(2);
});


it("-1 * -1 !== -1", () => {
expect(-1 * -1).not.toBe(-1);
});


});

所以,指挥部

node node_modules/jest/bin/jest.js -i test/math-tests.js -c test/tests-config.json -t "1 + 1 = 2"

将测试it("1 + 1 = 2", ...),但如果将-t参数更改为"math tests",则它将从describe("math tests",...)块运行两个测试。

备注:

  1. 对于Windows,将node_modules/.bin/jest替换为node_modules\jest\bin\jest.js
  2. 此方法允许您调试正在运行的脚本。要启用调试,请在命令中添加'--inspect-brk'参数。

在“package.json”中通过NPM脚本运行单个Jest测试

安装Jest后,您可以使用NPM脚本简化此命令(上图)的语法。在"package.json"中,向"scripts"部分添加一个新脚本:

"scripts": {
"test:math": "jest -i test/my-tests.js -t \"math tests\"",
}

在这种情况下,我们使用别名'jest'而不是将完整路径写入它。此外,我们不指定配置文件路径,因为我们也可以将其放在"package.json"中,Jest将默认查看它。现在您可以运行命令:

npm run test:math

带有两个测试的"math tests"块将被执行。或者,当然,您可以通过其名称指定一个特定的测试。

另一种选择是将<the-name-of-test-block>参数拉到"test:math"脚本之外并从NPM命令传递:

package.json:

"scripts": {
"test:math": "jest -i test/my-tests.js -t",
}

命令:

npm run test:math "math tests"

现在,您可以使用更短的命令来管理运行测试的名称。

备注:

  1. 'jest'命令将与NPM脚本一起使用,因为

npm在运行任何生命周期脚本时使"./node_modules/.bin"成为PATH环境变量中的第一个条目,因此即使您的程序没有全局安装(NPM博客),这也可以正常工作 2.这种方法似乎不允许调试,因为Jest是通过它的二进制/CLI运行的,而不是通过node运行的。


在Visual Studio Code中运行选定的Jest测试

如果您使用的是Visual Studio Code,您可以利用它并通过按下F5按钮来运行当前选择的测试(在代码编辑器中)。为此,我们需要在".vscode/launch.json"文件中创建一个新的启动配置块。在该配置中,我们将使用预定义变量,它在运行时被适当的(不幸的是并不总是)值替换。在所有可用的测试中,我们只对这些感兴趣:

  • ${relativeFile}-相对于当前打开的文件 ${workspaceFolder}
  • ${selectedText}-活动文件中当前选定的文本

但是在编写启动配置之前,我们应该在我们的'package.json'中添加'test'脚本(如果我们还没有这样做的话)。

文件package.json:

"scripts": {
"test": "jest"
}

然后我们可以在我们的启动配置中使用它。

启动配置:

{
"type": "node",
"request": "launch",
"name": "Run selected Jest test",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"test"
],
"args": [
"--",
"-i",
"${relativeFile}",
"-t",
"${selectedText}"
],
"console": "integratedTerminal",
}

它实际上与本答案前面描述的命令相同。现在一切都准备好了,我们可以运行任何我们想要的测试,而无需手动重写命令参数。

这是所有你需要做的:

  1. 在调试面板中选择当前创建的启动配置:

    在Visual Studio Code调试面板中选择启动配置

  2. 在代码编辑器中打开带有测试的文件,然后选择要测试的测试名称(不带引号):

    选择测试名称

  3. F5按钮。

瞧!

现在要运行任何您想要的测试。只需在编辑器中打开它,选择其名称,然后按F5

不幸的是,它不会在Windows机器上“瞧”,因为它们用路径有反斜线的替换(谁知道为什么)${relativeFile}变量,而Jest不会理解这样的路径。 (如果命令需要故障排除,请参阅https://www.basefactor.com/using-visual-studio-code-to-debug-jest-based-unit-tests中的类似方法)

备注:

  1. 要在调试器下运行,请不要忘记添加'--inspect-brk'参数。
  2. 在这个配置示例中,我们没有Jest配置参数,假设它包含在'package.json'中。

如果您有jest作为脚本命令运行,例如npm test,则需要使用以下命令才能使其工作:

npm test -- -t "fix order test"

只是一个小插件,因为如果使用./node_modules/.bin/jest -i ...或只是jest -i ...npm test -- -i ...,似乎会有一种斗争

  1. 如果您在全局安装了它(与npm install -g jest一样),只需调用jest即可,这是一种处理依赖项的不太干净的方式
  2. 如果您只在包中本地安装了Jest,并且想要调用Jest脚本而不使用npm脚本绕道,您可以使用npx jest -i ...=>这正是npx的用途。它使您免于编写./node_modules/.bin/...

用途:

npm run test -- test-name

只有当您的测试规范名称是唯一的时,这才有效。

上面的代码将引用具有以下名称的文件:test-name.component.spec.ts

npm test __tests__/filename.test.ts-运行单个文件。

test.only('check single test', () => { expect(true).toBe(true)});-运行单个测试用例

test.skip('to skip testcase, () => {expect(false).toBe(false_});-跳过测试用例

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

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

所以文件名在-i之后,测试名在-t之后。

对于Windows中的VSCode,我在我的launch.json文件中使用了这些。请注意使用${路径分隔符}来处理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
},

对于VSCode,您可以使用jest-run-it扩展,它将帮助您从编辑器运行和调试Jest测试。输入图片描述

在jest 26.6.0上,这是唯一对我有用的东西:

jest -- test/unit/name-of-test-file.test.ts

并观看

jest --watch -- test/unit/name-of-test-file.test.ts

您可以尝试使用以下命令,因为它对我有用

npm run test -- -t 'Your test name'

或者你可以做的另一种方法是在你的测试中添加.only,如下所示,然后运行命令npm run test

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

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

您的测试类似于这个名为my.test.js的文件

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

使用测试名称在CLI上运行

jest -t Sum

使用npm测试与文件名的正则表达式匹配部分示例:my.test.js

npm test -t my

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

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


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

运行此命令行:

   npm run test-jest unit_test/file_name -- -t test_name

我的Package.json

"test-jest": "jest --verbose=true --force-exit",
"test-jest:watch": "jest --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选项

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

如果您的测试名称中没有特殊字符,那么它将按预期工作(使用itdescribe中的字符串或这些字符串的组合)。

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

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

可以使用jest -t "\/ \(GET\)"进行定位。

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