如何通过 npm 运行 使用 Mocha?
How to use Mocha with npm run?
Objective
运行 Mocha 使用 npm 运行 进行测试,没有出现 ELIFECYCLE
错误。
背景
在我对 mocha 和 npm 进行 之后,我发现 Mocha return 结果是测试失败的次数。
现在,当 运行使用 npm(通过 npm run
)连接脚本时,脚本必须 return 0,否则 npm 将抛出 ELIFECYCLE
错误并且使脚本执行崩溃。
问题
这显然令人难以置信 愚蠢 麻烦,因为在开发代码时,测试应该会失败。这是正常的,我们不希望每次错过断言时代码都崩溃。
这几乎迫使我重新启动所有内容,令我震惊的是,在以前的 npm 版本中,这种崩溃行为并不存在。
目前我正在使用 npm 版本 3.10.3。
我试过的
阅读 npm error ELIFECYCLE while running the test 后,我看到了一种可能的解决方案。但是由于这个解决方案将抑制所有类型的错误(甚至是那些应该使应用程序崩溃的错误),我宁愿不实施它。
问题
- 我如何 运行 我在 Mocha 中的测试(知道某些 会 失败)而不会使整个应用程序因
ELIFECYCLE
错误而完全崩溃?
- 此问题是否已在较新版本的 npm 中得到解决? (什么是最新版本,因为我到处都找不到?)
如有任何帮助,我们将不胜感激。
您是否考虑过通过以下方式从您的代码中 运行 mocha:
const Mocha = require('mocha');
module.exports = () => {
// Instantiate a Mocha instance.
const mocha = new Mocha({
ui: 'bdd',
fullTrace: true,
});
const testDir = 'test/spec';
config.getGlobbedFiles(`${testDir}/**/*.spec.js`, './').forEach((filePath) => {
mocha.addFile(filePath);
});
// Run the tests.
mocha.run((failures) => {
process.exit(failures); // exit with non-zero status if there were failures
});
};
为了从 gulp 运行 它,你可以使用:
const minimist = require('minimist');
const knownOptions = {
string: 'params',
default: {
params: '',
},
};
const options = minimist(process.argv.slice(2), knownOptions);
...
gulp.task('test', () => {
if (process.env.NODE_ENV !== 'test') {
throw new Error('Wrong NODE_ENV value. Tests must be ran with "test"');
}
// eslint-disable-next-line import/no-dynamic-require, global-require
return require('test/test')(options.params); // where `test/test` is test runner above
});
我还没有对此进行测试,但我认为如果您将脚本更新为类似以下内容:
"test": "mocha || true"
它会在失败时 return 代码 0。
Objective
运行 Mocha 使用 npm 运行 进行测试,没有出现 ELIFECYCLE
错误。
背景
在我对 mocha 和 npm 进行
现在,当 运行使用 npm(通过 npm run
)连接脚本时,脚本必须 return 0,否则 npm 将抛出 ELIFECYCLE
错误并且使脚本执行崩溃。
问题
这显然令人难以置信 愚蠢 麻烦,因为在开发代码时,测试应该会失败。这是正常的,我们不希望每次错过断言时代码都崩溃。
这几乎迫使我重新启动所有内容,令我震惊的是,在以前的 npm 版本中,这种崩溃行为并不存在。
目前我正在使用 npm 版本 3.10.3。
我试过的
阅读 npm error ELIFECYCLE while running the test 后,我看到了一种可能的解决方案。但是由于这个解决方案将抑制所有类型的错误(甚至是那些应该使应用程序崩溃的错误),我宁愿不实施它。
问题
- 我如何 运行 我在 Mocha 中的测试(知道某些 会 失败)而不会使整个应用程序因
ELIFECYCLE
错误而完全崩溃? - 此问题是否已在较新版本的 npm 中得到解决? (什么是最新版本,因为我到处都找不到?)
如有任何帮助,我们将不胜感激。
您是否考虑过通过以下方式从您的代码中 运行 mocha:
const Mocha = require('mocha');
module.exports = () => {
// Instantiate a Mocha instance.
const mocha = new Mocha({
ui: 'bdd',
fullTrace: true,
});
const testDir = 'test/spec';
config.getGlobbedFiles(`${testDir}/**/*.spec.js`, './').forEach((filePath) => {
mocha.addFile(filePath);
});
// Run the tests.
mocha.run((failures) => {
process.exit(failures); // exit with non-zero status if there were failures
});
};
为了从 gulp 运行 它,你可以使用:
const minimist = require('minimist');
const knownOptions = {
string: 'params',
default: {
params: '',
},
};
const options = minimist(process.argv.slice(2), knownOptions);
...
gulp.task('test', () => {
if (process.env.NODE_ENV !== 'test') {
throw new Error('Wrong NODE_ENV value. Tests must be ran with "test"');
}
// eslint-disable-next-line import/no-dynamic-require, global-require
return require('test/test')(options.params); // where `test/test` is test runner above
});
我还没有对此进行测试,但我认为如果您将脚本更新为类似以下内容:
"test": "mocha || true"
它会在失败时 return 代码 0。