Łukasz Kazimierz Bandzarewicz

@lucassus

Jasmine Matchers Disassembled

Disassembled matchers for jasmine 1.1.0

Go to matcher: toBeTruthy() | toBeFalsy() | toBeDefined() | toBeUndefined() | toBeNull() | toEqual() | toContain() | toBeLessThan(), toBeGreaterThan() | toMatch() | toThrow()

toBeTruthy

Matcher that boolean not-nots the actual. Home

source link
1
2
3
jasmine.Matchers.prototype.toBeTruthy = function() {
  return !!this.actual;
};

toBeFalsy

Matcher that boolean nots the actual. Home

source link
1
2
3
jasmine.Matchers.prototype.toBeFalsy = function() {
  return !this.actual;
};

toBeDefined

Matcher that compares the actual to jasmine.undefined. Home

source link
1
2
3
jasmine.Matchers.prototype.toBeDefined = function() {
  return (this.actual !== jasmine.undefined);
};

toBeUndefined

Matcher that compares the actual to jasmine.undefined. Home

source link
1
2
3
jasmine.Matchers.prototype.toBeUndefined = function() {
  return (this.actual === jasmine.undefined);
};

toBeNull

Matcher that compares the actual to null. Home

source link
1
2
3
jasmine.Matchers.prototype.toBeNull = function() {
  return (this.actual === null);
};

toEqual

Compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. Home

toContain

Matcher that checks that the expected item is an element in the actual Array. Home

toBeLessThan, toBeGreaterThan

Home

source link
1
2
3
4
5
6
7
jasmine.Matchers.prototype.toBeLessThan = function(expected) {
  return this.actual < expected;
};

jasmine.Matchers.prototype.toBeGreaterThan = function(expected) {
  return this.actual > expected;
};

toMatch

Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes a pattern or a String. Home

source link
1
2
3
jasmine.Matchers.prototype.toMatch = function(expected) {
  return new RegExp(expected).test(this.actual);
};

toThrow

Matcher that checks that the expected exception was thrown by the actual. Home

source link
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
jasmine.Matchers.prototype.toThrow = function(expected) {
  var result = false;
  var exception;
  if (typeof this.actual != 'function') {
    throw new Error('Actual is not a function');
  }
  try {
    this.actual();
  } catch (e) {
    exception = e;
  }
  if (exception) {
    result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected));
  }

  var not = this.isNot ? "not " : "";

  this.message = function() {
    if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) {
      return ["Expected function " + not + "to throw", expected ? expected.message || expected : "an exception", ", but it threw", exception.message || exception].join(' ');
    } else {
      return "Expected function to throw an exception.";
    }
  };

  return result;
};

Comments