Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha, using this.skip() to skip tests dynamically doesn't work

I'm trying to skip tests if a condition returns true using this.skip() inside an "it", but I'm getting an error "this.skip is not a function". this is the simple code I'm trying to check it on:

var async = require('async');
var should = require('chai').should();
var chai = require('chai'),
should = chai.should();
expect = chai.expect;
chai.use(require('chai-sorted'));

describe('Testing skip...\n', function() {
        this.timeout(1000);
        it('test1', (done) => {
            this.skip();
            console.log("1")
            done();
        });

        it('test2', (done) => {         
            console.log("2");
            done();
        }); 

});

I installed "[email protected] " since I saw it only works on since "mocha v3.0.0", but I still cant get it to work, and none of the past discussions on the subject seems to fix my problem.

like image 461
rone Avatar asked Dec 01 '22 10:12

rone


1 Answers

in order to use this in mocha, don't use arrow function. So, in your code you need to write it as

it('test1', function(done) { // use regular function here
  this.skip();
  console.log("1")
  done();
});

The best practice in Mocha is to discourage arrow function as described in https://mochajs.org/#arrow-functions

Hope it helps

like image 137
deerawan Avatar answered Dec 06 '22 19:12

deerawan