fetch-reply-with

Mock window.fetch requests to ease unit testing

Usage no npm install needed!

<script type="module">
  import fetchReplyWith from 'https://cdn.skypack.dev/fetch-reply-with';
</script>

README

fetch-reply-with

npm version Shippable branch

Simplifies unit tests by intercepting window.fetch requests and returning mocked responses.

Install

npm install --save-dev fetch-reply-with

Usage

require('fetch-reply-with'); // <- fetch is now globally available within the node environment

//
// setup URL intercepts and mock responses
//

// intercept GET http://www.orcascan.com
fetch('http://www.orcascan.com', {

    // regular fetch option
    method: 'GET',

    // add reply for this fetch
    replyWith: {
        status: 200,
        body: 'Bulk Barcode Scanning app',
        headers: {
            'Content-Type': 'text/html'
        }
    }
});

// typical fetch request
fetch('http://www.orcascan.com').then(function(res){
    // gets mocked response
    return res.text();
})
.then(function(text){
    // text now equals Bulk Barcode Scanning app
});

Requests that are not intercepted are executed as normal.

Unit Tests

The project includes unit tests, to run the tests:

  1. Checkout git clone https://github.com/john-doherty/fetch-reply-with
  2. Navigate into project folder cd fetch-reply-with
  3. Install dependencies npm install
  4. Run the tests npm test

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :)

Star the repo

If you find this useful please star the repo, it helps us prioritize fixes :raised_hands:

History

For change-log, check releases.

License

Licensed under MIT License © John Doherty