Jasmine toEqual for complex objects (mixed with functions)

后端 未结 5 1842
鱼传尺愫
鱼传尺愫 2020-12-16 13:00

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 c

5条回答
  •  没有蜡笔的小新
    2020-12-16 13:32

    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);
    

提交回复
热议问题