How can I check if any mocha tests have passed because of the block?

describe('some tests', function () {
  /*
   * Run some tests...
   */
})

after(function () {
  failures = ? // <--- what goes here?
  console.log(failures + " tests failed!")
})

I would use this to keep the chrome browser open if the test failed, and report success or failure to bring the labs closer together .

Mocha Runner and Reporters have the information I'm looking for , like stats, but I'm not sure how to get to them from in the test file.

+4
source share
2 answers

After a quick check of the code, I believe that for the code in afterthere is no access to the video or reporters. However, there is a way to get the state of the tests at run time:

describe("blah", function () {

    it("blah", function () {
        throw new Error("blah");
    });

    after(function (){
        var failed = false;
        var tests = this.test.parent.tests;
        for(var i = 0, limit = tests.length; !failed && i < limit; ++i)
            failed = tests[i].state === "failed";
        if (failed)
            console.log("FAILED");
    });
});

var tests = this.test.parent.tests. , this.test , after. this.test.parent - , describe. this.test.parent.tests - . , , "failed".

, , ( ). , Mocha .

, , , , . , , ( has_failed):

var hook_failure = false;
function make_wrapper(f) {
    return function wrapper() {
        try {
            f.apply(this, arguments);
        }
        catch (e) {
            hook_failure = true;
            throw e;
        }
    };
}

function has_failed(it) {
    var failed = false;
    var tests = it.test.parent.tests;
    for(var i = 0, limit = tests.length; !failed && i < limit; ++i)
        failed = tests[i].state === "failed";
    return failed;
}

describe("blah", function () {

    before(make_wrapper(function () {
        throw new Error("yikes!");
    }));

    it("blah", function () {
        throw new Error("q");
    });

    after(function (){
        var failed = has_failed(this);
        if (failed)
            console.log(this.test.parent.title + " FAILED");
    });
});

after(function () {
    if (hook_failure)
        console.log("HOOK FAILURE");
});

after. , .

+3

afterEach(function() {
  if (this.currentTest.state == 'failed') {
    // ...
  }
});
+2

Source: https://habr.com/ru/post/1528885/


All Articles