Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Jasmine expectation, like xdescribe or xit?

On the Jasmine website I see that we can disable suites by xdescribe or individual specs by xit. Is there a way to disable only an expectation (like xexpect)?

The reason why I'm asking this is because I'm writing e2e tests with Protractor and in our continuous integration we don't yet (if ever) have access to the database, though locally we can run real end to end tests with access to the database, for example.

I would like to mark individual expectations as optional, depending on a configuration or environment variable. It would be nice to make a switch once, and then create a wrapper around expect, that only fails if we are running the tests locally (with access to the database).

So for example I can create a new spec family:

if (process.env.DB_AVAILABLE) {
  dbit = it;
} else {
  dbit = xit;
}

and write specs that depend on database connection as following:

dbit('creates new user', function () {});

Is there a way to do the same with expect (e.g. dbexpect)?

If there is something fundamentally wrong with my approach, don't hold it back and let me know.

like image 382
Vince Varga Avatar asked Jul 05 '16 16:07

Vince Varga


1 Answers

You could create your own xexpect by implementing all the methods/properties with an empty function:

var xexpect = function() {
  return xexpect;
};

Object.getOwnPropertyNames(jasmine.Expectation.prototype).forEach(function(name){
  xexpect[name] = xexpect;
});

Object.defineProperty(xexpect, 'not', {get: xexpect});

Usage :

xexpect(1).toBeGreaterThan(2);

xexpect(true).not.toEqual(true);
like image 136
Florent B. Avatar answered Sep 20 '22 17:09

Florent B.