# external-mock

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

Latest version **0.2.1** (published 2025-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install external-mock
pnpm add external-mock
yarn add external-mock
bun add external-mock
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2025-04-23 |
| First published | 2022-08-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18.0 |
| Dependencies | 1 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Tobias Lindström |
| Maintainers | tobiasli |
| Keywords | integration, mock, e2e |

## Links

- npm: https://www.npmjs.com/package/external-mock
- Repository: https://github.com/TobiasL/external-mock
- Homepage: https://github.com/TobiasL/external-mock#readme
- Issues: https://github.com/TobiasL/external-mock/issues
- npm.io page: https://npm.io/package/external-mock

## Dependencies (1)

- [express](https://npm.io/package/express.md) ^5.1.0

## Alternatives

- [@snazzah/davey](https://npm.io/package/@snazzah/davey.md) — 1.5M weekly downloads
- [@vendure/testing](https://npm.io/package/@vendure/testing.md) — 8.3K weekly downloads
- [vue-simple-context-menu](https://npm.io/package/vue-simple-context-menu.md) — 6.6K weekly downloads
- [cypress-webpack-preprocessor-v5](https://npm.io/package/cypress-webpack-preprocessor-v5.md) — 2.1K weekly downloads
- [@backstage/plugin-catalog-backend-module-puppetdb](https://npm.io/package/@backstage/plugin-catalog-backend-module-puppetdb.md) — 1.3K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2025-04-23
- 0.2.0 — 2025-04-22
- 0.1.0 — 2022-08-11
- 0.0.1 — 2022-08-10

## README

# External Mock

[![Build Status](https://github.com/TobiasL/external-mock/workflows/Test%20and%20lint/badge.svg)](https://github.com/TobiasL/external-mock/actions)
[![Available on NPM](https://img.shields.io/npm/v/external-mock.svg)](https://npmjs.com/package/external-mock)

_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](https://jestjs.io/docs/mock-functions) functions or [Nock](https://github.com/nock/nock).

[Wiremock](https://github.com/wiremock/wiremock) is a commonly used alternative if you want to run another process.

## Install

Using npm:

```bash
$ npm install --save-dev external-mock
```

## Example usage

```javascript
const { createMock, cleanExternalMocks } = require("external-mock");

afterEach(() => cleanExternalMocks());

test("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.

---
_Source: https://npm.io/package/external-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
