Skip to content

Instantly share code, notes, and snippets.

@thisconnect
Last active December 14, 2015 09:18
Show Gist options
  • Save thisconnect/5063576 to your computer and use it in GitHub Desktop.
Save thisconnect/5063576 to your computer and use it in GitHub Desktop.
Use connect.js to server a directory but add some files at a specific URI.
<!DOCTYPE html>
<meta charset="utf-8">
<meta name=viewport content="width=device-width, initial-scale=1.0">
<title>Mocha Tests</title>
<link rel=stylesheet href="mocha.css">
<div id=mocha></div>
<script src="mocha.js"></script>
<script>mocha.setup('bdd');</script>
<script src="expect.js"></script>
<script src="indexOf.js"></script>
<script>mocha.run();</script>
if (!global.expect) expect = require('expect.js'); // ugly
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
expect([1, 2, 3].indexOf(5)).to.equal(-1);
expect([1, 2, 3].indexOf(0)).to.equal(-1);
});
it('should return position when the value is present', function(){
expect([1, 2, 3].indexOf(3)).to.equal(2);
});
});
});
var connect = require('connect'),
send = require('send');
connect()
.use(connect.static(__dirname + '/'))
.use('/mocha.js', function(req, res){
send(req, 'mocha.js')
.root(__dirname + '/../node_modules/mocha/')
.pipe(res);
})
.use('/mocha.css', function(req, res){
send(req, 'mocha.css')
.root(__dirname + '/../node_modules/mocha/')
.pipe(res);
})
.use('/expect.js', function(req, res){
send(req, 'expect.js')
.root(__dirname + '/../node_modules/expect.js/')
.pipe(res);
})
.use(function(req, res){
var body = '<!DOCTYPE HTML><meta charset="utf-8"><h1>404</h1>';
res.statusCode = 404;
res.setHeader('Content-Length', body.length);
res.end(body);
})
.listen(8080, '127.0.0.1');
console.log('browse tests @ http://127.0.0.1:8080/');
{
"name": "mocha-test"
, "devDependencies": {
"mocha": ">= 1.8.1"
, "connect": ">= 2.7.3"
, "expect.js": "0.2.0"
, "send": ">= 0.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment