Skip to content

Instantly share code, notes, and snippets.

@yurist38
Created August 1, 2017 21:04
Show Gist options
  • Save yurist38/e9abae53fb8255d0f27a6592488f75a5 to your computer and use it in GitHub Desktop.
Save yurist38/e9abae53fb8255d0f27a6592488f75a5 to your computer and use it in GitHub Desktop.
Jest - test same objects and use expect.extend for logging detailed error message.
const firstArray = [{}, {}, {}];
const secondArray = [firstArray[0], {}, firstArray[2]];
test('Objects should be the same', () => {
expect.extend({
toBeSameObject(received, errMsg) {
const result = {
pass: received,
message: () => errMsg
};
return result;
}
});
firstArray.forEach((e, i) => {
const errMsg = `
Objects are different.
First value: ${JSON.stringify(e)}
Second value: ${JSON.stringify(secondArray[i])}
Index: ${i}
`;
expect(e === secondArray[i]).toBeSameObject(errMsg);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment