Skip to content

Commit 8a8cbce

Browse files
authored
[Changed] Improve jest-diffs (#145)
* chore: improve jest-diffs * fix: run eslint * chore: build new package with node 10 * fix: move deps * fix: reinstall jest-diff * chore: fix import * fix: use right imports * fix: use right jest-diff dependency
1 parent 8a20454 commit 8a8cbce

4 files changed

Lines changed: 201 additions & 38 deletions

File tree

package-lock.json

Lines changed: 183 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"homepage": "https://github.com/mercadona/wrapito#readme",
3939
"dependencies": {
4040
"deep-equal": "^1.0.1",
41+
"jest-diff": "^29.7.0",
4142
"object-hash": "^2.1.1",
4243
"whatwg-fetch": "^3.5.0"
4344
},
@@ -66,8 +67,8 @@
6667
"typescript": "^4.3.3"
6768
},
6869
"peerDependencies": {
69-
"jest": "*",
7070
"chalk": "*",
71+
"jest": "*",
7172
"react": "*",
7273
"react-dom": "*"
7374
},

src/assertions/messages.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { green, red } from 'chalk'
1+
import { diff } from 'jest-diff'
22

33
const emptyErrorMessage = (path, options) => {
44
const message = options?.host
@@ -20,19 +20,21 @@ const fetchLengthErrorMessage = (path, expectLength, currentLength) => ({
2020
const methodDoesNotMatchErrorMessage = (expected, received) => ({
2121
pass: false,
2222
message: () =>
23-
`🌯 Wrapito: Fetch method does not match, expected ${expected} received ${received}`,
23+
`🌯 Wrapito: Fetch method does not match, expected ${expected} received ${received ?? 'none'}`,
2424
})
2525

26-
const bodyDoesNotMatchErrorMessage = (expected, received) => ({
27-
pass: false,
28-
message: () =>
29-
`🌯 Wrapito: Fetch body does not match.
30-
Expected:
31-
${green(JSON.stringify(expected, null, ' '))}
26+
const bodyDoesNotMatchErrorMessage = (expected, receivedBodies) => {
27+
const diffs = receivedBodies
28+
.map(received => diff(expected, received))
29+
.join('\n\n')
3230

33-
Received:
34-
${red(JSON.stringify(received, null, ' '))}`,
35-
})
31+
return {
32+
pass: false,
33+
message: () =>
34+
`🌯 Wrapito: Fetch body does not match.
35+
${diffs}`,
36+
}
37+
}
3638

3739
const doesNotHaveBodyErrorMessage = () => ({
3840
pass: false,

tests/assertions/toHaveBeenFetchedWith.test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertions } from '../../src'
2-
import { green, red } from 'chalk'
2+
import { diff } from 'jest-diff'
33

44
expect.extend(assertions)
55

@@ -25,7 +25,7 @@ describe('toHaveBeenFetchedWith', () => {
2525
await fetch(new Request(path))
2626
const { message } = await assertions.toHaveBeenFetchedWith(expectedPath)
2727

28-
expect(message()).toBe("🌯 Wrapito: /some/unknown ain't got called")
28+
expect(message()).toBe('🌯 Wrapito: /some/unknown ain\'t got called')
2929
expect(expectedPath).not.toHaveBeenFetchedWith()
3030
})
3131

@@ -152,11 +152,7 @@ describe('toHaveBeenFetchedWith', () => {
152152

153153
expect(message()).toBe(
154154
`🌯 Wrapito: Fetch body does not match.
155-
Expected:
156-
${green(JSON.stringify(expectedBody, null, ' '))}
157-
158-
Received:
159-
${red(JSON.stringify([receivedBody], null, ' '))}`,
155+
${diff(expectedBody, receivedBody)}`,
160156
)
161157
expect(path).not.toHaveBeenFetchedWith({
162158
body: expectedBody,

0 commit comments

Comments
 (0)