# reqresnext

> Tiny helper for express middleware testing

Latest version **1.7.0** (published 2022-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install reqresnext
pnpm add reqresnext
yarn add reqresnext
bun add reqresnext
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.7.0 |
| Published | 2022-02-15 |
| First published | 2018-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 1.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Anton Golub |
| Maintainers | antongolub, qiwibot |
| Keywords | mock, express mock, http mock, mock testing, req res next |

## Links

- npm: https://www.npmjs.com/package/reqresnext
- Repository: https://github.com/antongolub/reqresnext
- Homepage: https://github.com/antongolub/reqresnext#readme
- Issues: https://github.com/antongolub/reqresnext/issues
- npm.io page: https://npm.io/package/reqresnext

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [express](https://npm.io/package/express.md) ^4.17.2
- [setprototypeof](https://npm.io/package/setprototypeof.md) ^1.2.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
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.7.0 (latest) — 2022-02-15
- 1.8.0-beta.0 (beta) — 2022-04-07
- 1.6.0-beta.1 — 2022-04-06
- 1.6.0-beta.0 — 2022-04-06
- 1.6.6 — 2022-01-27
- 1.6.5 — 2022-01-26
- 1.6.4 — 2021-07-12
- 1.6.3 — 2020-02-03
- 1.6.2 — 2019-10-18
- 1.6.1 — 2019-10-10
- 1.6.0 — 2019-10-10
- 1.5.5 — 2019-09-01
- 1.5.4 — 2019-08-16
- 1.5.3 — 2019-08-01
- 1.5.2 — 2019-07-13
- … 15 more at https://npm.io/package/reqresnext/versions

## README

# reqresnext

[![Build Status](https://travis-ci.com/antongolub/reqresnext.svg?branch=master)](https://travis-ci.com/antongolub/reqresnext)
[![npm (tag)](https://img.shields.io/npm/v/reqresnext/latest.svg)](https://www.npmjs.com/package/reqresnext)
[![Maintainability](https://api.codeclimate.com/v1/badges/b6f37eeb4ba506b87f6c/maintainability)](https://codeclimate.com/github/antongolub/reqresnext/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/b6f37eeb4ba506b87f6c/test_coverage)](https://codeclimate.com/github/antongolub/reqresnext/test_coverage)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com)

Tiny helper for [expressjs](https://expressjs.com/) middlewares testing.

##### Motivation
There're several nice mocking tools for express/connect.
The best of them (imho) is [node-mocks-http](https://github.com/howardabrams/node-mocks-http) by Howard Abrams.
This lib brings constructors, that accurately reproduces logic of express classes. That's pretty cool, but sometimes you need a bit more.

Reqresnext:
1. Just uses `express` proto directly. So you get not _just similar_, but _exactly the same_ behaviour as in production.
2. Exposes the only additional property to verify outgoing data — `res.body`.

## Install
```bash
npm add reqresnext -D
yarn add reqresnext -D
```

## Usage
```javascript
    import reqresnext from 'reqresnext'
 
    it('middleware does something', () => {
      const {req, res, next} = reqresnext(<ReqOptions>, <ResOptions>)
      mware(req, res, next)
 
      expect(res.statusCode).toEqual('...')
      expect(res.body).toEqual('...')
    })
```
#### Request & Response
Also you may construct `req/res` instances directly:
```javascript
    import {Response, Request} from 'reqresnext'

    const foo = {name: 'foo', value: 'bar', options: {}}
    const res = new Response({cookies: [foo]})
    
    expect(res.get('Set-Cookie')).toEqual('foo=bar; Path=/')
```
Supported options are described in [interface](./src/interface.js). The main ones:
```javascript
// cookies
    const foo = {name: 'foo', value: 'bar', options: {}}
    const res = new Response({cookies: [foo]})

// headers
    const req = new Request({headers: {foo: 'bar', baz: 'qux'}})

// url
    const req = new Request({url: 'https://example.com'})
```

Any additional props that does not intersect with proto are injected as is.
```javascript
    const res = new Response({foo: 'bar', baz: 1, ...})
    res.foo // 'bar'
```

#### `next()` handler 
`Next` handler may be wrapped with `spy` anytime: before or after `reqresnext`. If function is specified, the factory just passes it back.
```javascript
    const handler = chai.spy(() => {})
    const {next} = reqresnext({}, {}, handler)
    
    next === handler // true  
```

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