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

How do I run a single test using Jest?

我在文件 fix-order-test.js.

中有一个测试 'works with nested children'

运行 以下 运行 文件中的所有测试。

jest fix-order-test

我如何运行只进行一次测试?以下不起作用,因为它搜索指定正则表达式的文件。

jest 'works with nested children'

Jest documentation 推荐如下:

If a test is failing, one of the first things to check should be whether the test is failing when it's the only test that runs. In Jest it's simple to run only one test - just temporarily change that test command to a 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 documentation.

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


如果 describe 块中有 it,则必须 运行

jest -t '<describeString> <itString>'

如其他答案中所述,test.only 仅过滤掉同一文件中的其他测试。所以其他文件中的测试仍然会 运行.

所以要运行单测,有两种做法:

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

  • 选项 2:

    1. 在观看模式下点击 p 以输入您想要 运行 的文件名的正则表达式。 (在手表模式下运行开玩笑时会显示类似这样的相关命令)。
    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(或 Mocha 或...)二进制文件,该二进制文件随本地安装的软件包一起提供。 (是的,在你的 npm 脚本中你可以 jest 之前什么都没有,但这在命令行上很方便......(这也是你的调试配置的一个好的开始,无论你使用哪个 IDE .. .)
  • 您的项目可能没有一组配置选项。但如果确实如此(查看 package.json 中的脚本),这就是您所需要的。
  • --runInBand – 如前所述,不知道您的配置,但如果您专注于 developing/fixing 单个测试,您宁愿不想与网络工作者打交道...
  • 是的,您可以给出文件的完整、明确的路径
  • 可选,您可以使用 -t 不 运行 该文件中的所有测试,但只有一个(这里:那个,那个名称中带有“show expanded”的东西)。通过将 .only() 粘贴到该文件中可以实现相同的效果。

使用 latest Jest version,您可以使用以下方法之一来仅 运行 一个测试,同样适用于一个测试套件。

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

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

fit('test 1', () => {})
如果测试名称是唯一的,

jest 'test 1' 也可能有效。

在 Visual Studio 代码中,这让我 run/debug 只有一个带断点的 Jest 测试:Debugging tests in 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 代码中,转到调试视图 (Shift + Cmd + DShift + Ctrl + D).
  • 从顶部的下拉菜单中,选择 Jest 当前文件
  • 单击绿色箭头 运行 该测试。

现在有一个很好的 Jest plugin 叫做 jest-watch-typeahead,它使这个过程简单得多。

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

命令:

node <path-to-jest> -i <you-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. 此方法允许您调试 运行ning 脚本。要启用 debugging,请在命令中添加 '--inspect-brk' 参数。

运行 'package.json'

中通过 NPM 脚本进行的单个 Jest 测试

安装 Jest 后,您可以使用 NPM scripts 简化此命令(以上)的语法。在 "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 makes "./node_modules/.bin" the first entry in the PATH environment variable when running any lifecycle scripts, so this will work fine, even if your program is not globally installed (NPM blog) 2. This approach doesn't seem to allow debugging because Jest is run via its binary/CLI, not via node.


运行 selected Jest 测试 Visual Studio Code

如果您正在使用 Visual Studio 代码,您可以利用它和 运行 当前 selected 测试(在代码编辑器中),方法是按 F5按钮。为此,我们需要在 运行ning 时创建一个新的 launch configuration block in the ".vscode/launch.json" file. In that configuration, we will use predefined variables which are substituted with the appropriate (unfortunately not always) 值。在所有可用的中,我们只对这些感兴趣:

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

但在编写启动配置之前,我们应该在 '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. Select 当前在调试面板中创建启动配置:

  2. 在代码编辑器中打开包含测试的文件,select要测试的测试名称(不带引号):

  3. F5键。

瞧瞧!

现在 运行 任何你想要的测试。只需在编辑器中打开它,select它的名字,然后按F5

不幸的是,它不会在 Windows 机器上“瞧”,因为它们用路径 having reversed slashes 替换(谁知道为什么)${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. 如果您在全局安装了 jest,则只需调用 jest 即可(与 npm install -g 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 的人添加它:

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

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

对于 Windows 中的 VSCode,我在我的 launch.json 文件中使用了这些。请注意使用 ${pathSeparator} 来处理 Win 和 Mac 中的差异。 Select 调试下拉菜单中的一个,然后按 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 extension that will help you run and debug Jest tests from your editor.

在 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',() => {});