Skip to content

Commit 8edd9c2

Browse files
asafiganjamestalmage
authored andcommitted
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'
1 parent c82f980 commit 8edd9c2

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/runner.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function Runner(options) {
4646

4747
this.results = [];
4848
this.tests = new TestCollection();
49+
this.hasStarted = false;
4950
this._bail = options.bail;
5051
this._serial = options.serial;
5152
this._match = options.match || [];
@@ -61,6 +62,11 @@ optionChain(chainableMethods, function (opts, args) {
6162
var fn;
6263
var macroArgIndex;
6364

65+
if (this.hasStarted) {
66+
throw new Error('All tests and hooks must be declared synchronously in your ' +
67+
'test file, and cannot be nested within other tests or hooks.');
68+
}
69+
6470
if (typeof args[0] === 'string') {
6571
title = args[0];
6672
fn = args[1];
@@ -196,6 +202,8 @@ Runner.prototype.run = function (options) {
196202

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

205+
this.hasStarted = true;
206+
199207
return Promise.resolve(this.tests.build(this._bail).run()).then(this._buildStats);
200208
};
201209

test/runner.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@ test('must be called with new', function (t) {
1313
t.end();
1414
});
1515

16+
test('nested tests and hooks aren\'t allowed', function (t) {
17+
t.plan(1);
18+
19+
var runner = new Runner();
20+
21+
runner.test(function () {
22+
t.throws(function () {
23+
runner.test(noop);
24+
}, {message: 'All tests and hooks must be declared synchronously in your ' +
25+
'test file, and cannot be nested within other tests or hooks.'});
26+
});
27+
28+
runner.run({}).then(function () {
29+
t.end();
30+
});
31+
});
32+
33+
test('tests must be declared synchronously', function (t) {
34+
t.plan(1);
35+
36+
var runner = new Runner();
37+
38+
runner.test(function () {
39+
return Promise.resolve();
40+
});
41+
42+
runner.run({});
43+
44+
t.throws(function () {
45+
runner.test(noop);
46+
}, {message: 'All tests and hooks must be declared synchronously in your ' +
47+
'test file, and cannot be nested within other tests or hooks.'});
48+
49+
t.end();
50+
});
51+
1652
test('runner emits a "test" event', function (t) {
1753
var runner = new Runner();
1854

0 commit comments

Comments
 (0)