Jasmine expect logic (expect A OR B)

21,858

Solution 1

Note: This solution contains syntax for versions prior to Jasmine v2.0. For more information on custom matchers now, see: https://jasmine.github.io/2.0/custom_matcher.html


Matchers.js works with a single 'result modifier' only - not:

core/Spec.js:

jasmine.Spec.prototype.expect = function(actual) {
  var positive = new (this.getMatchersClass_())(this.env, actual, this);
  positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
  return positive;

core/Matchers.js:

jasmine.Matchers = function(env, actual, spec, opt_isNot) {
  ...
  this.isNot = opt_isNot || false;
}
...
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
  return function() {
    ...
    if (this.isNot) {
      result = !result;
    }
  }
}

So it looks like you indeed need to write your own matcher (from within a before or it bloc for correct this). For example:

this.addMatchers({
   toBeAnyOf: function(expecteds) {
      var result = false;
      for (var i = 0, l = expecteds.length; i < l; i++) {
        if (this.actual === expecteds[i]) {
          result = true;
          break;
        }
      }
      return result;
   }
});

Solution 2

Add multiple comparable strings into an array and then compare. Reverse the order of comparison.

expect(["New", "In Progress"]).toContain(Status);

Solution 3

This is an old question, but in case anyone is still looking I have another answer.

How about building the logical OR expression and just expecting that? Like this:

var argIsANumber = !isNaN(mySpy.mostRecentCall.args[0]);
var argIsBooleanFalse = (mySpy.mostRecentCall.args[0] === false);

expect( argIsANumber || argIsBooleanFalse ).toBe(true);

This way, you can explicitly test/expect the OR condition, and you just need to use Jasmine to test for a Boolean match/mismatch. Will work in Jasmine 1 or Jasmine 2 :)

Share:
21,858

Related videos on Youtube

naugtur
Author by

naugtur

I am a JavaScript developer with experience in building unusual browser applications, complicated live data visualization, creating tools for developers and building back-ends in node.js. Senior JS developer, R&amp;D and speaker. Open Source enthusiast Started using jQuery about the year 2007, early adopter of jQuery Mobile Framework (started at the alpha2 version, first jquery-mobile silver badge on stackoverflow) A fan of Browseify http://naugtur.pl https://github.com/naugtur

Updated on April 08, 2020

Comments

  • naugtur
    naugtur about 4 years

    I need to set the test to succeed if one of the two expectations is met:

    expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number));
    expect(mySpy.mostRecentCall.args[0]).toEqual(false);
    

    I expected it to look like this:

    expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)).or.toEqual(false);
    

    Is there anything I missed in the docs or do I have to write my own matcher?

    • Magus
      Magus over 11 years
      I checked the documentation and i tried some code, but i think we can't do that "easily" sorry. But according to what i have found, you can create your own "chainable" matcher.
  • naugtur
    naugtur over 11 years
    All I needed was 'there are no multi matches', this answer is so much more! :) Thanks for the code. I don't think it handles the jasmine.any(Number) case, but I can work it out. I'll put it in your answer once figured out if you don't mind.
  • Jessica Knight
    Jessica Knight over 6 years
    Great idea, very succinct! And gives much clearer error messages than expect( Status == "New" || Status == "In Progress").toBe(true);
  • Yann Chabot
    Yann Chabot over 4 years
    Did it for me nice one
  • Pankaj
    Pankaj over 4 years
    Wondering why is this not the selected answer? This is the simplest solution to test one of the two expectations.
  • Campalo
    Campalo over 3 years
    Smart idea and easy to read implementation. Works great!