Skip to content

Instantly share code, notes, and snippets.

@mefernandez
Last active December 24, 2015 16:38
Show Gist options
  • Save mefernandez/6829025 to your computer and use it in GitHub Desktop.
Save mefernandez/6829025 to your computer and use it in GitHub Desktop.
A Mocha test to find out what's the problem with Rewire and Express apps.
var express = require('express');
var app = express();
function sayHello(req, res) {
res.send(200, 'Hello!');
res.end();
}
// all environments
app.set('port', process.env.PORT || 3000);
app.use(express.errorHandler());
app.use(app.router);
app.get('/', sayHello);
module.exports = app;
{
"name": "rewire-mocha-express",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.2.5",
"jade": "*",
"superagent": "*",
"rewire": "*"
}
}
var fs = require('fs')
, request = require('superagent')
, assert = require('assert')
, http = require('http');
describe('App', function() {
var base = 'http://localhost:3000';
it('should say Hello', function(done) {
var app = require('../app.js');
var server = http.createServer(app);
server.listen(3000);
var path = '/';
request.get(base + path).redirects(0).end(function(res) {
assert.equal(res.status, 200);
assert.equal(res.text, 'Hello!');
server.close();
done();
});
});
it('should say Rewired', function(done) {
var rewire = require('rewire');
var app = rewire('../app.js');
app.__set__('sayHello', function(req, res) {
res.send(200, 'Rewired!');
res.end();
});
var server = http.createServer(app);
server.listen(3000);
var path = '/';
request.get(base + path).redirects(0).end(function(res) {
assert.equal(res.status, 200);
assert.equal(res.text, 'Rewired!');
server.close();
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment