How to programmatically skip a test in mocha?

2019-01-21 15:34发布

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 test in mocha during the runtime execution?

标签: mocha
14条回答
够拽才男人
2楼-- · 2019-01-21 15:59

You can skip tests by placing an x in front of the describe or it block, or placing a .skip after it.

xit('should work', function (done) {});

describe.skip('features', function() {});

You can also run a single test by placing a .only on the test. for instance

describe('feature 1', function() {});
describe.only('feature 2', function() {});
describe('feature 3', function() {});

Only the feature 2 block would run in this case.

There doesn't appear to be a way to programmatically skip tests, but you could just do some sort of check in a beforeEach statement and only run the test if the flag was set.

beforeEach(function(){
    if (wrongEnvironment){
        runTest = false
    }
}

describe('feature', function(){
    if(runTest){
         it('should work', function(){
            // Test would not run or show up if runTest was false,
         }
    }
}
查看更多
Juvenile、少年°
3楼-- · 2019-01-21 16:01

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.

查看更多
唯我独甜
4楼-- · 2019-01-21 16:02

Say I wanted to skip my parametrized test if my test description contained the string "foo", I would do this:

// Skip parametrized test if description contains the string "foo"
(test.description.indexOf("foo") === -1 ? it : it.skip)("should test something", function (done) {
    // Code here
});

// Parametrized tests
describe("testFoo", function () {
        test({
            description: "foo" // This will skip
        });
        test({
            description: "bar" // This will be tested
        });
});

In your case, I believe that if you wanted to check environment variables, you could use NodeJS's:

process.env.ENV_VARIABLE

For example (Warning: I haven't tested this bit of code!), maybe something like this:

(process.env.NODE_ENV.indexOf("prod") === -1 ? it : it.skip)("should...", function(done) {
    // Code here
});

Where you can set ENV_VARIABLE to be whatever you are keying off of, and using that value, skip or run the test. (FYI the documentation for the NodeJS' process.env is here: https://nodejs.org/api/process.html#process_process_env)

I won't take complete credit for the first part of this solution, I found and tested the answer and it worked perfectly to skip tests based on a simple condition through this resource: https://github.com/mochajs/mocha/issues/591

Hope this helps! :)

查看更多
可以哭但决不认输i
5楼-- · 2019-01-21 16:03
mocha test/ --grep <pattern>

https://mochajs.org/

查看更多
ら.Afraid
6楼-- · 2019-01-21 16:05

to skip tests, use describe.skip or it.skip

describe('Array', function() {
  describe.skip('#indexOf()', function() {
    // ...
  });
});

to include tests you could use describe.only

describe('Array', function() {
  describe.only('#indexOf()', function() {
    // ...
  });
});

More info at https://mochajs.org/

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-21 16:06

This answer does work for ES6.

Instead of:

describe('your describe block', () => {

You want:

(condition ? describe : describe.skip)('your describe block', () => {

This conditionally skips all tests in the describe block IF the condition is false.

Or, instead of:

it('your it block', () => {

You want:

(condition ? it : it.skip)('your it block', () => {

This conditionally skips one test IF the condition is false.

查看更多
登录 后发表回答