How to recover Jasmine spy name in custom matcher

227 views Asked by At

Im going to create a custom matcher in Jasmine 2.0 to verify spies against some additional conditions. In huge simplification, something like:

var customMatchers = {
  toDoSomething: function(util, customEqualityTesters) {
    return {
      compare: function(spy) {
        var comparison = {};
        comparison.pass = testSomeCondition(spy);
        if (!comparison.pass) {
          comparison.message = "Expect " + /insert code here/ + " to do something";
        }
        return comparison;
      }
    }
  }
};

beforeEach(function() {
    jasmine.addMatchers(customMatchers);
});

My question is, how to recover the spy name, passed as a first argument of factory method: createSpy(name, originalFn)?

I cannot find anything in Jasmine documentation v2.6 neither in online tutorials.

console.log(spy) returns function(...) {...}

1

There are 1 answers

0
mpasko256 On

I don't know if it is the right thing to do so, but found in Jasmine source code how toHaveBeenCalled was originally implemented and found:

spy.and.identity()

It works well also in a custom matcher.