1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75# reqresnext
[](https://travis-ci.com/antongolub/reqresnext)
[](https://www.npmjs.com/package/reqresnext)
[](https://codeclimate.com/github/antongolub/reqresnext/maintainability)
[](https://codeclimate.com/github/antongolub/reqresnext/test_coverage)
[](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
```