我在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'
当前回答
运行单个Jest测试的完整命令
命令:
节点<path-to-jest> -i <your-test-file> -c <jest-config> -t "<test-block-name>"
< path-to-jest >: Windows: node_modules \ \ bin \ jest.js开玩笑 其他:node_modules。bin /笑话 -i <you-test-file>:测试文件(js或ts)的路径 -c < Jest -config>:一个单独的Jest配置文件(JSON)的路径,如果你把你的Jest配置保存在包中。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);
});
});
那么,命令
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参数更改为"math tests",那么它将从describe("math tests",…)块中运行两个测试。
备注:
对于Windows,将node_modules/.bin/jest替换为node_modules\jest\bin\jest.js。 这种方法允许您调试正在运行的脚本。为了打开调试开关,在命令中添加参数——inspect-brk。
通过'package.json'中的NPM脚本运行单个Jest测试
安装了Jest之后,你可以使用NPM脚本简化这个命令的语法。在“包。添加一个新的脚本到“scripts”部分:
"scripts": {
"test:math": "jest -i test/my-tests.js -t \"math tests\"",
}
在本例中,我们使用别名“jest”,而不是写入它的完整路径。此外,我们不指定配置文件的路径,因为我们可以将它放在“package”中。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",
}
命令:
NPM运行测试:数学“数学测试”
现在您可以使用一个更短的命令来管理运行测试的名称。
备注:
'jest'命令将与NPM脚本一起工作,因为
当运行任何生命周期脚本时,npm将"./node_modules/.bin"作为PATH环境变量的第一个条目,所以这将很好地工作,即使你的程序没有全局安装(npm blog) 2. 这种方法似乎不允许调试,因为Jest是通过二进制/CLI运行的,而不是通过节点。
在Visual Studio Code中运行选定的Jest测试
如果您正在使用Visual Studio Code,您可以利用它并通过按F5按钮运行当前选定的测试(在代码编辑器中)。要做到这一点,我们需要在“.vscode/launch. conf”文件中创建一个新的启动配置块。json文件。在该配置中,我们将使用预定义的变量,这些变量在运行时被适当的(不幸的是并不总是)值所取代。在所有可获得的信息中,我们只对这些感兴趣:
${relativeFile} -当前打开的文件相对于 $ {workspaceFolder} ${selectedText} -活动文件中当前选中的文本
但是在编写启动配置之前,我们应该在我们的“包”中添加“test”脚本。Json '(如果我们还没有这样做的话)。
文件package.json:
"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",
}
它实际执行的操作与本回答中前面描述的命令相同。现在一切都准备好了,我们可以运行任何我们想要的测试,而不必手动重写命令参数。
以下是你需要做的:
在调试面板中选择当前创建的启动配置: 在代码编辑器中打开包含测试的文件,并选择要测试的测试的名称(不带引号): 按F5键。
瞧!
现在可以运行任何你想要的测试了。只需在编辑器中打开它,选择它的名称,然后按F5。
不幸的是,在Windows机器上它不会是“voilà”,因为它们将${relativeFile}变量替换为带有反斜杠的路径(谁知道为什么),Jest无法理解这样的路径。 (如果需要对命令进行故障处理,请参见https://www.basefactor.com/using-visual-studio-code-to-debug-jest-based-unit-tests中的类似方法)
备注:
要在调试器下运行,不要忘记添加'——inspect-brk'参数。 在这个配置示例中,我们没有Jest配置参数,假设它包含在'package.json'中。
其他回答
在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)。 从顶部的下拉菜单中,选择笑话当前文件。 单击绿色箭头以运行该测试。
对于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
},
在命令行中,使用——testnameppattern或-t标志:
jest -t 'fix-order-test'
这将只运行与您提供的测试名称模式匹配的测试。它在Jest文档中。
另一种方法是在监视模式下运行测试,jest—watch,然后按P来过滤测试,输入测试文件名或T来运行单个测试名称。
如果你在描述块中有一个it,你必须运行
jest -t '<describeString> <itString>'
只是一个小插件,因为如果使用。/node_modules/.bin/jest -i…或者只是开玩笑-我…或者NPM测试-我…
如果你在全局安装了jest(如npm install -g jest),那么只调用jest就可以了,这是一种不太干净的处理依赖关系的方式 如果Jest只安装在包的本地,并且想调用Jest脚本而不需要绕道npm脚本,你可以使用npx Jest -i…=>这正是NPX的目的。它可以节省编写。/node_modules/.bin/....的时间
正如前面的回答所述,您可以运行该命令
jest -t 'fix-order-test'
如果你在描述块中有一个it,你必须运行
jest -t '<describeString> <itString>'