0.1.0 • Published 2 years ago

external-mock v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

External Mock

Build Status Available on NPM

Mock external REST APIs that we don't have control over in an integration test scenario.

About

External Mock is intended to be used to mock external REST APIs that we don't have control over in an integration test scenario.

If you need to mock requests from the same process use the Jest mock functions or Nock.

Wiremock is a commonly used alternative if you want to run another process.

Install

npm install external-mock

Example usage

const { createMock, cleanExternalMocks } = require('external-mock')

afterEach(() => cleanExternalMocks())

it('Slack hook is used', async () => {
  const slackHook = jest.fn()

  const fakeSlackServer = createMock(5555)

  fakeSlackServer
    .post('/message')
    .spy(slackHook)
    .reply(200, { text: 'Hello World' })

  await makeOurServiceCallSlack()

  expect(slackHook).toBeCalledWith({ text: 'Temp' })
})

API

createMock(port: number): ExternalMock

Starts a server without any request handlers on the specified port. Add request handlers by calling the HTTP verbs functions on the returned ExternalMock instance. The available functions are get, post, put, patch and delete.

A test spy can optionally be added by calling the function spy after the HTTP verbs.

The request handler is finalized by calling the function reply with the status code and optionally a response body.

cleanExternalMocks(): void

Call to clean up all created mocks after the test is done.

0.1.0

2 years ago

0.0.1

2 years ago