https://github.com/msn0/fake-fetch
fake window.fetch for tests
https://github.com/msn0/fake-fetch
fetch fetch-api
Last synced: 9 months ago
JSON representation
fake window.fetch for tests
- Host: GitHub
- URL: https://github.com/msn0/fake-fetch
- Owner: msn0
- Created: 2015-06-10T11:06:43.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T08:19:27.000Z (over 3 years ago)
- Last Synced: 2025-08-25T21:51:37.501Z (9 months ago)
- Topics: fetch, fetch-api
- Language: JavaScript
- Size: 1.28 MB
- Stars: 19
- Watchers: 1
- Forks: 2
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fake-fetch [](http://travis-ci.org/msn0/fake-fetch)
> fake window.fetch for tests.
## Installation
```sh
$ npm install fake-fetch --save-dev
```
## Usage
```js
var fakeFetch = require('fake-fetch');
beforeEach(fakeFetch.install);
afterEach(fakeFetch.restore);
it("should fetch what you need", done => {
fakeFetch.respondWith({"foo": "bar"});
fetch('/my-service', {headers: new Headers({accept: 'application/json'})}).then(data => {
expect(fakeFetch.getUrl()).toEqual('/my-service');
expect(fakeFetch.getMethod()).toEqual('get');
expect(data._bodyText).toEqual('{"foo":"bar"}');
expect(fakeFetch.getRequestHeaders()).toEqual(new Headers({accept: 'application/json'}));
done();
});
});
```
### API
#### install
Mock `window.fetch` before doing anything.
#### restore
Restore `window.fetch`.
#### getUrl
Returns request URL.
#### getMethod
Returns request method. Default is 'get'.
#### getBody
Returns message body. Default is '' (empty string).
#### getRequestHeaders
Returns request headers. Default is {} (empty object).
#### getOptions
Returns request options. Default is {} (empty object)
#### called (Boolean getter)
Check whether this instance of fakeFetch was ever called
#### respondWith(data, options)
##### data
Type: `object`
The object to be sent as request-body.
##### options
Type: `object`
Custom Response options, see [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)
## Remarks
You may find necessary to use browserify before using fake-fetch within your specs, e.g.
```js
// karma.conf.js
preprocessors: {
'./node_modules/fake-fetch/index.js': [ 'browserify' ]
}
```
Real-life usage example can be found here [https://github.com/msn0/file-downloader/blob/master/test.js](https://github.com/msn0/file-downloader/blob/master/test.js)
## License
MIT © [Michał Jezierski](https://pl.linkedin.com/in/jezierskimichal)