@mock/request
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

@mock/request npm

Mock HTTP(S) requests through redirecting to a temporary HTTP(S) server allowing to handle a request in whatever way.

Install

$ yarn add --dev @mock/request

Usage

const mockRequest: (file: string, callback: http.RequestListener) => () => void
// fetch-data.js
import fetch from 'node-fetch'

export const fetchData = async (): Promise<{ [key: string]: any }> => {
  const response = await fetch('https://api.thecatapi.com/v1/breeds')
  const data = await response.json()

  return data
}
import test from 'blue-tape'
import { mockRequest } from '@mock/request'

test('fetchData', async (t) => {
  const unmockRequest = mockRequest('./fetch-data', (req, res) => {
    if (req.url === 'https://api.thecatapi.com/v1/breeds' && req.method === 'GET') {
      res.end(JSON.stringify({ fake: true }))
    } else {
      t.fail('should not get here')
    }
  })

  const { fetchData } = await import('./fetch-data')
  const data = await fetchData()

  t.deepEqual(
    data,
    { fake: true },
    'should fetch data'
  )

  unmockRequest()
})

Readme

Keywords

Package Sidebar

Install

npm i @mock/request

Weekly Downloads

0

Version

0.2.1

License

MIT

Unpacked Size

5.95 kB

Total Files

5

Last publish

Collaborators

  • psxcode
  • deepsweet