Simplifies unit tests by intercepting fetch requests and returning mocked responses.
npm install --save-dev fetch-reply-with
require('fetch-reply-with'); // <- `fetch` is now globally available
// intercept GET http://orcascan.com and reply with...
fetch('http://orcascan.com', {
// regular fetch option
method: 'GET',
// add reply for this fetch
replyWith: {
status: 200,
body: 'Barcode Scanner app',
headers: {
'Content-Type': 'text/html'
},
delay: 500 // miliseconds to wait before responding (default = 0)
}
});
// execute fetch request
fetch('http://orcascan.com').then(function(res){
return res.text();
})
.then(function(text){
// text === `Barcode Scanner app`
});
Couple of things to note:
replyWith
are executed as normalreplyWith
can be modified by redefining the replyWith
The project includes unit tests, to run the tests:
git clone https://github.com/john-doherty/fetch-reply-with
cd fetch-reply-with
npm install
npm test
git checkout -b my-new-feature
git commit -m 'Add some feature'
git push origin my-new-feature
If you find this useful please star the repo, it helps us prioritize fixes :raised_hands:
For change-log, check releases.
Author: john-doherty
Source Code: https://github.com/john-doherty/fetch-reply-with
License: MIT License