我能够使用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'


当前回答

“test”: “jest api/ds_server/ds_server.test.js”

其他回答

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

npm test -- fileName.jsx 

如果你正在运行npm >= 5.2.0,并且你已经在本地安装了一个带有npm i -d Jest的devDependencies,你可以通过执行npx Jest /path/to/your/spec.js在一个特定的文件上运行Jest。

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

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");
  })
})

简单的方法是运行单个单元测试文件,就是在根文件夹内运行终端中的命令。

npm test <fileName.test.js>

// For example
npm test utils.test.js

我也尝试了Jest Runner扩展的Visual Studio代码,它工作得很好。

如果你不想全局安装笑话,你可以使用

npx jest foo.test.js