Jasmine toEqual for complex objects (mixed with functions)

pocesar picture pocesar · Jan 26, 2013 · Viewed 31.6k times · Source

Currently, I have a function that sometimes return an object with some functions inside. When using expect(...).toEqual({...}) it doesn't seem to match those complex objects. Objects having functions or the File class (from input type file), it just can't. How to overcome this?

Answer

Vlad Magdalin picture Vlad Magdalin · Jan 26, 2013

Try the Underscore _.isEqual() function:

expect(_.isEqual(obj1, obj2)).toEqual(true);

If that works, you could create a custom matcher:

this.addMatchers({
    toDeepEqual: function(expected) {
        return _.isEqual(this.actual, expected);
    };
});

You can then write specs like the following:

expect(some_obj).toDeepEqual(expected_obj);