将 mocha 设置为 运行 个不同的文件夹测试

Set up mocha to run different folder tests

我的文件夹结构如下:

package.json 具有以下配置:

{
"scripts": {
    "test": "mocha ./**/*-tests.js"
},
"devDependencies": {
    "axios": "^0.19.2",
    "mocha": "^7.1.1",
    "should": "^13.2.3",
    "should-sinon": "0.0.6",
    "sinon": "^9.0.1"
}

}

现在我可以从命令行执行 "npm test" 和所有测试、集成和单元,将 运行。

我希望能够传递一个参数,比如"npm test --folder unit_tests",所以同一个下只有一组测试文件夹将 运行.

您可以在 package.json:

中为集成和单元测试定义单独的测试-scripts
"scripts": {
    "test:unit": "mocha path/to/unit-test-folder/*-tests.js",
    "test:integration": "mocha path/to/integration-test-folder/*-tests.js"
}

然后您可以独立调用它们:

npm run test:unit
npm run test:integration