如何在先前的异步测试通过后才运行mocha测试?

Tim*_*ind 8 javascript testing mocha.js

使用mocha javascript测试框架,我希望能够在先前定义的测试通过之后执行多个测试(所有异步).

我不想将这些测试嵌套在一起.

describe("BBController", function() {
    it("should save", function(done) {});
    it("should delete", function(done) {});
})
Run Code Online (Sandbox Code Playgroud)

kke*_*ple -6

如果您的测试设置正确,只测试一小部分业务逻辑,那么您可以异步运行测试,但它们不应该阻碍其他测试。完成测试的方法是执行以下操作:

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
    });
});
Run Code Online (Sandbox Code Playgroud)

再次强调,任何测试都不需要等待另一个测试运行才能通过,如果您遇到此问题,那么您应该寻找改进依赖项注入的方法或使用 before 方法准备测试