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

vij*_*yst 272 jestjs

我在文件fix-order-test.js中进行了"与嵌套子项一起工作"的测试.

运行以下命令会运行文件中的所有测试.

jest fix-order-test
Run Code Online (Sandbox Code Playgroud)

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

jest 'works with nested children'
Run Code Online (Sandbox Code Playgroud)

And*_*rle 315

从命令行使用--testNamePattern-t标志

jest -t 'fix-order-test'
Run Code Online (Sandbox Code Playgroud)

这将仅运行与您提供的测试名称模式匹配的测试.这是在Jest文档中.

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

  • 如果使用npm测试,你需要做`npm test - -t"fix order test"` (41认同)
  • 请注意,这是`it()`函数内特定测试名称的测试模式,而不是文件名.这就是我的想法. (11认同)
  • 这对我有用,但它也跳过了项目中的所有其他测试,这对于大型项目来说很慢。指定测试所在的特定测试文件确实有帮助:`./node_modules/.bin/jest --config =。/。jest.config.json ./src/x/y/sites.js/sitesWorker.test。 js -t'仅提供不正确的网站'` (2认同)
  • 不确定当时是否是这种情况,但是现在,如果您删除“-t”,它将仅运行您关心的测试,而不会跳过所有其余的测试。 (2认同)

fla*_*aky 83

jest文档建议如下:

如果测试失败,首先要检查的事情之一应该是测试是否失败,因为它是唯一运行的测试.在Jest中,只运行一个测试很简单 - 只需暂时将该test 命令更改为atest.only

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

  • 但这不起作用......开玩笑20.0.4 (26认同)
  • 适用于我的开玩笑20.0.4.虽然它只跳过该文件中的其余测试.除非您已将运行限制为一个文件,否则其他文件中的测试将继续运行. (6认同)
  • 这是一个开玩笑的事情 - 因为它以异步方式运行测试,它可能无法确定从一开始就在哪个文件中运行哪个测试.所以它将默认运行所有文件,并在文件中检查`test.only`.因此,如果您只想在一个包含许多文件的测试用例中包含许多测试用例的文件中运行一个测试,那么您不得不运行该单个文件`jest myTestFile.test.js` (6认同)
  • @johnslay好,感谢您在写回复之前阅读之前的评论我猜/ s :) (3认同)

Cor*_*use 43

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

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

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

选项2:

  1. p在监视模式下命中,输入您要运行的文件名的正则表达式.(在监视模式下运行Jest时会显示这样的相关命令).
  2. 更改itit.only您要运行的测试.

使用上述任一方法,Jest将仅在您指定的文件中运行单个测试.


Ser*_*gey 26

使用完整命令运行单个Jest测试

命令:

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

  • <path-to-jest>
    • 视窗: 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);
  });

});
Run Code Online (Sandbox Code Playgroud)

所以,命令

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/jestnode_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\"",
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我们使用别名'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",
}
Run Code Online (Sandbox Code Playgroud)

命令:

npm run test:math "math tests"

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

备注:

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

    运行任何生命周期脚本时,npm都会"./node_modules/.bin"PATH环境变量中输入第一个条目,因此即使您的程序未全局安装,它也可以正常工作(NPM blog

  2. 这种方法似乎不允许调试,因为Jest是通过其Binary / CLI运行的,而不是通过运行的node

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

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

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

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

package.json:

"scripts": {
  "test": "jest"
}
Run Code Online (Sandbox Code Playgroud)

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

启动配置:

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

它实际上与此答案前面所述的命令相同。现在一切都准备就绪,我们可以运行所需的任何测试,而不必手动重写命令参数。

这是您需要做的所有事情:

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

在VSCode调试面板中选择启动配置

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

选择测试名称

  1. 按下'F5'按钮。

瞧!

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

不幸的是,在Windows机器上它不会“瞧”,因为它们将${relativeFile}变量(由谁知道)替换为带有反斜杠的路径而Jest无法理解这种路径。

备注:

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

  • 优秀!这应该是公认的答案。特别是如果它添加了一个 `npx` 来大大简化调用 Jest,而不管操作系统如何。 (4认同)
  • 要跨平台,请在启动配置中使用“${fileBasename}”而不是“${relativeFile}”,因为 jest 无法解析带有反斜杠“\”的路径(Windows) (2认同)

Bab*_*med 23

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

npm run test -- -t 'Your test name'
Run Code Online (Sandbox Code Playgroud)

或者您可以做的另一种方法就是添加.only您的测试,如下所示并运行命令npm run test

it.only('Your test name', () => {})
Run Code Online (Sandbox Code Playgroud)


Nim*_*ush 19

您还可以使用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);
});
Run Code Online (Sandbox Code Playgroud)

  • `xit` 对我有用,但 `fit` 不行。我正在使用 jest@22.4.4。 (2认同)

Wla*_*cho 11

如上所述,您可以运行该命令

jest -t 'fix-order-test'
Run Code Online (Sandbox Code Playgroud)

如果你有it一个describe块的内部,你必须运行

jest -t '<describeString> <itString>'
Run Code Online (Sandbox Code Playgroud)


cod*_*ine 11

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

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

并观看

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


Ray*_*Gan 9

在 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",
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这在文件中package.json

  "scripts": {
    "test": "jest"
  }
Run Code Online (Sandbox Code Playgroud)
  • 要运行一项测试,请在该测试中将test(或it)更改为test.only(或it.only)。要运行一个测试套件(多个测试),请更改describedescribe.only.
  • 如果需要,设置断点。
  • 在Visual Studio代码,进入调试视图(Shift+ Cmd+DShift+ Ctrl+ D)。
  • 从顶部的下拉菜单中,选择Jest Current File
  • 单击绿色箭头以运行该测试。


sch*_*ode 8

使用最新的jest版本,您可以使用以下其中一项仅运行一个测试,测试套件也是如此。

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

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

fit('test 1', () => {})
Run Code Online (Sandbox Code Playgroud)

jest 'test 1' 如果测试名称是唯一的,也可能起作用。


ton*_*nes 7

用:

npm run test -- test-name
Run Code Online (Sandbox Code Playgroud)

这仅在您的测试规范名称唯一时才有效。

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


MiF*_*vil 7

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


Raj*_*med 7

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

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

it.only('only this test will run',() => {});
Run Code Online (Sandbox Code Playgroud)


Mug*_*ica 6

如果您jest以脚本命令运行,例如npm test,则需要使用以下命令使其运行:

npm test -- -t "fix order test"
Run Code Online (Sandbox Code Playgroud)

  • 如果你有很多测试文件,你也应该指定文件的名称。例如 `npm test -- testFile.js -t "fix order test"`。否则,它将遍历所有测试文件来查找匹配项,这会花费更长的时间。 (2认同)

And*_*eks 5

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

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

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


Ant*_*der 5

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

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

test("My Sum", () => {
  const sum = 3;
  expect(sum).toBe(3);
});
Run Code Online (Sandbox Code Playgroud)

使用测试名称在 CLI 上运行

jest -t Sum
Run Code Online (Sandbox Code Playgroud)

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

npm test -t my
Run Code Online (Sandbox Code Playgroud)