1.7.0 • Published 2 years ago

reqresnext v1.7.0

Weekly downloads
18
License
MIT
Repository
github
Last release
2 years ago

reqresnext

Build Status npm (tag) Maintainability Test Coverage js-standard-style

Tiny helper for expressjs middlewares testing.

Motivation

There're several nice mocking tools for express/connect. The best of them (imho) is 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

npm add reqresnext -D
yarn add reqresnext -D

Usage

    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:

    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. The main ones:

// 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.

    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.

    const handler = chai.spy(() => {})
    const {next} = reqresnext({}, {}, handler)
    
    next === handler // true  
1.8.0-beta.0

2 years ago

1.6.0-beta.0

2 years ago

1.6.0-beta.1

2 years ago

1.7.0

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.4

3 years ago

1.6.3

4 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.8

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago