How to programmatically skip a test in mocha?

前端 未结 14 1160
失恋的感觉
失恋的感觉 2020-12-07 17:31

I have a code where certain tests will always fail in CI environment. I would like to disable them based on an environment condition.

How to programmatically skip a

14条回答
  •  我在风中等你
    2020-12-07 17:44

    You can use my package mocha-assume to skip tests programmatically, but only from outside the tests. You use it like this:

    assuming(myAssumption).it("does someting nice", () => {});
    

    Mocha-assume will only run your test when myAssumption is true, otherwise it will skip it (using it.skip) with a nice message.

    Here's a more detailed example:

    describe("My Unit", () => {
        /* ...Tests that verify someAssuption is always true... */
    
        describe("when [someAssumption] holds...", () => {
            let someAssumption;
    
            beforeAll(() => {
                someAssumption = /* ...calculate assumption... */
            });
    
            assuming(someAssumption).it("Does something cool", () => {
                /* ...test something cool... */
            });
        });
    });
    

    Using it this way, you can avoid cascading failures. Say the test "Does something cool" would always fail when someAssumption does not hold - But this assumption was already tested above (in Tests that verify someAssuption is always true").

    So the test failure does not give you any new information. In fact, it is even a false-positive: The test did not fail because "something cool" did not work, but because a precondition for the test was not satisfied. with mocha-assume you can often avoid such false positives.

提交回复
热议问题