From cb35ea028718711df4bca61e9247c723a3e5fa2e Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 15:20:08 +0300 Subject: [PATCH 01/19] promisify Runner#serial and Runner#concurrent --- lib/runner.js | 55 ++++++++++++++++++++++++++++++--------------------- package.json | 2 ++ 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 3743c5df8..ea49b3c9b 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,8 +1,10 @@ 'use strict'; +var Promise = require('pinkie-promise'); +var promisify = require('pify'); var util = require('util'); var EventEmitter = require('events').EventEmitter; -var each = require('each-async'); -var eachSerial = require('async-each-series'); +var each = promisify(require('each-async')); +var eachSerial = promisify(require('async-each-series')); var Test = require('./test'); function Runner(opts) { @@ -44,8 +46,8 @@ Runner.prototype.addAfterHook = function (title, cb) { this.tests.after.push(new Test(title, cb)); }; -Runner.prototype.concurrent = function (tests, cb) { - each(tests, function (test, i, next) { +Runner.prototype.concurrent = function (tests) { + return each(tests, function (test, i, next) { test.run(function (err, duration) { if (err) { this.stats.failCount++; @@ -60,11 +62,11 @@ Runner.prototype.concurrent = function (tests, cb) { this.emit('test', err, test.title, duration); next(); }.bind(this)); - }.bind(this), cb); + }.bind(this)); }; -Runner.prototype.serial = function (tests, cb) { - eachSerial(tests, function (test, next) { +Runner.prototype.serial = function (tests) { + return eachSerial(tests, function (test, next) { test.run(function (err, duration) { if (err) { this.stats.failCount++; @@ -79,7 +81,7 @@ Runner.prototype.serial = function (tests, cb) { this.emit('test', err, test.title, duration); next(); }.bind(this)); - }.bind(this), cb); + }.bind(this)); }; Runner.prototype.run = function (cb) { @@ -88,20 +90,29 @@ Runner.prototype.run = function (cb) { var before = this.tests.before; var after = this.tests.after; - // TODO: refactor this bullshit - this.serial(before, function () { - if (this.stats.failCount > 0) { - return this.end(cb); - } - - this.serial(serial, function () { - this.concurrent(concurrent, function () { - this.serial(after, function () { - this.end(cb); - }.bind(this)); - }.bind(this)); - }.bind(this)); - }.bind(this)); + var self = this; + + this.serial(before) + .then(function () { + if (self.stats.failCount > 0) { + return Promise.reject(); + } + }) + .then(function () { + return self.serial(serial); + }) + .then(function () { + return self.concurrent(concurrent); + }) + .then(function () { + return self.serial(after); + }) + .catch(function () { + return; + }) + .then(function () { + self.end(cb); + }); }; Runner.prototype.end = function (cb) { diff --git a/package.json b/package.json index 4dd956435..a7192b319 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,8 @@ "fn-name": "^2.0.0", "globby": "^3.0.1", "meow": "^3.3.0", + "pify": "^2.0.0", + "pinkie-promise": "^1.0.0", "plur": "^2.0.0", "pretty-ms": "^2.0.0", "resolve-from": "^1.0.0", From 4952ba05a2640ecbb6aad168ba04c9f8d0fdec6f Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 15:37:08 +0300 Subject: [PATCH 02/19] pass Promise to `pify` module to work, when Promise is missing --- lib/runner.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index ea49b3c9b..f0ca8addb 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -3,8 +3,8 @@ var Promise = require('pinkie-promise'); var promisify = require('pify'); var util = require('util'); var EventEmitter = require('events').EventEmitter; -var each = promisify(require('each-async')); -var eachSerial = promisify(require('async-each-series')); +var each = promisify(require('each-async'), Promise); +var eachSerial = promisify(require('async-each-series'), Promise); var Test = require('./test'); function Runner(opts) { From 49cdda62b5e937da38802702fe51a6ce63171f5c Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 15:56:09 +0300 Subject: [PATCH 03/19] promisify Runner#run --- index.js | 2 +- lib/runner.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 6d3e9590d..3192391bb 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ function exit(stats, results) { setImmediate(function () { runner.on('test', test); - runner.run(exit); + runner.run().then(exit); }); module.exports = runner.addTest.bind(runner); diff --git a/lib/runner.js b/lib/runner.js index f0ca8addb..08fe3799f 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -84,7 +84,7 @@ Runner.prototype.serial = function (tests) { }.bind(this)); }; -Runner.prototype.run = function (cb) { +Runner.prototype.run = function () { var concurrent = this.tests.concurrent; var serial = this.tests.serial; var before = this.tests.before; @@ -92,7 +92,7 @@ Runner.prototype.run = function (cb) { var self = this; - this.serial(before) + return this.serial(before) .then(function () { if (self.stats.failCount > 0) { return Promise.reject(); @@ -111,11 +111,11 @@ Runner.prototype.run = function (cb) { return; }) .then(function () { - self.end(cb); + self.stats.passCount = self.stats.testCount - self.stats.failCount; }); }; -Runner.prototype.end = function (cb) { - this.stats.passCount = this.stats.testCount - this.stats.failCount; - cb(this.stats, this.results); -}; +// Runner.prototype.end = function (cb) { +// this.stats.passCount = this.stats.testCount - this.stats.failCount; +// cb(this.stats, this.results); +// }; From 6248fbcc38d14831007c98b3872c62014776d129 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 15:56:25 +0300 Subject: [PATCH 04/19] update tests to use promise-based Runner#run --- test/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index 4e03152bd..ccc36f866 100644 --- a/test/test.js +++ b/test/test.js @@ -278,7 +278,7 @@ test('run serial tests before concurrent ones', function (t) { a.end(); }); - runner.run(function () { + runner.run().then(function () { t.same(arr, ['a', 'b', 'c']); t.end(); }); @@ -381,7 +381,7 @@ test('hooks - before', function (t) { a.end(); }); - runner.run(function () { + runner.run().then(function () { t.same(arr, ['a', 'b']); t.end(); }); @@ -405,7 +405,7 @@ test('hooks - after', function (t) { a.end(); }); - runner.run(function () { + runner.run().then(function () { t.same(arr, ['a', 'b']); t.end(); }); @@ -433,7 +433,7 @@ test('hooks - stop if before hooks failed', function (t) { a.end(); }); - runner.run(function () { + runner.run().then(function () { t.same(arr, ['a']); t.end(); }); From 4fc926bdab06d15187c8873a6abb9d0837996b8b Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 16:59:21 +0300 Subject: [PATCH 05/19] promisify Test#run --- lib/runner.js | 64 +++++++++++++++++++++++++++++---------------------- lib/test.js | 58 +++++++++++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 52 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 08fe3799f..c83693121 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -48,39 +48,47 @@ Runner.prototype.addAfterHook = function (title, cb) { Runner.prototype.concurrent = function (tests) { return each(tests, function (test, i, next) { - test.run(function (err, duration) { - if (err) { - this.stats.failCount++; - } - - this.results.push({ - duration: duration, - title: test.title, - error: err - }); - - this.emit('test', err, test.title, duration); - next(); - }.bind(this)); + test.run() + .catch(function (err) { + return err; + }) + .then(function (err) { + if (err) { + this.stats.failCount++; + } + + this.results.push({ + duration: test.duration, + title: test.title, + error: err + }); + + this.emit('test', err, test.title, test.duration); + next(); + }.bind(this)); }.bind(this)); }; Runner.prototype.serial = function (tests) { return eachSerial(tests, function (test, next) { - test.run(function (err, duration) { - if (err) { - this.stats.failCount++; - } - - this.results.push({ - duration: duration, - title: test.title, - error: err - }); - - this.emit('test', err, test.title, duration); - next(); - }.bind(this)); + test.run() + .catch(function (err) { + return err; + }) + .then(function (err) { + if (err) { + this.stats.failCount++; + } + + this.results.push({ + duration: test.duration, + title: test.title, + error: err + }); + + this.emit('test', err, test.title, test.duration); + next(); + }.bind(this)); }.bind(this)); }; diff --git a/lib/test.js b/lib/test.js index 7dbd284eb..c5baff737 100644 --- a/lib/test.js +++ b/lib/test.js @@ -1,4 +1,5 @@ 'use strict'; +var Promise = require('pinkie-promise'); var setImmediate = require('set-immediate-shim'); var util = require('util'); var assert = require('assert'); @@ -66,34 +67,39 @@ Test.prototype.skip = function () { this.skipTest = true; }; -Test.prototype.run = function (cb) { - this.cb = cb; +Test.prototype.run = function () { + this.promise = {}; - if (!this.fn || this.skipTest) { - this.exit(); - } - - this._timeStart = Date.now(); + return new Promise(function (resolve, reject) { + this.promise.resolve = resolve; + this.promise.reject = reject; - try { - var ret = this.fn(this); + if (!this.fn || this.skipTest) { + return this.exit(); + } - if (ret && typeof ret.then === 'function') { - ret.then(this.exit.bind(this)).catch(function (err) { - this.assertError = new assert.AssertionError({ - actual: err, - message: 'Promise rejected → ' + err, - operator: 'promise', - stackStartFunction: this - }); + this._timeStart = Date.now(); - this.exit(); - }.bind(this)); + try { + var ret = this.fn(this); + + if (ret && typeof ret.then === 'function') { + ret.then(this.exit.bind(this)).catch(function (err) { + this.assertError = new assert.AssertionError({ + actual: err, + message: 'Promise rejected → ' + err, + operator: 'promise', + stackStartFunction: this + }); + + this.exit(); + }.bind(this)); + } + } catch (err) { + this.assertError = err; + this.exit(); } - } catch (err) { - this.assertError = err; - this.exit(); - } + }.bind(this)); }; Test.prototype.end = function () { @@ -124,7 +130,11 @@ Test.prototype.exit = function () { this.ended = true; setImmediate(function () { - this.cb(this.assertError, this.duration); + if (this.assertError) { + return this.promise.reject(this.assertError); + } + + this.promise.resolve(); }.bind(this)); } }; From 9334bc2991d92ab166d05793f4a1fd8e8d0defe1 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 17:03:05 +0300 Subject: [PATCH 06/19] resolve Test#run with a test instance --- lib/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/test.js b/lib/test.js index c5baff737..3cedfc68c 100644 --- a/lib/test.js +++ b/lib/test.js @@ -134,7 +134,7 @@ Test.prototype.exit = function () { return this.promise.reject(this.assertError); } - this.promise.resolve(); + this.promise.resolve(this); }.bind(this)); } }; From ccd9702aa58dd5905021a65f6accdd6d71ce4a33 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 17:23:04 +0300 Subject: [PATCH 07/19] fix error handling --- lib/runner.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index c83693121..848f62960 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -49,21 +49,21 @@ Runner.prototype.addAfterHook = function (title, cb) { Runner.prototype.concurrent = function (tests) { return each(tests, function (test, i, next) { test.run() - .catch(function (err) { - return err; + .catch(function () { + return; }) - .then(function (err) { - if (err) { + .then(function () { + if (test.assertError) { this.stats.failCount++; } this.results.push({ duration: test.duration, title: test.title, - error: err + error: test.assertError }); - this.emit('test', err, test.title, test.duration); + this.emit('test', test.assertError, test.title, test.duration); next(); }.bind(this)); }.bind(this)); @@ -72,21 +72,21 @@ Runner.prototype.concurrent = function (tests) { Runner.prototype.serial = function (tests) { return eachSerial(tests, function (test, next) { test.run() - .catch(function (err) { - return err; + .catch(function () { + return; }) - .then(function (err) { - if (err) { + .then(function () { + if (test.assertError) { this.stats.failCount++; } this.results.push({ duration: test.duration, title: test.title, - error: err + error: test.assertError }); - this.emit('test', err, test.title, test.duration); + this.emit('test', test.assertError, test.title, test.duration); next(); }.bind(this)); }.bind(this)); From 40c0271302dca126b8cce2f33b1604ec83698172 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 17:23:15 +0300 Subject: [PATCH 08/19] make tests use promises --- test/test.js | 90 ++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/test/test.js b/test/test.js index ccc36f866..0ec7edd8f 100644 --- a/test/test.js +++ b/test/test.js @@ -9,7 +9,7 @@ test('run test', function (t) { ava('foo', function (a) { a.true(false); a.end(); - }).run(function (err) { + }).run().catch(function (err) { t.true(err); t.end(); }); @@ -18,8 +18,8 @@ test('run test', function (t) { test('test title is optional', function (t) { ava(function (a) { a.end(); - }).run(function () { - t.is(this.title, '[anonymous]'); + }).run().then(function (a) { + t.is(a.title, '[anonymous]'); t.end(); }); }); @@ -27,8 +27,8 @@ test('test title is optional', function (t) { test('infer test name from function', function (t) { ava(function foo(a) { a.end(); - }).run(function () { - t.is(this.title, 'foo'); + }).run().then(function (a) { + t.is(a.title, 'foo'); t.end(); }); }); @@ -39,8 +39,8 @@ test('multiple asserts', function (t) { a.true(true); a.true(true); a.end(); - }).run(function () { - t.is(this.assertCount, 3); + }).run().then(function (a) { + t.is(a.assertCount, 3); t.end(); }); }); @@ -50,9 +50,9 @@ test('plan assertions', function (t) { a.plan(2); a.true(true); a.true(true); - }).run(function () { - t.is(this.planCount, 2); - t.is(this.assertCount, 2); + }).run().then(function (a) { + t.is(a.planCount, 2); + t.is(a.assertCount, 2); t.end(); }); }); @@ -63,7 +63,7 @@ test('run more assertions than planned', function (t) { a.true(true); a.true(true); a.true(true); - }).run(function (err) { + }).run().catch(function (err) { t.true(err); t.is(err.name, 'AssertionError'); t.end(); @@ -73,7 +73,7 @@ test('run more assertions than planned', function (t) { test('handle non-assertion errors', function (t) { ava(function () { throw new Error(); - }).run(function (err) { + }).run().catch(function (err) { t.is(err.name, 'Error'); t.true(err instanceof Error); t.end(); @@ -84,8 +84,8 @@ test('handle testing of arrays', function (t) { ava(function (a) { a.same(['foo', 'bar'], ['foo', 'bar']); a.end(); - }).run(function (err) { - t.false(err); + }).run().then(function (a) { + t.false(a.assertError); t.end(); }); }); @@ -94,8 +94,8 @@ test('handle falsy testing of arrays', function (t) { ava(function (a) { a.notSame(['foo', 'bar'], ['foo', 'bar', 'cat']); a.end(); - }).run(function (err) { - t.false(err); + }).run().then(function (a) { + t.false(a.assertError); t.end(); }); }); @@ -104,8 +104,8 @@ test('handle testing of objects', function (t) { ava(function (a) { a.same({foo: 'foo', bar: 'bar'}, {foo: 'foo', bar: 'bar'}); a.end(); - }).run(function (err) { - t.false(err); + }).run().then(function (a) { + t.false(a.assertError); t.end(); }); }); @@ -114,8 +114,8 @@ test('handle falsy testing of objects', function (t) { ava(function (a) { a.notSame({foo: 'foo', bar: 'bar'}, {foo: 'foo', bar: 'bar', cat: 'cake'}); a.end(); - }).run(function (err) { - t.false(err); + }).run().then(function (a) { + t.false(a.assertError); t.end(); }); }); @@ -127,8 +127,8 @@ test('handle throws', function (t) { }); a.end(); - }).run(function (err) { - t.false(err); + }).run().then(function (a) { + t.false(a.assertError); t.end(); }); }); @@ -140,7 +140,7 @@ test('handle throws with error', function (t) { }); a.end(); - }).run(function (err) { + }).run().catch(function (err) { t.true(err); t.end(); }); @@ -153,8 +153,8 @@ test('handle falsy throws', function (t) { }); a.end(); - }).run(function (err) { - t.false(err); + }).run().then(function (a) { + t.false(a.assertError); t.end(); }); }); @@ -166,7 +166,7 @@ test('handle falsy throws with error', function (t) { }); a.end(); - }).run(function (err) { + }).run().catch(function (err) { t.true(err); t.end(); }); @@ -179,7 +179,7 @@ test('run functions after last planned assertion', function (t) { a.plan(1); a.true(true); i++; - }).run(function () { + }).run().then(function () { t.is(i, 1); t.end(); }); @@ -199,7 +199,7 @@ test('run async functions after last planned assertion', function (t) { foo(function () { i++; }); - }).run(function () { + }).run().then(function () { t.is(i, 1); t.end(); }); @@ -212,8 +212,8 @@ test('planned async assertion', function (t) { setTimeout(function () { a.pass(); }, 100); - }).run(function (err) { - t.error(err); + }).run().then(function (a) { + t.error(a.assertError); t.end(); }); }); @@ -224,8 +224,8 @@ test('async assertion with `.end()`', function (t) { a.pass(); a.end(); }, 100); - }).run(function (err) { - t.error(err); + }).run().then(function (a) { + t.error(a.assertError); t.end(); }); }); @@ -235,7 +235,7 @@ test('more assertions than planned should emit an assertion error', function (t) a.plan(1); a.pass(); a.pass(); - }).run(function (err) { + }).run().catch(function (err) { t.true(err, err); t.is(err.name, 'AssertionError'); t.end(); @@ -289,8 +289,8 @@ test.skip('skip test with `.skip()`', function (t) { a.skip(); a.pass(); a.end(); - }).run(function () { - t.is(this.assertCount, 0); + }).run().then(function (a) { + t.is(a.assertCount, 0); t.end(); }); }); @@ -314,8 +314,8 @@ test('promise support - assert pass', function (t) { return promisePass().then(function () { a.pass(); }); - }).run(function () { - t.is(this.assertCount, 1); + }).run().then(function (a) { + t.is(a.assertCount, 1); t.end(); }); }); @@ -326,7 +326,7 @@ test('promise support - assert fail', function (t) { // TODO: replace with `a.fail()` when it's available a.true(false); }); - }).run(function (err) { + }).run().catch(function (err) { t.true(err); t.is(err.name, 'AssertionError'); t.end(); @@ -338,7 +338,7 @@ test('promise support - reject', function (t) { return promiseFail().then(function () { a.pass(); }); - }).run(function (err) { + }).run().catch(function (err) { t.true(err); t.is(err.name, 'AssertionError'); t.end(); @@ -346,19 +346,14 @@ test('promise support - reject', function (t) { }); test('record test duration', function (t) { - var avaTest; - ava(function (a) { - avaTest = a; - a.plan(1); setTimeout(function () { a.true(true); }, 1234); - }).run(function (err) { - t.false(err); - t.true(avaTest.duration >= 1234); + }).run().then(function (a) { + t.true(a.duration >= 1234); t.end(); }); }); @@ -383,7 +378,6 @@ test('hooks - before', function (t) { runner.run().then(function () { t.same(arr, ['a', 'b']); - t.end(); }); }); @@ -439,7 +433,7 @@ test('hooks - stop if before hooks failed', function (t) { }); }); -test('ES2015 support', function (t) { +test.skip('ES2015 support', function (t) { t.plan(2); execFile('../cli.js', ['es2015.js'], { From 49f49a6023543b96640fd796c3437e1673f7163c Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 17:26:40 +0300 Subject: [PATCH 09/19] fix es2015 test to work with promises --- test/es2015.js | 2 +- test/test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/es2015.js b/test/es2015.js index d53c5357a..f85f47ad3 100644 --- a/test/es2015.js +++ b/test/es2015.js @@ -5,7 +5,7 @@ test('run test', t => { ava('foo', a => { a.true(false); a.end(); - }).run(err => { + }).run().catch(err => { t.true(err); t.end(); }); diff --git a/test/test.js b/test/test.js index 0ec7edd8f..ebde57b98 100644 --- a/test/test.js +++ b/test/test.js @@ -433,7 +433,7 @@ test('hooks - stop if before hooks failed', function (t) { }); }); -test.skip('ES2015 support', function (t) { +test('ES2015 support', function (t) { t.plan(2); execFile('../cli.js', ['es2015.js'], { From 52377b443727b1465f6d28e0dd147851108f6920 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 20:39:06 +0300 Subject: [PATCH 10/19] use bluebird instead of pinkie-promise --- lib/runner.js | 2 +- lib/test.js | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 848f62960..06992de75 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,5 +1,5 @@ 'use strict'; -var Promise = require('pinkie-promise'); +var Promise = require('bluebird'); var promisify = require('pify'); var util = require('util'); var EventEmitter = require('events').EventEmitter; diff --git a/lib/test.js b/lib/test.js index 3cedfc68c..830cf68b6 100644 --- a/lib/test.js +++ b/lib/test.js @@ -1,5 +1,5 @@ 'use strict'; -var Promise = require('pinkie-promise'); +var Promise = require('bluebird'); var setImmediate = require('set-immediate-shim'); var util = require('util'); var assert = require('assert'); diff --git a/package.json b/package.json index a7192b319..31da4656a 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "async-each-series": "^1.0.0", "ava-init": "^0.1.0", "babel-core": "^5.8.23", + "bluebird": "^2.9.34", "chalk": "^1.0.0", "claim": "^1.3.0", "each-async": "^1.0.0", @@ -56,7 +57,6 @@ "globby": "^3.0.1", "meow": "^3.3.0", "pify": "^2.0.0", - "pinkie-promise": "^1.0.0", "plur": "^2.0.0", "pretty-ms": "^2.0.0", "resolve-from": "^1.0.0", From 9fca658b93d809e14e2a8b8edb75ca27502bfa1d Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 20:55:41 +0300 Subject: [PATCH 11/19] use bluebird for control flow --- lib/runner.js | 23 +++++++++-------------- lib/test.js | 1 + package.json | 3 --- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 06992de75..e8c8023d3 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,10 +1,7 @@ 'use strict'; var Promise = require('bluebird'); -var promisify = require('pify'); var util = require('util'); var EventEmitter = require('events').EventEmitter; -var each = promisify(require('each-async'), Promise); -var eachSerial = promisify(require('async-each-series'), Promise); var Test = require('./test'); function Runner(opts) { @@ -47,8 +44,13 @@ Runner.prototype.addAfterHook = function (title, cb) { }; Runner.prototype.concurrent = function (tests) { - return each(tests, function (test, i, next) { - test.run() + // run all tests + tests = tests.map(function (test) { + return test.run(); + }); + + return Promise.all(tests, function (test) { + return test .catch(function () { return; }) @@ -64,14 +66,13 @@ Runner.prototype.concurrent = function (tests) { }); this.emit('test', test.assertError, test.title, test.duration); - next(); }.bind(this)); }.bind(this)); }; Runner.prototype.serial = function (tests) { - return eachSerial(tests, function (test, next) { - test.run() + return Promise.resolve(tests).each(function (test) { + return test.run() .catch(function () { return; }) @@ -87,7 +88,6 @@ Runner.prototype.serial = function (tests) { }); this.emit('test', test.assertError, test.title, test.duration); - next(); }.bind(this)); }.bind(this)); }; @@ -122,8 +122,3 @@ Runner.prototype.run = function () { self.stats.passCount = self.stats.testCount - self.stats.failCount; }); }; - -// Runner.prototype.end = function (cb) { -// this.stats.passCount = this.stats.testCount - this.stats.failCount; -// cb(this.stats, this.results); -// }; diff --git a/lib/test.js b/lib/test.js index 830cf68b6..bba0c01c8 100644 --- a/lib/test.js +++ b/lib/test.js @@ -70,6 +70,7 @@ Test.prototype.skip = function () { Test.prototype.run = function () { this.promise = {}; + // TODO: refactor this to avoid storing the promise return new Promise(function (resolve, reject) { this.promise.resolve = resolve; this.promise.reject = reject; diff --git a/package.json b/package.json index 31da4656a..8afafd68f 100644 --- a/package.json +++ b/package.json @@ -45,18 +45,15 @@ "jasmine" ], "dependencies": { - "async-each-series": "^1.0.0", "ava-init": "^0.1.0", "babel-core": "^5.8.23", "bluebird": "^2.9.34", "chalk": "^1.0.0", "claim": "^1.3.0", - "each-async": "^1.0.0", "figures": "^1.3.5", "fn-name": "^2.0.0", "globby": "^3.0.1", "meow": "^3.3.0", - "pify": "^2.0.0", "plur": "^2.0.0", "pretty-ms": "^2.0.0", "resolve-from": "^1.0.0", From f438b81e2687ad6571f33dda9045cbb99eef1a58 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 21:06:44 +0300 Subject: [PATCH 12/19] separate test result handling into a function to DRY stuff up --- lib/runner.js | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index e8c8023d3..f615a444f 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -55,17 +55,7 @@ Runner.prototype.concurrent = function (tests) { return; }) .then(function () { - if (test.assertError) { - this.stats.failCount++; - } - - this.results.push({ - duration: test.duration, - title: test.title, - error: test.assertError - }); - - this.emit('test', test.assertError, test.title, test.duration); + this.addTestResult(test); }.bind(this)); }.bind(this)); }; @@ -77,21 +67,25 @@ Runner.prototype.serial = function (tests) { return; }) .then(function () { - if (test.assertError) { - this.stats.failCount++; - } - - this.results.push({ - duration: test.duration, - title: test.title, - error: test.assertError - }); - - this.emit('test', test.assertError, test.title, test.duration); + this.addTestResult(test); }.bind(this)); }.bind(this)); }; +Runner.prototype.addTestResult = function (test) { + if (test.assertError) { + this.stats.failCount++; + } + + this.results.push({ + duration: test.duration, + title: test.title, + error: test.assertError + }); + + this.emit('test', test.assertError, test.title, test.duration); +}; + Runner.prototype.run = function () { var concurrent = this.tests.concurrent; var serial = this.tests.serial; From a132e9f99904bef443a05eff8d885501533f7036 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sun, 6 Sep 2015 21:07:25 +0300 Subject: [PATCH 13/19] use bluebird in tests --- package.json | 1 - test/test.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 8afafd68f..48d1fefcd 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "update-notifier": "^0.5.0" }, "devDependencies": { - "pinkie-promise": "^1.0.0", "tap-dot": "^1.0.0", "tape": "^4.0.0", "xo": "*" diff --git a/test/test.js b/test/test.js index ebde57b98..aa1c2bb98 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,6 @@ 'use strict'; var test = require('tape'); -var Promise = require('pinkie-promise'); +var Promise = require('bluebird'); var execFile = require('child_process').execFile; var ava = require('../lib/test'); var Runner = require('../lib/runner'); From 74c6854c5ba047714cc3900e281759a73c977e6a Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 7 Sep 2015 13:04:38 +0300 Subject: [PATCH 14/19] make Runner#addTestResult() a private method --- lib/runner.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index f615a444f..8d899da4b 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -55,7 +55,7 @@ Runner.prototype.concurrent = function (tests) { return; }) .then(function () { - this.addTestResult(test); + this._addTestResult(test); }.bind(this)); }.bind(this)); }; @@ -67,12 +67,12 @@ Runner.prototype.serial = function (tests) { return; }) .then(function () { - this.addTestResult(test); + this._addTestResult(test); }.bind(this)); }.bind(this)); }; -Runner.prototype.addTestResult = function (test) { +Runner.prototype._addTestResult = function (test) { if (test.assertError) { this.stats.failCount++; } From b85433603cb1e4f9753bf51bff35176b035a290a Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 7 Sep 2015 13:20:09 +0300 Subject: [PATCH 15/19] fix Runner#concurrent method --- lib/runner.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 8d899da4b..417fb71a2 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -46,17 +46,15 @@ Runner.prototype.addAfterHook = function (title, cb) { Runner.prototype.concurrent = function (tests) { // run all tests tests = tests.map(function (test) { - return test.run(); + // in case of error, don't reject a promise + // if promise would be rejected, Promise.all would exit early + return test.run().catch(function () { + return; + }); }); - return Promise.all(tests, function (test) { - return test - .catch(function () { - return; - }) - .then(function () { - this._addTestResult(test); - }.bind(this)); + return Promise.all(tests).each(function (test) { + this._addTestResult(test); }.bind(this)); }; From 7b54e1767bda781d8e5b76db04b3538bc01dc494 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 7 Sep 2015 13:20:29 +0300 Subject: [PATCH 16/19] fix exit function to get stats & results from runner --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3192391bb..7124727d5 100644 --- a/index.js +++ b/index.js @@ -48,7 +48,10 @@ function stack(results) { }); } -function exit(stats, results) { +function exit() { + var stats = runner.stats; + var results = runner.results; + if (stats.testCount > 0) { log.write(); } From ec0194353e6b3e93469916353c81677c17b0f88f Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 7 Sep 2015 13:43:17 +0300 Subject: [PATCH 17/19] fix Runner#concurrent to return a test instance in case of error --- lib/runner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runner.js b/lib/runner.js index 417fb71a2..51650d816 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -49,7 +49,7 @@ Runner.prototype.concurrent = function (tests) { // in case of error, don't reject a promise // if promise would be rejected, Promise.all would exit early return test.run().catch(function () { - return; + return test; }); }); From 5033b913361671d47c80c4453bd151609451b8c7 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 7 Sep 2015 13:47:10 +0300 Subject: [PATCH 18/19] use main entrypoint in es6 test, instead of Test directly --- test/es2015.js | 12 +++--------- test/test.js | 6 +++--- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/test/es2015.js b/test/es2015.js index f85f47ad3..32cf73bc7 100644 --- a/test/es2015.js +++ b/test/es2015.js @@ -1,12 +1,6 @@ -import test from 'tape'; -import ava from '../lib/test'; +import test from '../'; test('run test', t => { - ava('foo', a => { - a.true(false); - a.end(); - }).run().catch(err => { - t.true(err); - t.end(); - }); + t.true(false); + t.end(); }); diff --git a/test/test.js b/test/test.js index aa1c2bb98..dcb5f4c63 100644 --- a/test/test.js +++ b/test/test.js @@ -438,8 +438,8 @@ test('ES2015 support', function (t) { execFile('../cli.js', ['es2015.js'], { cwd: __dirname - }, function (err, stdout) { - t.error(err); - t.true(stdout.trim().length > 0); + }, function (err, stdout, stderr) { + t.true(err); + t.true(stderr.trim().length > 0); }); }); From dedc6fb358ff197c8c8e5e1033118af0c4afe282 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Mon, 7 Sep 2015 13:56:40 +0300 Subject: [PATCH 19/19] continuous output of test results in Runner#concurrent --- lib/runner.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 51650d816..2955be88c 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -47,15 +47,14 @@ Runner.prototype.concurrent = function (tests) { // run all tests tests = tests.map(function (test) { // in case of error, don't reject a promise - // if promise would be rejected, Promise.all would exit early return test.run().catch(function () { - return test; - }); - }); + return; + }).then(function () { + this._addTestResult(test); + }.bind(this)); + }, this); - return Promise.all(tests).each(function (test) { - this._addTestResult(test); - }.bind(this)); + return Promise.all(tests); }; Runner.prototype.serial = function (tests) {