Conditional Mocha Test

I use mocha for some integration testing and have many test suites. They all have their own initialization tests for each set. When such tests failed, the rest of the set should not be run at all, because all of them will not be sure. The fact is that I can’t avoid such an initialization test, because part of the code / environment is created by some tool that does not guarantee any correct result.

Is it possible to implement this with mocha?

+4
source share
2 answers

Using the BDD interface, the normal way to do this with mocha is to put something that sets up a test environment in beforeor beforeEach:

describe("foo", function () {
    describe("first", function () {
        before(function () {
            // Stuff to be performed before all tests in the current `describe`.
        });

        beforeEach(function () {
            // Stuff to perform once per test, before the test.
        });

        it("blah", ...
        // etc...
    });

    describe("second", function () {
        before(function () {
            // Stuff to be performed before all tests in the current `describe`.
        });

        beforeEach(function () {
            // Stuff to perform once per test, before the test.
        });

        it("blah", ...
        // etc...
    });
});

If the beforeor parameter beforeEach, which depends on the test, does not work, then the test does not start. Other tests that are independent of it will still be executed. Thus, in the above example, if the callback is transmitted beforein describethe name firstfails, in tests describeunder the name secondwill not be affected at all, and will work, provided that their own beforeand beforeEachdo not return.

In addition, Mocha is designed to run tests that are independent of each other. Therefore, if one itfails, the rest continue to work.

+1

mocha-steps, "" it() ( step()), , , , pull request 8 . :

describe("businessCode()", function() {
  step("should be not null", function() {
    assert(businessCode() != null)
  });
  step("should be a number", function() {
    assert(typeof businessCode() === 'number');
  });
  step("should be greater than 10", function() {
    assert(businessCode() > 10);
  });
  describe("thingThatCallsBusinessCode()", function() {
    step("should be greater than 10", function() {
      assert(thingThatCallsBusinessCode() != null);
    });
  });
});

, , businessCode() , should be a number; ( ).

+1

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


All Articles