Jestjs 如何使用Jest运行单个测试?
我在fix-order-test.js文件中有一个测试“与嵌套子项一起工作” 运行以下命令将运行文件中的所有测试Jestjs 如何使用Jest运行单个测试?,jestjs,Jestjs,我在fix-order-test.js文件中有一个测试“与嵌套子项一起工作” 运行以下命令将运行文件中的所有测试 jest fix-order-test 如何只运行一个测试?以下选项不起作用,因为它搜索指定正则表达式的文件 jest 'works with nested children' 建议如下: 如果测试失败,首先要检查的事情之一应该是 当测试是唯一运行的测试时,测试是否失败。开玩笑 只运行一个测试很简单-只需临时更改test 命令进行测试。仅限 或 在命令行中,使用--testNam
jest fix-order-test
如何只运行一个测试?以下选项不起作用,因为它搜索指定正则表达式的文件
jest 'works with nested children'
建议如下:
如果测试失败,首先要检查的事情之一应该是
当测试是唯一运行的测试时,测试是否失败。开玩笑
只运行一个测试很简单-只需临时更改test
命令进行测试。仅限
或
在命令行中,使用--testNamePattern
或-t
标志:
jest -t 'fix-order-test'
这将只运行与您提供的测试名称模式匹配的测试。在房间里
另一种方法是在监视模式下运行测试,jest--watch
,然后按p键通过键入测试文件名筛选测试,或按T键运行单个测试名
如果在descripe
块中有it
,则必须运行
jest -t '<describeString> <itString>'
jest -t '<describeString> <itString>'
jest-t''
如其他答案中所述,测试。仅
仅过滤掉同一文件中的其他测试。因此,其他文件中的测试仍将运行
因此,要运行单个测试,有两种方法:
- 选项1:如果您的测试名称是唯一的,您可以在监视模式下输入
t
,然后输入您想要运行的测试的名称
- 备选案文2:
- 在监视模式下点击
p
,为要运行的文件名输入正则表达式。(在监视模式下运行Jest时,会显示类似于此的相关命令)
- 将
it
更改为it。仅对要运行的测试执行
使用上述任一方法,Jest将只在指定的文件中运行单个测试。您还可以使用f
或x
来集中或排除测试。比如说
jest 'works with nested children'
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 -t 'fix-order-test'
node node_modules/jest/bin/jest.js -i test/math-tests.js -c test/tests-config.json -t "1 + 1 = 2"
如果在descripe
块中有it
,则必须运行
jest -t '<describeString> <itString>'
jest -t '<describeString> <itString>'
jest-t''
以下是我的观点:
笔记:
/node\u modules/.bin/..
是访问本地安装的Jest(或…)二进制文件(随本地安装的软件包一起提供)的一种极好的方式。(是的,在您的npm脚本中,您可以jest
之前什么都不做,但这在命令行上很方便…(这也是调试配置的良好开端,无论您使用哪种IDE…)
- 您的项目可能没有一组配置选项。但如果有(查看
package.json
中的脚本),这就是您所需要的
- –如前所述,我不知道您的配置,但如果您专注于开发/修复单个测试,您就不想与web工作者打交道
- 是的,您可以提供文件的完整、明确的路径
- 或者,您可以使用
-t
来不运行该文件中的所有测试,而只运行一个测试(此处:名称中带有“显示扩展的
”的测试)。通过将.only()
粘贴到该文件中也可以达到相同的效果
使用,您可以使用以下选项之一仅运行一个测试,对于测试套件也是如此
it.only('test 1', () => {})
test.only('test 1', () => {})
fit('test 1', () => {})
如果测试名称是唯一的,jest“test 1”
也可以工作。在Visual Studio代码中,这允许我只运行/调试一个jest测试,并带有断点:
我的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",
}
}
]
}
在文件package.json
中:
"scripts": {
"test": "jest"
}
- 要运行一个测试,请在该测试中,将
test
(或it
)更改为test.only
(或it.only
)。要运行一个测试套件(多个测试),请将descripe
更改为descripe.only
- 如果需要,请设置断点
- 在VisualStudio代码中,转到调试视图(Shift+Cmd+D或Shift+Ctrl+D)
- 从顶部的下拉菜单中,选择Jest Current File
- 单击绿色箭头以运行该测试
现在有了一个很好的方法,名为jest watch typeahead
它使这个过程更加简单。运行单个jest测试的完整命令
命令:
describe("math tests", () => {
it("1 + 1 = 2", () => {
expect(1 + 1).toBe(2);
});
it("-1 * -1 !== -1", () => {
expect(-1 * -1).not.toBe(-1);
});
});
"scripts": {
"test:math": "jest -i test/my-tests.js -t",
}
"scripts": {
"test": "jest"
}
{
"type": "node",
"request": "launch",
"name": "Run selected Jest test",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"test"
],
"args": [
"--",
"-i",
"${relativeFile}",
"-t",
"${selectedText}"
],
"console": "integratedTerminal",
}
node-i-c-t”“
:
- Windows:
node\u modules\jest\bin\jest.js
- 其他:
node\u modules/.bin/jest
-i
:带有测试的文件的路径(js
或ts
)
-c
:单独Jest配置文件(JSON)的路径,如果您将Jest配置保存在package.JSON
中,则无需指定此参数(Jest将在没有您帮助的情况下找到它)
-t
:实际上它是描述(…)
、它(…)
或测试(…)
块的名称(第一个参数)
示例:
describe("math tests", () => {
it("1 + 1 = 2", () => {
expect(1 + 1).toBe(2);
});
it("-1 * -1 !== -1", () => {
expect(-1 * -1).not.toBe(-1);
});
});
"scripts": {
"test:math": "jest -i test/my-tests.js -t",
}
"scripts": {
"test": "jest"
}
{
"type": "node",
"request": "launch",
"name": "Run selected Jest test",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"test"
],
"args": [
"--",
"-i",
"${relativeFile}",
"-t",
"${selectedText}"
],
"console": "integratedTerminal",
}
那么,命令呢
jest -t 'fix-order-test'
node node_modules/jest/bin/jest.js -i test/math-tests.js -c test/tests-config.json -t "1 + 1 = 2"
将测试它(“1+1=2”,…)
,但如果将-t
参数更改为“数学测试”
,则它将从描述(“数学测试”,…)
块运行这两个测试
备注:
describe("math tests", () => {
it("1 + 1 = 2", () => {
expect(1 + 1).toBe(2);
});
it("-1 * -1 !== -1", () => {
expect(-1 * -1).not.toBe(-1);
});
});
"scripts": {
"test:math": "jest -i test/my-tests.js -t",
}
"scripts": {
"test": "jest"
}
{
"type": "node",
"request": "launch",
"name": "Run selected Jest test",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"test"
],
"args": [
"--",
"-i",
"${relativeFile}",
"-t",
"${selectedText}"
],
"console": "integratedTerminal",
}
对于Windows,将node\u modules/.bin/jest
替换为node\u modules\jest\bin\jest.js
此方法允许您调试正在运行的脚本。若要启用,请在命令中添加'--inspect brk'
参数
通过“package.json”中的NPM脚本运行单个Jest测试
安装Jest后,您可以通过使用.In“package.json”
向“scripts”
部分添加新脚本来简化此命令(如上)的语法:
"scripts": {
"test:math": "jest -i test/my-tests.js -t \"math tests\"",
}
在这种情况下,我们使用别名'jest'
,而不是写入它的完整路径。此外,我们不指定配置文件路径,因为我们可以将它放在包中。