Jest: Add ability to add own matchers

Created on 10 Aug 2014  路  11Comments  路  Source: facebook/jest

With plain jasmine, it is possible to add own matchers using the jasmine.addMatchers() function. Jest does not expose this functionality. Would it be possible to implement this somehow?

Most helpful comment

This can be done already. In your package.json, add a jest configuration object and add the setupTestFrameworkScriptFile i.e.

"jest": {
  "setupTestFrameworkScriptFile": "<rootDir>/jest/customMatchers.js"
}

In this particular example, the customMatchers.js is located at project_root/jest/customMatchers.js.

In my customMatchers.js, I do the following:

// our own custom matchers
var matchers = {
  toHaveLength: function () {
    return {
      compare: function (actual, expected) {
        return {
          pass: actual.length === expected
        };
      }
    };
  }
};

jasmine.getEnv().beforeEach(function () {
  this.addMatchers(matchers);
});

That's all there is to it.

All 11 comments

This can be done already. In your package.json, add a jest configuration object and add the setupTestFrameworkScriptFile i.e.

"jest": {
  "setupTestFrameworkScriptFile": "<rootDir>/jest/customMatchers.js"
}

In this particular example, the customMatchers.js is located at project_root/jest/customMatchers.js.

In my customMatchers.js, I do the following:

// our own custom matchers
var matchers = {
  toHaveLength: function () {
    return {
      compare: function (actual, expected) {
        return {
          pass: actual.length === expected
        };
      }
    };
  }
};

jasmine.getEnv().beforeEach(function () {
  this.addMatchers(matchers);
});

That's all there is to it.

Thank you!

@gillesruppert That looks like the new syntax. Jest appears to have version 1.3 vendored.

I'm using something like this:

// Checks a react component's DOM node for matching text.
toMatchContent: function(expected) {
  return this.actual.getDOMNode().innerHTML.match(expected);
}

hey guys, when I try to use that I get an Object #<Object> has no method 'addMatchers', how I add matchers on the Jasmine 1.3?

there is any other way to that besides on a test setup file? I rather prefer load just the matchers that are relevant to a given file on that file instead of having to inject everything globally, there is a way to do that?

It seems like I have to add every package used in /jest/customMatchers.js to unmockedModulePathPatterns. Is there a way to avoid having to add each package one by one?

I'm trying to implement the matcher given in the last example in this page. That example uses clean, indent and inspect-react-element packages. Now I need to manually add each of those packages to 'unmockedModulePathPatterns'.

I'd recommend doing that, yes, and then setting up the matchers in your setup env file.

Can I do it programmatically? Or i must use the package.json?

fwiw this appears to have changed in recent versions: https://facebook.github.io/jest/docs/en/expect.html#expectextendmatchers
eg; in customMatchers.js, you would now do

expect.extend({
  toHaveLength(received, argument) {
    // ...
  }
})

(assuming you are not using jasmine)

Correct, this was added in https://github.com/facebook/jest/pull/1933, and is the way you're supposed to add matchers to jest (or just to expect, which works in any test test runner)

Was this page helpful?
0 / 5 - 0 ratings