@twilson63/mock-fetch
mock out fetch requests and provide a defined response for integration testing.
Last updated 2 months ago by twilson63 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @twilson63/mock-fetch 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 2.0.1                                ...           latest (2 months ago)

6 Versions

  • 2.0.1                                ...           2 months ago
  • 2.0.0                                ...           3 months ago
  • 1.1.0                                ...           3 months ago
  • 1.0.2                                ...           3 months ago
  • 1.0.1                                ...           3 months ago
  • 1.0.0                                ...           3 months ago
Maintainers (1)
Downloads
Today 0
This Week 12
This Month 13
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (8)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |