Skip to content

Commit

Permalink
[Changed] Improve jest-diffs (#145)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arturosdg authored May 9, 2024
1 parent 8a20454 commit 8a8cbce
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 38 deletions.
202 changes: 183 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"homepage": "https://github.com/mercadona/wrapito#readme",
"dependencies": {
"deep-equal": "^1.0.1",
"jest-diff": "^29.7.0",
"object-hash": "^2.1.1",
"whatwg-fetch": "^3.5.0"
},
Expand Down Expand Up @@ -66,8 +67,8 @@
"typescript": "^4.3.3"
},
"peerDependencies": {
"jest": "*",
"chalk": "*",
"jest": "*",
"react": "*",
"react-dom": "*"
},
Expand Down
24 changes: 13 additions & 11 deletions src/assertions/messages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { green, red } from 'chalk'
import { diff } from 'jest-diff'

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

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

Received:
${red(JSON.stringify(received, null, ' '))}`,
})
return {
pass: false,
message: () =>
`🌯 Wrapito: Fetch body does not match.
${diffs}`,
}
}

const doesNotHaveBodyErrorMessage = () => ({
pass: false,
Expand Down
10 changes: 3 additions & 7 deletions tests/assertions/toHaveBeenFetchedWith.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertions } from '../../src'
import { green, red } from 'chalk'
import { diff } from 'jest-diff'

expect.extend(assertions)

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

expect(message()).toBe("🌯 Wrapito: /some/unknown ain't got called")
expect(message()).toBe('🌯 Wrapito: /some/unknown ain\'t got called')
expect(expectedPath).not.toHaveBeenFetchedWith()
})

Expand Down Expand Up @@ -152,11 +152,7 @@ describe('toHaveBeenFetchedWith', () => {

expect(message()).toBe(
`🌯 Wrapito: Fetch body does not match.
Expected:
${green(JSON.stringify(expectedBody, null, ' '))}
Received:
${red(JSON.stringify([receivedBody], null, ' '))}`,
${diff(expectedBody, receivedBody)}`,
)
expect(path).not.toHaveBeenFetchedWith({
body: expectedBody,
Expand Down

0 comments on commit 8a8cbce

Please sign in to comment.