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条回答
Explosion°爆炸
2楼-- · 2019-01-21 16:20

As @danielstjules answered here there is a way to skip test. @author of this topic has copied answer from github.com mochajs discussion, but there is no info in what version of mocha it available.

I'm using grunt-mocha-test module for integrating mocha test functionality in my project. Jumping to last (for now) version - 0.12.7 bring me mocha version 2.4.5 with implementation of this.skip().

So, in my package.json

  "devDependencies": {
    "grunt-mocha-test": "^0.12.7",
    ...

And then

npm install

And it make me happy with this hook:

describe('Feature', function() {

    before(function () {

        if (!Config.isFeaturePresent) {

            console.log('Feature not configured for that env, skipping...');
            this.skip();
        }
    });
...

    it('should return correct response on AB', function (done) {

        if (!Config.isABPresent) {

           return this.skip();
        }

        ...
查看更多
孤傲高冷的网名
3楼-- · 2019-01-21 16:21

There is an undocumented way of programmatically skipping tests:

$ cat test.js
describe('foo', function() {
  before(function() {
    this.skip();
  });

  it('foo', function() {
    // will not run
    console.log('This will not be printed');
  });
});
$ mocha test.js


  foo
    - foo


  0 passing (9ms)
  1 pending

This is discussed in https://github.com/mochajs/mocha/issues/1901.

查看更多
登录 后发表回答