我在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'
当前回答
在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)。 从顶部的下拉菜单中,选择笑话当前文件。 单击绿色箭头以运行该测试。
其他回答
NPM test __tests__/filename.test。t -运行单个文件。
测试。only('check single test', () => {expect(true).toBe(true)});-运行单个测试用例
测试。skip('跳过testcase, () => {expect(false).toBe(false_});-跳过一个测试用例
您还可以使用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);
});
https://jestjs.io/docs/cli#--testnamepatternregex
你的测试是这个文件my。test。js
test("My Sum", () => {
const sum = 3;
expect(sum).toBe(3);
});
以测试名在CLI上运行
jest -t Sum
使用npm测试与regex匹配部分文件名的例子:my.test.js
npm test -t my
正如在其他答案中提到的,测试。仅过滤掉同一文件中的其他测试。因此其他文件中的测试仍将运行。
因此,要运行单个测试,有两种方法:
选项1:如果您的测试名称是唯一的,您可以在监视模式下输入t,并输入您想要运行的测试的名称。 选项2: 在监视模式下点击p,为您想运行的文件名输入一个正则表达式。(在监视模式下运行Jest时会显示类似的相关命令)。 换成它。只在您想要运行的测试中使用。
使用上述任何一种方法,Jest都将只运行您指定的文件中的单个测试。
在最新版本的jest中,您可以以多种方式运行任何单个测试。
fit('only this test will run', () => {});
it.only('only this test will run',() => {});