使用 Node js Mocha 运行单个测试文件

Sin*_*r M 5 unit-testing mocha.js node.js

我的所有测试都在测试文件夹下,当我给出所有测试时npm test,所有测试都会被执行。现在我尝试按文件或按描述运行我的测试脚本。我的 package.json 中有以下内容

"scripts": {
  "test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000"}
Run Code Online (Sandbox Code Playgroud)

当我给出npm test --grep 'filename / describe / it'它时,它没有获取给定的输入,而是收到以下警告Warning: Cannot find any files matching pattern "given filename / describe /it",即使给定的输入匹配。

我的文件名模式是filename.test.js,文件夹结构就像test\***\filename.test.js

谁能指导我我在这方面缺少什么。

eol*_*eol 9

您需要在命令grep中包含该选项npm:

"scripts": {
  "test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000 --grep"
}
Run Code Online (Sandbox Code Playgroud)

然后用以下方式调用它:

npm run test <name of test>
Run Code Online (Sandbox Code Playgroud)