New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

external-mock

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

external-mock

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

0.1.0
latest
Source
npm
Version published
Maintainers
1
Created
Source

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.

Keywords

integration

FAQs

Package last updated on 11 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts