https://github.com/antongolub/reqresnext
Tiny helper for express middleware testing
https://github.com/antongolub/reqresnext
Last synced: 7 months ago
JSON representation
Tiny helper for express middleware testing
- Host: GitHub
- URL: https://github.com/antongolub/reqresnext
- Owner: antongolub
- License: mit
- Created: 2018-02-28T14:01:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-04-06T21:17:52.000Z (almost 4 years ago)
- Last Synced: 2025-05-20T15:28:58.743Z (8 months ago)
- Language: JavaScript
- Size: 3.21 MB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-web-dev-resources - reqresnext
README
# 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(, )
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
```