我在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"

其他回答

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

以下是我的看法:

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

注:

./node_modules/.bin/... is a wonderful way, to access the locally installed Jest (or Mocha or...) binary that came with the locally installed package. (Yes, in your npm scripts you can jest with nothing before, but this is handy on command line... (that's also a good start for your debugging config, whichever IDE you are using...) Your project might not have a set of configuration options. But if it does (peek into the scripts in package.json), this is, what you need. --runInBand – as said, don't know about your configuration, but if you concentrate on developing/fixing a single test, you rather do not want to deal with web workers... Yes, you can give the whole, explicit path to your file Optionally, you can use -t to not run all tests in that file, but only a single one (here: the one, that has something with ‘show expanded’ in its name). Same effect can be achieved by glueing .only() into that file.

NPM test __tests__/filename.test。t -运行单个文件。

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

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

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

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

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

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

正如前面的回答所述,您可以运行该命令

jest -t 'fix-order-test'

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

jest -t '<describeString> <itString>'