Gathering detailed insights and metrics for express-test-stubs
Gathering detailed insights and metrics for express-test-stubs
Gathering detailed insights and metrics for express-test-stubs
Gathering detailed insights and metrics for express-test-stubs
Collection of stubs for unit testing Express endpoints
npm install express-test-stubs
Typescript
Module System
Node Version
NPM Version
63.1
Supply Chain
98
Quality
73.4
Maintenance
100
Vulnerability
99.3
License
JavaScript (100%)
Total Downloads
2,227
Last Day
1
Last Week
8
Last Month
19
Last Year
177
MIT License
7 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 11, 2020
Latest Version
2.0.0
Package Id
express-test-stubs@2.0.0
Unpacked Size
8.56 kB
Size
3.34 kB
File Count
8
NPM Version
6.13.2
Node Version
12.13.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
100%
8
Compared to previous week
Last Month
72.7%
19
Compared to previous month
Last Year
-36.3%
177
Compared to previous year
4
A helper factory function that produces test fakes for the need of unit-testing Express endpoints.
This module makes use of stampit library - an implementation of Stamp Specification.
Some properties of objects produced by this factory are sinon spies.
1$ npm install --save-dev express-test-stubs
Using default stubs provided by express-test-stubs
:
1/* my-endpoint-handler.test.js */ 2const test = require('tape'); 3const ExpressStubs = require('express-test-stubs'); 4 5const self = require('./path/to/my-endpoint-handler'); 6 7test('normal scenario', async t => { 8 try { 9 const expressStubs = ExpressStubs(); 10 const expectedResponsePayload = { foo: 'bar '}; 11 12 await self(...expressStubs); 13 14 t.equal( 15 expressStubs.res.status.calledWith(200), 16 true, 17 'should respond with 200 OK' 18 ); 19 20 t.deepEqual( 21 expressStubs.res.json.getCall(0).args[0], 22 expectedResponsePayload, 23 'should send foo set to bar' 24 ); 25 26 t.equal( 27 expressStubs.next.called, 28 false, 29 'should not propagate any error' 30 ); 31 32 t.end(); 33 } catch (err) { 34 t.end(err); 35 } 36});
Using custom stubs:
1/* ... */ 2 3const expressStubs = ExpressStubs({ 4 req: ExpressStubs.Req 5 .props({ 6 body: { userId: 'baz' } 7 }), 8 res: ExpressStubs.Res 9 .props({ 10 additionalProp: 'foo', 11 json: 'value_that_overwrites_default_json_prop' 12 }) 13}); 14 15/* ... */
1const ExpressStubs = require('express-test-stubs');
The main factory function. This is a stampit stamp. Although you can utilize all the features stampit provides, below are the features added to the stamp by express-test-stubs
.
Just calling ExpressStubs
factory function without any arguments produces a default object containing default stubs for req
and res
objects, and next
callback function that are usually passed in Express endpoint handlers.
1const expressStubs = ExpressStubs(); 2// or 3const { req, res, next } = ExpressStubs();
An object with req
, res
, and next
properties that are default stubs.
For your convenience, the expressStubs
object is iterable over these three properties. So you can use it with destructuring:
1myExpressEndpointHandler(...expressStubs);
Request stub with the default set of properties (see below how you can customize them).
An empty POJO.
An empty POJO.
Response stub with the default set of properties (see below how you can customize them).
Sinon spy function with an implementation that returns this
when called by production code - just like Express` method status()
.
Sinon spy function with empty implementation.
Sinon spy function with empty implementation.
Sinon spy function with empty implementation.
Sinon spy function with empty implementation.
Helper static property referencing default stamp that is used internally for producing req
stubs. Use it as base stamp to compose your own custom stamp to be passed in ExpressStubs
factory (see below).
Helper static property referencing default stamp that is used internally for producing res
stubs. Use it as base stamp to compose your own custom stamp to be passed in ExpressStubs
factory (see below).
You can pass your custom implementation of the request and/or response stamps to ExpressStubs
factory function to produce an object with your custom implementation of req
and/or res
stubs. It's recommended to use ExpressStubs.Req
or ExpressStubs.Res
as the base for composition:
1const expressStubs = ExpressStubs({ 2 req: ExpressStubs.Req 3 .props({ 4 body: { userId: 'baz' } 5 }) 6});
1const expressStubs = ExpressStubs({
2 res: ExpressStubs.Res
3 .props({
4 additionalProp: 'foo',
5 json: 'value_that_overwrites_default_json_prop'
6 })
7});
1const expressStubs = ExpressStubs({ 2 req: ExpressStubs.Req 3 .props({ 4 body: { userId: 'baz' } 5 }), 6 res: ExpressStubs.Res 7 .props({ 8 additionalProp: 'foo', 9 json: 'value_that_overwrites_default_json_prop' 10 }) 11});
No vulnerabilities found.