Extend toHaveFetched test output to show options diff#32
Extend toHaveFetched test output to show options diff#32tobilen wants to merge 5 commits intowheresrhys:masterfrom
toHaveFetched test output to show options diff#32Conversation
19021c8 to
6216808
Compare
|
@wheresrhys anything missing from this PR? |
|
still waiting on an update here :) |
|
published under |
6216808 to
ce43621
Compare
|
published new version with latest jest version under |
|
@wheresrhys Any thing we can do to help get this merged in? This would make dealing with assert failures much easier to understand. |
|
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. |
|
Updated the fork to newest version of jest, available under new functionality: 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"
}
})
});
}) |
e114ec5 to
728cd54
Compare
728cd54 to
87245ef
Compare
|
+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", |
There was a problem hiding this comment.
It's hard to see the diff on this. Any chance you could make the change without changing the tab?
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:

The only way to proceed now is to manually
console.logthe calls i've received withfetchMock.calls()and see where it goes wrong.In this PR i've extended the output to:
jest-diffto display the diff between the expected options object and the received options objectThis is what the new output looks like:
