with-fetch-mock v0.1.0
This is a wrapper around fetch-mock which allows to set up expectations for api calls more easily.
Features overview:
- Returns a promise which can used to block test from finishing early.
- Captures thrown errors to be used as a reason for test failure.
- Captures every request to assist in debugging.
- Request matching is performed through assertions.
- Supports async functions in scenario and mocks.
- Supports many shortcuts carried over from fetch-mock.
Usage
Import the wrapper:
const withMock = require('with-fetch-mock');withMock returns a promise and accepts following arguments:
scenariocallback which makes requests and verifies responses- one or more
mockwhich can be function or a value
Scenario can be an asyn
Promise returned by withMock will not fulfill until:
- All declared mocks have been called
- Promises returned by scenario and mock callbacks have been fulfilled
Mocks can be declared as one of the following forms:
numberwhich will be used as the status code of the response with empty bodystringwhich will be used as the body of the response with status code 200objectwith magical properties which will be used to create aResponseobject:bodyresponse bodystatusresponse status codeheadersresponse headersthrowsa rejection reason for the promise returned tofetchcallsendAsJsoncan be set to false in order to preventbodyfrom being forcefully converted tostring
objectwithout magical properties which will be converted tostringand used as the response bodyResponsewhich will be resolved to from thePromisereturned to the fetch callPromisewhich resolves to one of the values described abovefunctionwhich returns one of the values described above
Arguments supplied to fetch will be passed to the mock function:
inputrequest url (string)initrequest settings (object)methodthe request method (string)headersrequest headers (object)bodyrequest body (string,Blob, orFormData)- and other request settings...
Intended usage
Return the promise created by withMock to prevent your tests from finishing early and to ensure that meaningful failure messages will be produced in response to assertion errors inside the scenario or mock callbacks.
Any callback may return a promise or be declared as an async function. Resolution of the top level promise will be delayed until promise returned by callback is fulfilled. Callback promise rejection will be propagated to the top level promise.
Example
it('must make some requests', () => {
return withMock(
async () => {
const posts = await fetch('/posts/18')
.then(response => response.json());
expect(posts).toEqual({
title: 'Some random post',
});
const response = await fetch('/posts', {
method: 'POST',
body: JSON.stringify({
title: 'A work of art',
}),
});
expect(response.status).toBe(201);
},
(url) => {
expect(url).toEqual('/posts/18');
return { title: 'Some random post' };
},
(url, options) => {
expect(url).toEqual('/posts');
expect(options.method).toEqual('POST');
expect(options.body).toEqual(
JSON.stringify({
title: 'A work of art',
})
);
return 201;
}
)
});8 years ago