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

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

jest fix-order-test

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

jest 'works with nested children'

当前回答

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

npm test -- -t "fix order test"

其他回答

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

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

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

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

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

在Visual Studio Code中,这让我只能运行/调试一个带有断点的Jest测试:在Visual Studio Code中调试测试

我的发射。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。只有(或it.only)。若要运行一个测试套件(多个测试),请将describe更改为description .only。 如果需要,可以设置断点。 在Visual Studio Code中,转到调试视图(Shift + Cmd + D或Shift + Ctrl + D)。 从顶部的下拉菜单中,选择笑话当前文件。 单击绿色箭头以运行该测试。

Use:

npm run test -- test-name

只有当您的测试规范名称是唯一的时,这才会起作用。

上面的代码将引用一个名为test-name.component.spec.ts的文件

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

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

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

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

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

或者你可以像下面这样在测试中添加.only,然后运行npm run test命令

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