Releases: jefflau/jest-fetch-mock
Conditional Mocking, Request Based Responses, Enable/Disable Facade, and Abort Mocking
Fetches can now be mocked conditionally based upon URL or request matching. This gives the developer the capability to allow some requests to use the real fetch implementation and others to be mocked. See the Readme for detailed usage information.
The async functionality added with the last release has been enhanced. The async function that can be used to return a dynamic response now accepts an optional single API argument of "request" of type Request that can be used to vary the response returned from the mock fetch.
Enabling and disabling jest-fetch-mock can now be handled via the "enableMocks/disableMocks" exported functions instead of relying on custom code.
Request aborting is now supported by the mock framework. Both forced aborting via "mockAbort()" or "mockAbortOnce()" or aborting dynamically by passing a "signal" option to the "fetch" function and calling "abort" on the controller.
Thank you @yinzara for leading this one.
Merry Christmas all!
Jeff
Async version
Mocks can now take the normal Response body, or can optionally take a function that returns a promise. Basic usage will be setting timeouts to mock a slower connection or server.
Switched from isomorphic-fetch to cross-fetch and added tests
This changes the internals to cross-fetch
, which allows the use of blobs. isomorphic fetch
did not support this so we have now moved over to cross-fetch
.
In addition to that, this packages also has a lot of users now, so I've added a test suite to try and ensure that it doesn't break with any additional changes. Moving forward I will only allow PRs that write some additional tests to ensure additional functionality is work correctly and also doesn't break current tests.
Promise.finally polyfill added and new .ts types
Added support for Promise.finally and merged another PR for better Typescript types
Cleaned up docs and added `once` alias
Clearly I haven't done a release here on github as I didn't know what I was doing when I first started this package. This release is mainly documentation clean up. I've added a simple example without redux and I've also added another example showing some of the underlying mock capabilities of Jest. I've also added the once
alias for mockResponseOnce
. With mockResponseOnce
returning the mock function you can chain your responses and the syntax might be nicer than mockResponses
:
it('tests my api', () => {
const s = JSON.stringify
fetch
.once(s({ data: '12345'}))
.once(s({ data: '67890'}))
}
Other future plans will be to include more simple examples that do not require on redux as it seems to fallen out of favour recently!