我能够使用Jest测试多个文件,但我不知道如何测试单个文件。

我有:

执行npm install jest-cli——save-dev命令 更新包。Json: '{…"scripts": {"test": "jest"}…} 编写了许多测试。

运行npm测试可以正常工作(目前运行14个测试)。

如何测试单个文件,例如test app/foo/__tests__/bar.spec.js?

我已经尝试运行npm test app/foo/__tests__/bar.spec.js(从项目根),但我得到以下错误:

npm犯错!错误:ENOENT,打开'/node_modules/app/foo/tests/bar.spec.js/package.json'


当前回答

运行单个测试:

npm test -t ValidationUtil # `ValidationUtil` is my module `ValidationUtil.spec.js`

-t -在后面,放置一个包含测试名称的正则表达式。

其他回答

至少从2019年开始:

NPM test——bar.spec.js

2015年:

为了运行特定的测试,您需要使用jest命令。NPM测试无效。要在命令行上直接访问jest,可以通过npm i -g jest-cli或yarn global add jest-cli安装它。

然后使用jest bar.spec.js运行特定的测试。

注意:您不必输入测试文件的完整路径。参数被解释为正则表达式。完整路径的任何部分都可以唯一地标识一个文件。

使用该命令:

npx jest test --runTestsByPath <path-to-file>

你可以使用npm test的文件名——

npm test -- fileName.jsx 

在特定文件中运行特定测试:

yarn test -f "partial-filename" -t "as split node"

npm测试,或笑话可以取代纱线测试,这取决于你喜欢的JS捆绑器。

这将只尝试在包含some-partial-filename的文件中查找测试,并且在这些文件中,测试将需要有一个describe或it指令,例如提到“作为分割节点”

// In cool-partial-filename.js
describe("with a less indented sibling", () => {
  it("should create a new check-list-item with the same indent as split node", () => {
    console.log("This would run with the invocation above");
  })
})

如果你用的是纱线,就这样做

yarn test nameofthefile

eg:

yarn test file.test.js