我在fix-order-test.js文件中有一个测试“works with nested children”。
运行下面的代码将运行文件中的所有测试。
jest fix-order-test
如何只运行一个测试?下面的方法无法工作,因为它搜索指定的正则表达式的文件。
jest 'works with nested children'
我在fix-order-test.js文件中有一个测试“works with nested children”。
运行下面的代码将运行文件中的所有测试。
jest fix-order-test
如何只运行一个测试?下面的方法无法工作,因为它搜索指定的正则表达式的文件。
jest 'works with nested children'
当前回答
您还可以使用f或x来集中或排除一个测试。例如
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 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文档建议如下:
如果测试失败,首先要检查的事情之一应该是 当测试是唯一运行的测试时,测试是否失败。在开玩笑 只运行一个测试很简单——只是临时更改该测试 命令到test.only
test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
or
it.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
我发现最快/最简单的方法是用
npx 是 TheNameOfYourFile
此外,您可以“订阅”以继续阅读更改
npx 是 TheNameOfYourFile --watchAll
在命令行中,使用——testnameppattern或-t标志:
jest -t 'fix-order-test'
这将只运行与您提供的测试名称模式匹配的测试。它在Jest文档中。
另一种方法是在监视模式下运行测试,jest—watch,然后按P来过滤测试,输入测试文件名或T来运行单个测试名称。
如果你在描述块中有一个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/... 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.