Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const { equals } = require('expect/build/jasmineUtils');
expect.extend({
toContainObject(received, argument) {
const pass = (() => {
if (Array.isArray(argument)) {
return equals(received,
expect.arrayContaining(argument.map(arg => expect.objectContaining(arg)))
);
}
return equals(received,
expect.arrayContaining([
expect.objectContaining(argument)
])
);
})();
if (pass) {
return {
message: () => (`expected ${this.utils.printReceived(received)} not to contain object ${this.utils.printExpected(argument)}`),
pass: true
}
} else {
return {
message: () => (`expected ${this.utils.printReceived(received)} to contain object ${this.utils.printExpected(argument)}`),
pass: false
}
}
}
});
@xalakox

This comment has been minimized.

Copy link
Owner Author

@xalakox xalakox commented Nov 17, 2020

example usage on a first level and nested object: (should pass)

expect([{
  foo: 'bar',
  prop1: 'what',
  prop2: [{
    xyz: 'asd',
    bar: 'foo',
  }],
}]).toContainObject([{
            foo: 'bar'
            prop2: expect.toContainObject({
                bar: 'foo',
            }),
        },{
          foo: 'bar'
            prop2: expect.toContainObject({
                xyz: 'asd',
            }),
        }]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment