Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Extend toHaveFetched test output to show options diff #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tobilen
Copy link

@tobilen tobilen commented Mar 4, 2021

Asserting against a certain output, especially a body, is currently very cumbersome.

Scenario: I want to mock a POST request with a specific body, and i want to piece together the body in a test-driven manner.

The assertion only tells us that fetchMock was not called with the right url, which is not even technically correct. This is what it looks like if i pass a wrong body:
image

The only way to proceed now is to manually console.log the calls i've received with fetchMock.calls() and see where it goes wrong.

In this PR i've extended the output to:

  • json-parse the body (if possible)
  • use jest-diff to display the diff between the expected options object and the received options object
  • for all calls that match the filter

This is what the new output looks like:
image

@tobilen tobilen force-pushed the extend-body-parsing-response branch 3 times, most recently from 19021c8 to 6216808 Compare March 4, 2021 12:47
@tobilen
Copy link
Author

tobilen commented Mar 10, 2021

@wheresrhys anything missing from this PR?

@tobilen
Copy link
Author

tobilen commented May 17, 2021

still waiting on an update here :)

@tobilen
Copy link
Author

tobilen commented Jun 4, 2021

published under [email protected] for preview purposes

@tobilen tobilen force-pushed the extend-body-parsing-response branch from 6216808 to ce43621 Compare June 4, 2021 16:11
@tobilen
Copy link
Author

tobilen commented Aug 20, 2021

published new version with latest jest version under [email protected]

@petersendidit
Copy link

@wheresrhys Any thing we can do to help get this merged in? This would make dealing with assert failures much easier to understand.

@theghostbel
Copy link

Yeah, that's really a pain to distinguish between not-called API and called with a different request payload. Also have spent quite a lot of time just to figure out what's the cause for such an error report.

@tobilen
Copy link
Author

tobilen commented Jul 21, 2022

Updated the fork to newest version of jest, available under [email protected]

new functionality:
you're now able to use jest matches in the body you are expecting. for example:

fetch('http://example.com/path', {
	method: 'post',
	headers: {
		test: 'header',
	},
	body: {
		test: {
			value: "value"
		},
		otherValue: {
			value: "value"
		}
	}
})

it("matches when using jest expect matchers", () => {
	expect(fetch).toHavePosted('http://example.com/path', {
		method: 'post',
		headers: {
			test: 'header',
		},
		body: expect.objectContaining({
			test: {
				value: "value"
			}
		})
	});
})

@tobilen tobilen force-pushed the extend-body-parsing-response branch from e114ec5 to 728cd54 Compare July 21, 2022 15:49
@tobilen tobilen force-pushed the extend-body-parsing-response branch from 728cd54 to 87245ef Compare August 8, 2022 11:51
@macdonaldr93
Copy link

+1 I was about to start looking into writing this. So nice to see someone has already written the code. What do we need to do to get this merged?

"optional": true
}
}
"name": "fetch-mock-jest",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to see the diff on this. Any chance you could make the change without changing the tab?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants