Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
The "Mock Request" object extracted from express
branch: master

This branch is even with ericallam:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore
README.md
index.js
package.json

README.md

express-mock-request

express-mock-request is a utility function to make it easy to write tests for Node.JS Express applications. In fact, it was extracted from the Express test-suite here. I wanted to use it in my own application and I so I extracted it into this module and removed the mocha specific testing code.

How to use:

First, install using npm:

npm install express-mock-request

Then require it in your test file like so:

var request = require('express-mock-request');

Let's say you want to test this simple express app to make sure it returns the proper status, body, and headers:

var express = require('express')
var app = module.exports = express();

app.get('/', function(req, res) {
  res.send("ok", {'Content-Type': 'text/html'}, 200);
});

// Only start listening on 8080 when this file is run directly i.e.: node app.js
if(!module.parent) {
  app.listen(8080);
}

You could write your test like so:

var request = require('express-mock-request');
// require the express application, notice how we exported the express app using `module.exports` above
var app = require('../app');

// This example uses nodeunit
exports.testGet = function(test){
  request(app).get('/').expect(function(response) {
    // response is the response from hitting '/'
    test.equal(response.body, "ok");
    test.equal(response.statusCode, 200);
    test.equal(response.headers['content-type'], "text/html");
    test.done();
  });
}

You can see more examples in the express test suite here.

Something went wrong with that request. Please try again.