Skip to content

Commit

Permalink
provide clear error message when users attempt nested/async calls to …
Browse files Browse the repository at this point in the history
…`test` (#961)

Fixes #948.

AVA does not support nested / async calls to `test`, but we were not providing a great error message when users attempted that.

* added failing test for expected error

* tests passed: waiting for review

* added test

* changed error message

* fixed error in test

* changed 'hasBegunRunning' to 'hasStarted'
  • Loading branch information
asafigan authored and jamestalmage committed Jul 14, 2016
1 parent c82f980 commit 8edd9c2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/runner.js
Expand Up @@ -46,6 +46,7 @@ function Runner(options) {

this.results = [];
this.tests = new TestCollection();
this.hasStarted = false;
this._bail = options.bail;
this._serial = options.serial;
this._match = options.match || [];
Expand All @@ -61,6 +62,11 @@ optionChain(chainableMethods, function (opts, args) {
var fn;
var macroArgIndex;

if (this.hasStarted) {
throw new Error('All tests and hooks must be declared synchronously in your ' +
'test file, and cannot be nested within other tests or hooks.');
}

if (typeof args[0] === 'string') {
title = args[0];
fn = args[1];
Expand Down Expand Up @@ -196,6 +202,8 @@ Runner.prototype.run = function (options) {

this.tests.on('test', this._addTestResult);

this.hasStarted = true;

return Promise.resolve(this.tests.build(this._bail).run()).then(this._buildStats);
};

Expand Down
36 changes: 36 additions & 0 deletions test/runner.js
Expand Up @@ -13,6 +13,42 @@ test('must be called with new', function (t) {
t.end();
});

test('nested tests and hooks aren\'t allowed', function (t) {
t.plan(1);

var runner = new Runner();

runner.test(function () {
t.throws(function () {
runner.test(noop);
}, {message: 'All tests and hooks must be declared synchronously in your ' +
'test file, and cannot be nested within other tests or hooks.'});
});

runner.run({}).then(function () {
t.end();
});
});

test('tests must be declared synchronously', function (t) {
t.plan(1);

var runner = new Runner();

runner.test(function () {
return Promise.resolve();
});

runner.run({});

t.throws(function () {
runner.test(noop);
}, {message: 'All tests and hooks must be declared synchronously in your ' +
'test file, and cannot be nested within other tests or hooks.'});

t.end();
});

test('runner emits a "test" event', function (t) {
var runner = new Runner();

Expand Down

0 comments on commit 8edd9c2

Please sign in to comment.