@twilson63/mock-fetch

mock out fetch requests and provide a defined response for integration testing.

Usage no npm install needed!

<script type="module">
  import twilson63MockFetch from 'https://cdn.skypack.dev/@twilson63/mock-fetch';
</script>

README

mock-fetch

mock out fetch requests and provide a defined response for integration testing.

Example

import { test } from 'tape-modern'
import { mfetch, clear } from './'

const url = 'https://jsonplaceholder.typicode.com/todos'

test('post mfetch', async t => {
  mfetch.post(url, { status: 201, body: {ok: true}})

  const result = await fetch(url, {
    method: 'POST',
    body: JSON.stringify({"name": "foo"}),
    headers: {
      'content-type': 'application/json'
    }
  }).then(res => res.json())

  t.ok(result.ok)

  clear()
})

Install

npm install --save-dev @twilson63/mock-fetch

API

  • mfetch(url, method, resposne)

  • mfetch.post(url, response)

  • mfetch.put(url, response)

  • mfetch.delete(url, response) - watches for delete fetches that matches the url

  • clear() - clears mocks

  • response object contains a status and body property

{
  status: 200,
  body: { ok: true }
}

License

MIT

Contributions

pull-requests are welcome.