Skip to content
Permalink
Browse files

provide clear error message when users attempt nested/async calls to …

…`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 8edd9c2ed169909bf19a3f9229143d8fc87d5284
Showing with 44 additions and 0 deletions.
  1. +8 −0 lib/runner.js
  2. +36 −0 test/runner.js
@@ -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 || [];
@@ -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];
@@ -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);
};

@@ -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();

0 comments on commit 8edd9c2

Please sign in to comment.
You can’t perform that action at this time.