Created
August 1, 2017 21:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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