Disable Jasmine expectation, like xdescribe or xit?
Asked Answered
A

1

9

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.

Automobile answered 5/7, 2016 at 16:40 Comment(4)
Not sure about prepending it to the expect statement -- but you could possibly use browser.getProcessedConfig() and check the seleniumAddress and do conditional expect's (assuming you have 2 configs, one for local and one for the CI server). if(local) { expect(true).toBe(true) } else { expect(false).toBe(false) } ... but this might be costly if you are doing it in a lot of places.Linkman
Also I never even thought about changing the it to a variable based on environment... thanks for that :)Linkman
Thank you. Yes, I thought about it, but was looking for a clean solutionAutomobile
We do not have such option in Jasmine, only way to do so is that either you have to maintain two different specs for each environment or use if conditions while writing spec based on environment variableShelves
C
4

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);
Cutaway answered 5/7, 2016 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.