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

Run a single test file using Node js Mocha

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

中有以下内容
"scripts": {
  "test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000"}

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

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

任何人都可以指导我在这方面缺少什么。

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

"scripts": {
  "test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000 --grep"
}

然后调用它:

npm run test <name of test>