Skip to content

Commit

Permalink
Update: Switch to lab for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 27, 2016
1 parent e650813 commit f47e60e
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 147 deletions.
11 changes: 7 additions & 4 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ function buildOnSettled(done){
done = done || _.noop;

function onSettled(error, result){
if(error){
return done(error, null);
}

var settledErrors = _.where(result, { state: 'error' });
var settledResults = _.where(result, { state: 'success' });

var errors;
var errors = null;
if(settledErrors.length){
errors = _.pluck(settledErrors, 'value');
}

var results;
var results = null;
if(settledResults.length){
results = _.pluck(settledResults, 'value');
}

error = error || errors;
done(error, results);
done(errors, results);
}

return onSettled;
Expand Down
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"test": "test"
},
"scripts": {
"test": "tap ./test/*.js",
"cover": "istanbul cover ./test/cover/index.js"
"test": "lab -cv"
},
"repository": {
"type": "git",
Expand All @@ -27,14 +26,12 @@
},
"homepage": "https://github.com/phated/bach",
"devDependencies": {
"istanbul": "^0.3.0",
"require-dir": "^0.1.0",
"tap": "~0.4.8"
"lab": "^4.1.0"
},
"dependencies": {
"async-done": "~0.3.0",
"async-settle": "~0.1.0",
"async-done": "~0.4.0",
"async-settle": "~0.2.0",
"lodash": "~2.4.1",
"now-and-later": "0.0.3"
"now-and-later": "0.0.4"
}
}
3 changes: 0 additions & 3 deletions test/cover/index.js

This file was deleted.

30 changes: 20 additions & 10 deletions test/getExtensions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
'use strict';

var test = require('tap').test;
var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = require('lab').expect;

var getExtensions = require('../lib/helpers').getExtensions;

test('should return the argument if it is an object', function(t){
var obj = {};
t.equal(getExtensions(obj), obj, 'should be the same object');
t.end();
});
describe('getExtensions', function(){

it('should return the argument if it is an object', function(done){
var obj = {};
expect(getExtensions(obj)).to.equal(obj);
done();
});

test('should return undefined if argument is not an object', function(t){
var fn = function(){};
t.deepEqual(getExtensions(fn), undefined, 'should be a undefined');
t.end();
it('should return undefined if argument is not an object', function(done){
var fn = function(){};
expect(getExtensions(fn)).to.equal(undefined);
done();
});
});
40 changes: 29 additions & 11 deletions test/onSettled.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';

var test = require('tap').test;
var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = require('lab').expect;

var onSettled = require('../lib/helpers').onSettled;

Expand All @@ -9,15 +16,26 @@ var errors = [
{ state: 'error', value: new Error('Error 2') }
];

test('should group all errors', function(t){
onSettled(function(errs, results){
t.equal(errs.length, 2, 'errors should contain both errors');
t.notOk(results, 'results should contain nothing');
t.end();
})(null, errors);
});
describe('onSettled', function(){

it('should group all errors', function(done){
onSettled(function(errs, results){
expect(errs).to.have.length(2);
expect(results).to.equal(null);
done();
})(null, errors);
});

it('should error early if called with an error', function(done){
onSettled(function(err, results){
expect(err).to.be.an.instanceof(Error);
expect(results).to.equal(null);
done();
})(new Error('Should not happen'));
});

test('should handle the no callback case', function(t){
onSettled()(null, errors);
t.end();
it('should handle the no callback case', function(done){
onSettled()(null, errors);
done();
});
});
71 changes: 50 additions & 21 deletions test/parallel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';

var test = require('tap').test;
var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = require('lab').expect;

var bach = require('../');

Expand All @@ -22,27 +29,49 @@ function fnError(done){
done(new Error('An Error Occurred'));
}

test('should execute functions in parallel and call callback with results on completion', function(t){
bach.parallel(fn1, fn2, fn3)(function(error, results){
t.notOk(error, 'error should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
describe('parallel', function(){

it('should execute functions in parallel and call callback with results on completion', function(done){
bach.parallel(fn1, fn2, fn3)(function(error, results){
expect(error).to.equal(null);
expect(results).to.deep.equal([1, 2, 3]);
done();
});
});

it('should execute functions in parallel and call callback with error on an error', function(done){
function slowFn(done){
setTimeout(function(){
expect('slow function should not be called').to.equal(undefined);
done(null, 2);
}, 500);
}
bach.parallel(fn1, slowFn, fn3, fnError)(function(error, results){
expect(error).to.be.instanceof(Error);
expect(results).to.deep.equal([1, undefined, 3, undefined]);
done();
});
});
});

test('should execute functions in parallel and call callback with error on an error', function(t){
function slowFn(done){
setTimeout(function(){
t.fail('slow function should not be called');
done(null, 2);
}, 500);
}
bach.parallel(fn1, slowFn, fn3, fnError)(function(error, results){
t.ok(error, 'error should be defined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, undefined, 3, undefined], 'results should be [1, undefined, 3, undefined]');
t.ok(error instanceof Error, 'error should be instance of an Error');
t.end();
it('should take extension points and call them for each function', function(done){
var arr = [];
var fns = [fn1, fn2, fn3];
bach.parallel(fn1, fn2, fn3, {
create: function(fn, idx){
expect(fns).to.include(fn);
arr[idx] = fn;
return arr;
},
before: function(storage){
expect(storage).to.equal(arr);
},
after: function(storage){
expect(storage).to.equal(arr);
}
})(function(error, results){
expect(error).to.equal(null);
expect(arr).to.deep.include.members(fns);
});
done();
});
});
70 changes: 49 additions & 21 deletions test/series.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';

var test = require('tap').test;
var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = require('lab').expect;

var bach = require('../');

Expand All @@ -22,27 +29,48 @@ function fnError(done){
done(new Error('An Error Occurred'));
}

test('should execute functions in series and call callback with results on completion', function(t){
bach.series(fn1, fn2, fn3)(function(error, results){
t.notOk(error, 'error should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
describe('series', function(){

it('should execute functions in series and call callback with results on completion', function(done){
bach.series(fn1, fn2, fn3)(function(error, results){
expect(error).to.equal(null);
expect(results).to.deep.equal([1, 2, 3]);
done();
});
});

it('should execute functions in series and call callback with error on an error', function(done){
function slowFn(done){
setTimeout(function(){
done(null, 2);
}, 500);
}
bach.series(fn1, slowFn, fn3, fnError)(function(error, results){
expect(error).to.be.an.instanceof(Error);
expect(results).to.deep.equal([1, 2, 3, undefined]);
done();
});
});
});

test('should execute functions in series and call callback with error on an error', function(t){
function slowFn(done){
setTimeout(function(){
t.ok(true, 'slow function should be called');
done(null, 2);
}, 500);
}
bach.series(fn1, slowFn, fn3, fnError)(function(error, results){
t.ok(error, 'error should be defined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3, undefined], 'results should be [1, 2, 3, undefined]');
t.ok(error instanceof Error, 'error should be instance of an Error');
t.end();
it('should take extension points and call them for each function', function(done){
var arr = [];
var fns = [fn1, fn2, fn3];
bach.series(fn1, fn2, fn3, {
create: function(fn, idx){
expect(fns).to.include(fn);
arr[idx] = fn;
return arr;
},
before: function(storage){
expect(storage).to.equal(arr);
},
after: function(storage){
expect(storage).to.equal(arr);
}
})(function(error, results){
expect(error).to.equal(null);
expect(arr).to.deep.include.members(fns);
});
done();
});
});
Loading

0 comments on commit f47e60e

Please sign in to comment.