mjackson / expect

Write better assertions
MIT License
2.29k stars 117 forks source link

Document that the jest code mods won't cover all cases? #230

Open jharris4 opened 6 years ago

jharris4 commented 6 years ago

I'm trying out migrating from mocha & expect to just jest.

The code mods handled most of the cases, but there are some MAJOR differences in how Jest's isEqual matcher works, mostly because it does not use the is-equal package anymore.

This means things like this worked with expect but DO NOT work with jest:

expect(["abc"]).toEqual("abc");
function createEnhancedFunction() {
  var myFunction = function() { return "a value"; };
  myFunction.a = "123";
  return myFunction;
}

expect({ a: createEnhancedFunction() }).toEqual({ a: createEnhancedFunction() });

The first difference is easy to work around, but the second one (inability to compare functions and treat them as equal) has me a little stumped.

I'd think it would be good to emphasize that is-equal is no longer used for equality checks, and maybe suggest some workarounds in the docs?

ljharb commented 6 years ago

Issues with expect v21+ need to be filed in the jest repo: https://github.com/facebook/jest

jharris4 commented 6 years ago

@ljharb This issue was specific to helping people migrate to the new package FROM the old expect package.

As such, I was pointing out that people coming here looking to move there could use some extra pointers than what is currently in the README.

At the very least, it would be good to make it clear in the README that the codemods won't give you full feature compatibility with expect v21+

ljharb commented 6 years ago

Fair point; I'll reopen this - but I'd still file an issue in the jest repo about it.

jharris4 commented 6 years ago

Thanks, when I have a little more time, I hope to file a PR here to update the README.

I'm currently using the following workaround to make my old tests pass with jest, and yeah, I may well file an issue/PR with jest related to it once I've clarified my thinking a bit.

import isEqual from "is-equal";

var customMatchers = {
  toIsEqual: function() {
    return {
      compare: function(actual, expected) {
        return {
          pass: isEqual(actual, expected)
        };
      }
    };
  }
};

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

Basically, it lets you replace the old expect().toEqual with expect().toIsEqual which is a bit hacky but did the trick for me.