Created
January 30, 2014 00:32
-
-
Save Raynos/8700273 to your computer and use it in GitHub Desktop.
Using throw for async tests === no cleanup
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 test = require('mocha').it | |
var assert = require('assert') | |
var request = require('request') | |
var http = require('http') | |
test('request things', function (done) { | |
var PORT = Math.floor(Math.random() * 10000 + 20000) | |
var serv = http.createServer(function (req, res) { | |
if (Math.random() < 0.5) { | |
// throws, `serv.close()` never called | |
assert.fail('Oops random assert failed') | |
} | |
res.end(JSON.stringify("ok")) | |
}) | |
serv.listen(PORT, function () { | |
request({ | |
uri: "localhost:" + PORT + "/", | |
json: true | |
}, function (err, res) { | |
assert.ifError(err) | |
assert.equal(res.body, 'ok') | |
serv.close(done) | |
}) | |
}) | |
}) |
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 test = require('mocha').it | |
// USE SAFE ASSERT | |
// DOES NOT THROW, queues errors if any | |
// and releases them on `.end()` | |
var assert = require('safe-assert') | |
var request = require('request') | |
var http = require('http') | |
test('request things', function (done) { | |
var PORT = Math.floor(Math.random() * 10000 + 20000) | |
var serv = http.createServer(function (req, res) { | |
if (Math.random() < 0.5) { | |
// this does not throw so no big deal | |
assert.fail('Oops random assert failed') | |
} | |
res.end(JSON.stringify("ok")) | |
}) | |
serv.listen(PORT, function () { | |
request({ | |
uri: "localhost:" + PORT + "/", | |
json: true | |
}, function (err, res) { | |
assert.ifError(err) | |
assert.equal(res.body, 'ok') | |
serv.close(function () { | |
assert.end(done) | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment