Mock 'http' objects for testing Express routing functions
This project is available as a NPM package.
$ npm install --save-dev node-mocks-http
Our example includes
--save-devbased on the assumption that node-mocks-http will be used as a development dependency..
After installing the package include the following in your test files:
var httpMocks = require'node-mocks-http';
Suppose you have the following Express route:
And you have created a function to handle that route's call:
You can easily test the
routeHandler function with some code like
this using the testing framework of your choice:
exports'routeHandler - Simple testing' =var request = httpMockscreateRequestmethod: 'GET'url: '/user/42'params:id: 42;var response = httpMockscreateResponse;routeHandlerrequest response;var data = JSONparse response_getData ;testequal"Bob Dog" dataname;testequal42 dataage;testequal"firstname.lastname@example.org" dataemail;testequal200 responsestatusCode ;testok response_isEndCalled;testok response_isJSON;testok response_isUTF8;testdone;;
We wanted some simple mocks without a large framework.
We also wanted the mocks to act like the original framework being mocked, but allow for setting of values before calling and inspecting of values after calling.
We are looking for more volunteers to bring value to this project, including the creation of more objects from the HTTP module.
This project doesn't address all features that must be mocked, but it is a good start. Feel free to send pull requests, and a member of the team will be timely in merging them.
If you wish to contribute please read our Contributing Guidelines.
Most releases fix bugs with our mocks or add features similar to the
Response objects offered by Node.js and extended
Licensed under MIT.