2.0.1 • Published 5 years ago

@twilson63/mock-fetch v2.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

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.

2.0.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago