Created
November 8, 2011 21:56
-
-
Save bsatrom/1349384 to your computer and use it in GitHub Desktop.
Samples for hooking into STDOUT for unit testing in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports = module.exports | |
exports.setup = (callback) -> | |
write = process.stdout.write | |
process.stdout.write = ((stub) -> | |
(string, encoding, fd) -> | |
stub.apply process.stdout, arguments | |
callback string, encoding, fd)(process.stdout.write) | |
return -> process.stdout.write = write |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exports = module.exports; | |
exports.setup = function(callback) { | |
var write = process.stdout.write; | |
process.stdout.write = (function(stub) { | |
function(string, encoding, fd) { | |
stub.apply(process.stdout, arguments); | |
callback(string, encoding, fd); | |
}; | |
})(process.stdout.write); | |
return function() { | |
process.stdout.write = write; | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports = module.exports | |
# server logic omitted for brevity | |
exports.listen = (port, host, callback) -> | |
try | |
server.listen port, host, callback | |
util.log "Server listening at http://#{host or "127.0.0.1"}:#{port}/" | |
catch err | |
util.log err.message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exports; | |
exports = module.exports; | |
exports.listen = function(port, host, callback) { | |
try { | |
server.listen(port, host, callback); | |
return util.log("Server listening at http://" + (host || "127.0.0.1") + ":" + port + "/"); | |
} catch (err) { | |
return util.log(err.message); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server = require '../js/server' | |
hook = require '../js/hook_stdout' | |
describe 'chat server', -> | |
it 'should listen at localhost on a port I specify', -> | |
unhook = hook.setup((string, encoding, fd) -> | |
expect(string).toContain 'Server listening at http://127.0.0.1:3001/' | |
) | |
server.listen '3001' | |
unhook() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternate version: