Skip to content

Instantly share code, notes, and snippets.

@jergason
Forked from coolaj86/vows-example.js
Created January 17, 2012 18:43
Show Gist options
  • Save jergason/1628063 to your computer and use it in GitHub Desktop.
Save jergason/1628063 to your computer and use it in GitHub Desktop.
Asynchronous Vows
(function () {
"use strict";
var vows = require('vows')
, suite
, batch
, helloWorld = require('./index')
, assert = require('assert')
, request = require('ahr2')
, createJoin = require('join')
, forEachAsync = require('forEachAsync')
;
batch = {
'foo context': {
topic: function () {
return helloWorld();
}
, 'helloWorld output test': function (msg) {
assert.strictEqual(msg, 'Hello World!');
}
}
, 'test foobar3000': {
topic: function () {
request('http://foobar3000.com/echo/bar.json').when(this.callback);
}
, 'foobar3000 pathname test': function (err, ahr, body) {
assert.strictEqual(body.pathname, '/echo/bar.json');
}
}
, 'test join foobar3000': {
topic: function () {
var join = createJoin()
;
request('http://foobar3000.com/echo/baz.json').when(join.add());
request('http://foobar3000.com/echo/quux.json').when(join.add());
join.when(this.callback);
}
, 'foobar3000 pathname test': function (res1, res2) {
//var args = Array.prototype.slice(arguments);
var body1 = res1[2]
, body2 = res2[2]
;
assert.strictEqual(body1.pathname, '/echo/baz.json');
assert.strictEqual(body2.pathname, '/echo/quux.json');
}
, 'teardown': function (topic) {
console.log('in teardown function');
}
}
, 'test forEachAsync foobar3000': {
topic: function () {
var urls
, self = this
;
urls =[
'/echo/qux.json'
, '/echo/hurp.json'
];
forEachAsync(urls, function (next, url) {
request('http://foobar3000.com' + url).when(next);
}).then(function () {
self.callback(true);
});
}
, 'foobar3000 pathname test': function (finished) {
// at least we got here
assert.ok(finished);
}
}
};
suite = vows.describe('my-test');
suite.addBatch(batch);
suite.export(module);
}());
@jergason
Copy link
Author

Added a teardown function. See http://stackoverflow.com/questions/7065035/how-to-run-cleanup-with-vows-js and vowsjs/vows#14 for discussion on it, but it runs after the vows in the context finish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment