Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created January 30, 2014 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/8700273 to your computer and use it in GitHub Desktop.
Save Raynos/8700273 to your computer and use it in GitHub Desktop.
Using throw for async tests === no cleanup
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)
})
})
})
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