我在文件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运行单个测试名称.
fla*_*aky 83
jest文档建议如下:
如果测试失败,首先要检查的事情之一应该是测试是否失败,因为它是唯一运行的测试.在Jest中,只运行一个测试很简单 - 只需暂时将该
test命令更改为atest.onlyRun Code Online (Sandbox Code Playgroud)test.only('this will be the only test that runs', () => { expect(true).toBe(false); });
Cor*_*use 43
如其他答案中所述,test.only仅过滤掉同一文件中的其他测试.因此,其他文件中的测试仍将运行.
因此,要运行单个测试,有两种方法:
选项1:如果您的测试名称是唯一的,您可以t在监视模式下输入并输入您要运行的测试名称.
选项2:
p在监视模式下命中,输入您要运行的文件名的正则表达式.(在监视模式下运行Jest时会显示这样的相关命令).it为it.only您要运行的测试.使用上述任一方法,Jest将仅在您指定的文件中运行单个测试.
Ser*_*gey 26
命令:
node <path-to-jest> -i <you-test-file> -c <jest-config> -t "<test-block-name>"
<path-to-jest>:
node_modules\jest\bin\jest.jsnode_modules/.bin/jest-i <you-test-file>:包含测试(js或ts)的文件的路径-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",...)块运行两个测试。
备注:
node_modules/.bin/jest为node_modules\jest\bin\jest.js。'--inspect-brk',请在命令中添加参数。安装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"
现在,您可以使用更短的命令来管理运行测试的名称。
备注:
'jest'命令将与NPM脚本一起使用,因为
运行任何生命周期脚本时,npm都会
"./node_modules/.bin"在PATH环境变量中输入第一个条目,因此即使您的程序未全局安装,它也可以正常工作(NPM blog)
node。如果您使用的是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)
它实际上与此答案前面所述的命令相同。现在一切都准备就绪,我们可以运行所需的任何测试,而不必手动重写命令参数。
这是您需要做的所有事情:
'F5'按钮。瞧!
现在,要运行任何测试,只需在编辑器中打开它,选择它的名称,然后按F5键。
不幸的是,在Windows机器上它不会“瞧”,因为它们将${relativeFile}变量(由谁知道)替换为带有反斜杠的路径,而Jest无法理解这种路径。
备注:
'--inspect-brk'参数。'package.json'。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
您还可以使用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);
});
Run Code Online (Sandbox Code Playgroud)
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
在 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)。要运行一个测试套件(多个测试),请更改describe为describe.only.使用最新的jest版本,您可以使用以下其中一项仅运行一个测试,测试套件也是如此。
it.only('test 1', () => {})
test.only('test 1', () => {})
fit('test 1', () => {})
Run Code Online (Sandbox Code Playgroud)
jest 'test 1' 如果测试名称是唯一的,也可能起作用。
用:
npm run test -- test-name
Run Code Online (Sandbox Code Playgroud)
这仅在您的测试规范名称唯一时才有效。
上面的代码将引用具有此名称的文件: test-name.component.spec.ts
在最新版本的 jest 中,您可以通过多种方式运行任何单个测试。
fit('only this test will run', () => {});
it.only('only this test will run',() => {});
Run Code Online (Sandbox Code Playgroud)
如果您jest以脚本命令运行,例如npm test,则需要使用以下命令使其运行:
npm test -- -t "fix order test"
Run Code Online (Sandbox Code Playgroud)
npm test __tests__/filename.test.ts - 运行单个文件。
test.only('check single test', () => { expect(true).toBe(true)}); - 运行单个测试用例
test.skip('to skip testcase, () => {expect(false).toBe(false_}); - 跳过测试用例
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)
| 归档时间: |
|
| 查看次数: |
148260 次 |
| 最近记录: |