naugtur naugtur - 1 year ago 278
Javascript Question

Jasmine expect logic (expect A OR B)

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


I expected it to look like this:


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

Answer Source

Note: This solution contains syntax for versions prior to Jasmine v2.0. For more information on custom matchers now, see:

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


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;


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:

   toBeAnyOf: function(expecteds) {
      var result = false;
      for (var i = 0, l = expecteds.length; i < l; i++) {
        if (this.actual === expecteds[i]) {
          result = true;
      return result;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download