[英]How do I tell Mocha to only run a certain test in the browser (and not in node.js)?
[英]How do I run a mocha test only after the prior asynchronous test has passed?
使用mocha javascript测试框架,我希望能够仅在先前定义的测试通过后才能执行多个测试(全部异步)。
我不想将这些测试相互嵌套。
describe("BBController", function() {
it("should save", function(done) {});
it("should delete", function(done) {});
})
使用--bail
选项。 确保您至少使用了摩卡咖啡0.14.0。 (我已经在较旧的版本中尝试过,但是没有成功。)
首先,仅在上一个测试完成后,您无需做任何事情就可以进行摩卡测试。 默认情况下,这就是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失败后,摩卡咖啡将立即停止。
如果您的测试设置正确,仅测试一小部分业务逻辑,则可以异步运行测试,但它们不应阻止其他测试。 完成测试的方法是执行以下操作:
describe("BBController", function() {
it("should save", function(done) {
// handle logic
// handle assertion or other test
done(); //let mocha know test is complete - results are added to test list
});
it("should delete", function(done) {
// handle logic
// handle assertion or other test
done(); //let mocha know test is complete - results are added to test list
});
});
再次,没有测试应该等待另一个测试通过,如果您遇到此问题,则应考虑改善依赖注入或使用before方法准备测试的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.