Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Make adding new matchers easy #173

Closed
alexeypetrushin opened this Issue Jan 7, 2012 · 3 comments

Comments

Projects
None yet
3 participants

Hello, right now the proposed way for adding new matchers is this:

beforeEach(function() {
  this.addMatchers({
    toBeLessThan: function(expected) {
      return this.actual < expected;
    }
  });
});

It's not very convenient, because usually You define special matchers in a standalone helper file (not in spec file) and reuse it among many spec files.

But right now it's not easy to define matchers without the beforeEach block, for example code below wouldn't work:

jasmine.Matchers.prototype.toBeLessThan = function(expected) {
  return this.actual < expected;
};

You has also to do some extra magic, like this:

jasmine.Matchers.prototype.toBeLessThan = function(expected) {
  return this.actual < expected;
};
jasmine.Matchers.prototype.toBeLessThan = jasmine.Matchers.matcherFn_(
  'toBeLessThan', 
  jasmine.Matchers.prototype.toBeLessThan
);

So, maybe it's worth to somehow simplify this, add some helper method for example?, like jasmine.Mathcers.addMatchers that will do all this behind the scene?

Thanks.

Contributor

ragaskar commented Feb 14, 2012

This seems reasonable to me. We'll take a look at adding such a feature.

Contributor

infews commented May 27, 2013

We've moved in this direction a bit. If you look at the matchers_redo branch you will see improvement.

There is now a top-level function on the environment to addCustomMatchers(). It takes an object that has matcher functions as prroperties. The underlying implementation has changed a lot since 1.0, so look at matchers.js to see how this is done.

Custom matchers still are applied and removed per spec, so you need to add them in a beforeEach block.

We will be documenting the functionality a lot better for 2.0.

Thanks for the feedback.

Closing.

@infews infews closed this May 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment