mockfetch
super simple mock object for fetch requests
Last updated 2 years ago by bergos .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mockfetch 
SYNC missed versions from official npm registry.

mockfetch

mockfetch is a super simple mock object for fetch requests.

Usage

The mockfetch factory returns the fetch mock object. Responses for specific URLs and methods can be defined in the constructor or with the .add method. If the callback property is defined in the response, the function will be called and the result will be merged with the configured response. Requests to an unknown URL or method will get a 404 response.

Examples

Using the Constructor

Defines responses for two URLs. The first URL response is static, the second generates the response body in the callback.

const fetch = mockFetch({
  'http://example.org/config': {
     headers: {
       'content-type': 'text/plain'
     }
     body: 'example body'
  },
  'http://example.org/callback': {
    headers: {
      'content-type': 'text/plain'
    },
    callback: (url, options) => {
      return {
        body: 'example body from callback'
      }
    }
  }
})

Using .add

Defines the same responses like in the constructor examples.

const fetch = mockFetch()

fetch.add('http://example.org/config', {
  headers: {
    'content-type': 'text/plain'
  }
  body: 'example body'
})

fetch.add('http://example.org/callback', {
  headers: {
    'content-type': 'text/plain'
  },
  callback: (url, options) => {
    return {
      body: 'example body from callback'
    }
  }
}

Current Tags

  • 1.0.0                                ...           latest (2 years ago)

1 Versions

  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (2)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |