使用 mocha javascript 测试框架,我希望能够让多个测试(全部异步)仅在先前定义的测试通过后执行。
我不想将这些测试相互嵌套。
describe("BBController", function() {
it("should save", function(done) {});
it("should delete", function(done) {});
})
最佳答案
使用 --bail
选项。确保你至少使用 mocha 0.14.0。 (我用旧版本尝试过但没有成功。)
首先,mocha 无需执行任何操作即可在前一个测试完成后运行测试。这就是 mocha 默认的工作方式。将其保存到 test.js
:
describe("test", function () {
this.timeout(5 * 1000); // Tests time out in 5 seconds.
it("first", function (done) {
console.log("first: do nothing");
done();
});
it("second", function (done) {
console.log("second is executing");
// This test will take 2.5 seconds.
setTimeout(function () {
done();
}, 2.5 * 1000);
});
it("third", function (done) {
console.log("third is executing");
// This test will time out.
});
it("fourth", function (done) {
console.log("fourth: do nothing");
done();
});
});
然后执行:
mocha -R spec test.js
您将不会看到第四个测试开始,直到:
- 第一次和第二次测试完成。
- 第三次测试已超时。
现在,运行它:
mocha -R spec --bail test.js
一旦测试 3 失败,Mocha 将停止。
关于javascript - 如何仅在先前的异步测试通过后才运行 mocha 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244773/