From 8edd9c2ed169909bf19a3f9229143d8fc87d5284 Mon Sep 17 00:00:00 2001 From: Andrew Safigan Date: Wed, 13 Jul 2016 22:38:59 -0400 Subject: [PATCH] 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' --- lib/runner.js | 8 ++++++++ test/runner.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/runner.js b/lib/runner.js index f39ed1070..1093e6cad 100644 --- a/lib/runner.js +++ b/lib/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); }; diff --git a/test/runner.js b/test/runner.js index 37df7938e..ef52674e6 100644 --- a/test/runner.js +++ b/test/runner.js @@ -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();