A Node library for testing controller actions without having to spin up a server. Because unit testing controllers in Node should be easier.
-
An acknowledged naming-spoof on supertest -- because super-duper-test was a little clunky.
-
Note: A mock database such as mockgoose is also suggested when unit testing, and works great with
dupertest
.
Unit testing Node controllers should be simple. Rails has it easy with RSpec, but unfortunately no test framework provides the same simplicity for unit testing controllers in Node.
Jasmine is great at testing code, but doesn't go very far with app logic. Supertest extends where Jasmine leaves off, but goes too far in spinning up a live server to send requests to. Unit tests shouldn't use live servers or even databases -- enter dupertest
. Test Node controllers like you want to.
To install the latest official version, use NPM:
npm install dupertest
To run the test suite, including example usage specs:
npm test
Calling a controller action and making an assertion about the response is as simple as this:
var request = require('dupertest').request;
request(controller.action)
.params({id: 123})
.expect(entity, done);
For a more detailed example, imagine an entities-controller
with a show
action that needs to be tested.
// example controller action
exports.show = function(req, res) {
var id = req.params.id;
var entity = Entity.find(id);
res.send(entity);
};
With dupertest
, testing that controller action is easy:
var dupertest = require('dupertest'),
request = dupertest.request
entities = require('../controllers/entities-controller');
// other test setup here
// such as entity = {id: 123, ...}
// and/or entity.save();
describe('entities.show', function () {
it('should return an error when an entity does not exist', function (done) {
request(entities.show)
.params({id: 1})
.expect(404, done);
});
it('should return an entity when one does exist', function (done) {
request(entities.show)
.params({id: 123})
.end(function(response) {
expect(response.id).toEqual(entity.id);
done();
});
});
});
// Examples shown using Jasmine -- dupertest will work with any test framework.
// However, the `expect` method is only available when using Jasmine
It's that simple. It looks just like request
or supertest
syntax, but without the requirement of a server.
- Check out the example specs for more usage examples.
Under the hood, it's a lot like one might expect. dupertest
takes in a controller action, and then lets you build up the request and response objects to your liking. After adding various properties to the req
and res
objects, such as params
, body
, or anything else you need with the flexible extendReq
function, the original controller action gets called with either the shorthand expect
or the longhand end
method.
The result might look something like the below (using the same entity example above):
// Building up the request and response objects
req = {
params: {
id: 123
}
};
res = {
// the assertion function here is the function passed into the `end` method
send: assertion;
}
};
And then the original action is called with the built up objects:
entities.show(req, res);
Effectively passing an assertion into the controller:
exports.show = function(req, res) {
var id = req.params.id;
var entity = Entity.find(id);
// res.send(entity) is effectively replaced with the assertion
expect(entity.id).toEqual(expectation);
};
Notice in the case described above, the function in res.send
will be some sort of assertion statement, creating the ability to test the response.
dupertest
really shines in allowing users to continue using a comfortable format for request chains, without having to worry about passing assertion statements around to odd locations.
For a direct example of where a need for dupertest
might be, check out this Stackoverflow post, and my response example using dupertest.
Methods available to the dupertest
object.
-
dupertest.setDefaults(object)
Sets default request and response values to be used for the entire test session. A common case would be to set something like thereq.get
function here, before initiating the request. See the example spec for an example of how this can be used. -
dupertest.clearDefaults()
Clears the defaults object. Useful if defaults were previously set and are no longer required. -
dupertest.request(fn)
Takes in a controller action to build the request against. This action is not called until eitherend
orexpect
is called upon the request chain.
Methods available to the dupertest.request
instance.
-
Request.prototype.params (object)
Shorthand notation to set thereq.params
object. -
Request.prototype.body (object)
Shorthand notation to set thereq.body
object. -
Request.prototype.extendReq (object)
Liberally extends thereq
object to include any passed in properties. -
Request.prototype.extendRes (object)
Liberally extends theres
object to include any passed in properties. -
Request.prototype.expect (object, fn)
Shorthand syntax for a Jasmine expect statement. The expectation is often in the form of an object (but can be anything), and will be compared to the return value of the controller action with the Jasmine statement:expect(obj).toEqual(object)
. This method ends the request chain. As such, the callback function will often bedone
.
Note: Jasmine must be the test framework for this method to work.
Request.prototype.end (fn)
Ends the request chain with a supplied callback receiving the return value from the original controller action. In almost every case the callback will want to include the expect statement.
- Bring in a standard library for more robust mock request and response objects.
- Add a way to inject assertions to other
res
properties besides justsend
. - Test with other frameworks, like Mocha.
- Create a before-end method, that allows any inferred request or response properties to be built. Example:
req.originalUrl
is built from thereq.baseUrl
andreq.params
properties. (a more robust request and response mock would help solve this issue as well) . - Consider adding support for multiple
expect
statements, similar tosupertest
. - Make defaults less global. Currently once they are set by any test they will remain until the test suite finishes. May be better suited to set in a
beforeEach
sort of setting, and be cleared after each test.
Be sure to also check out any issues.
- Fork it
- Create your feature branch
git checkout -b my-new-feature
- Commit your changes
git commit -am 'Add some feature'
- Push to the branch
git push origin my-new-feature
- Create new Pull Request