fetch mocking framework
import fetchwrap from 'fetchwrap';
describe('test', () => {
it('should mock fetch', () {
const payload = { foo: 'bar' };
fetchwrap.mock('GET', '/foo', payload);
fetch('/foo')
.then(response => response.json())
.then(response => expect(response).toEqual(payload));
});
});
Registers a mock for a given method/pattern.
methodcan be any of 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH'patterncan exactly match a URL or can use a regular expression string, (e.g., "/foo*").responseis indicative of what you will be sent back as payload. It can take an integer status code or an object representing the response. If the object has astatusmember, that will be pulled out of the payload
If multiple mocks are registered for the same method/pattern, the mocks will be called in order of registration, with the final matching mock called for subsequent matches.
delete(pattern, [response]), get(pattern, [response]), head(pattern, [response]), options(pattern, [response]), patch(pattern, [response]), post(pattern, [response]), put(pattern, [response])
Convenience methods which call mock with the appropriate method
Clears all registered mocks and tracking information
Returns an array of unmatched patterns