Skip to content

Instantly share code, notes, and snippets.

@xalakox
Created November 17, 2020 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xalakox/8253b1b11fbc5468aa89fb027bb7b6b9 to your computer and use it in GitHub Desktop.
Save xalakox/8253b1b11fbc5468aa89fb027bb7b6b9 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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