# expressively-mocked-fetch

> A fetch mock that launches express as a worker_thread

Latest version **0.2.3** (published 2022-07-26) · GPL-3.0-only license · 16 weekly downloads

## Install

```sh
npm install expressively-mocked-fetch
pnpm add expressively-mocked-fetch
yarn add expressively-mocked-fetch
bun add expressively-mocked-fetch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2022-07-26 |
| First published | 2020-07-21 |
| Weekly downloads | 16 |
| License | GPL-3.0-only |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 44.3 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| Author | Tim Daubenschütz |
| Maintainers | timdaub |
| Keywords | express, fetch, fetch, mock, test |

## Links

- npm: https://www.npmjs.com/package/expressively-mocked-fetch
- npm.io page: https://npm.io/package/expressively-mocked-fetch

## Dependencies (3)

- [express](https://npm.io/package/express.md) 4.17.1
- [body-parser](https://npm.io/package/body-parser.md) 1.19.0
- [connect-pause](https://npm.io/package/connect-pause.md) 0.1.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 0.2.3 (latest) — 2022-07-26
- 0.2.2 — 2020-12-17
- 0.2.1 — 2020-12-17
- 0.2.0 — 2020-12-17
- 0.1.2 — 2020-12-16
- 0.1.1 — 2020-08-13
- 0.1.0 — 2020-08-13
- 0.0.3 — 2020-07-30
- 0.0.2 — 2020-07-22
- 0.0.1 — 2020-07-21

## README

# expressively-mocked-fetch

[![npm version](https://badge.fury.io/js/expressively-mocked-fetch.svg)](https://badge.fury.io/js/expressively-mocked-fetch)

> A fetch mock that launches express as a worker_thread to help testing.

## Installation

**NOTE:** This library uses `worker_threads`, a nodejs builtin that was released
with node v13.

```bash
$ npm i -D expressively-mocked-fetch
```

## Why

[Mocking fetch is annoying](https://kentcdodds.com/blog/stop-mocking-fetch). I
saw [msw](https://github.com/mswjs/msw). But it [didn't fit my use case](https://github.com/mswjs/msw/issues/287). So I ended up writing a wrapper around express.js.

## Develop

1. Download this repo and install the dependencies
2. Run `npm run test`

## Usage

```js
// with ava
const test = require("ava");
const createWorker = require("expressively-mocked-fetch");

test("if module is loaded and executed", async (t) => {
  const worker = await createWorker(`
// regular express.js code
app.get('/', function (req, res) {
  res.send("hello world");
});
  `);

  const res = await fetch(`http://localhost:${worker.port}`);
  const text = await res.text();
  t.assert(test === "hello world");
});
```

### createWorker(string, object)

- `string` needs to be valid Node.js JavaScript
- object is of shape `{ requestCount: Number, port: Number }`, where:

  - `requestCount` (optional, default: 1) is the amount of times a server
    should respond before automatically shutting itself.
  - `port` (optional, default: 0) is the desired port the server should
    launch at. For dynamic allocation by the OS, use the default value `0`.
  - `pauseMilliseconds` (optional, default: 0) the time in milliseconds a
    server must delay sending an answer using
    [connect-pause](https://github.com/flesler/connect-pause).

## Changelog

### 0.2.3

- Introduce optional option `pauseMilliseconds` that allows simulating a
  server's delayed response-ability.

### 0.2.2

- Allow template's `app` to be overwritten by using `let`.

### 0.2.1

- (Bug fix): `options` allows now to set individual keys without forgetting
  all default options.

### 0.2.0

- Refactor `createWorker` function signature to allow an options object.
- Add `port` to `options` that allows a user to define a port before
  launching a worker.

### 0.1.2

- Using node.js's `worker_threads` now allows expressively-mocked-fetch to
  spawn threads without the usage of temporary files. Interface remains the
  same.

### 0.1.1

- Add support for `Content-Type: text/calendar` and potentially other types.

### 0.1.0

- Allow accepting `text/plain` and `application/json`.

### 0.0.3

- Allow `defaultCount` option allows a worker to exist for more than 1 request.

### 0.0.2

- Bug fix: Create new module file for each worker to allow running in parallel.

### 0.0.1

- Initial release

## LICENSE

See LICENSE file.

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