From 3252a020ffd372e9583d39c989affd3d3b22957b Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Thu, 21 Nov 2019 09:23:23 -0700 Subject: [PATCH] fix(run): throw error if axe.run is called after a run has started but not completed (#1914) * fix(run): throw error if axe.run is called after a run has started but not completed * remove comment --- lib/core/public/run.js | 13 +++++++++++++ test/core/public/run.js | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/core/public/run.js b/lib/core/public/run.js index 68014c38e8..4e64603e95 100644 --- a/lib/core/public/run.js +++ b/lib/core/public/run.js @@ -107,11 +107,22 @@ axe.run = function(context, options, callback) { }); } + if (axe._running) { + const err = + 'Axe is already running. Use `await axe.run()` to wait ' + + 'for the previous run to finish before starting a new run.'; + callback(err); + reject(err); + return p; + } + + axe._running = true; axe._runRules( context, options, function(rawResults, cleanup) { let respond = function(results) { + axe._running = false; cleanup(); try { callback(null, results); @@ -131,12 +142,14 @@ axe.run = function(context, options, callback) { respond(results); } } catch (err) { + axe._running = false; cleanup(); callback(err); reject(err); } }, function(err) { + axe._running = false; callback(err); reject(err); } diff --git a/test/core/public/run.js b/test/core/public/run.js index 976c081831..0e30929313 100644 --- a/test/core/public/run.js +++ b/test/core/public/run.js @@ -35,6 +35,7 @@ describe('axe.run', function() { fixture.innerHTML = ''; axe._audit = null; axe._runRules = origRunRules; + axe._running = false; }); it('takes context, options and callback as parameters', function(done) { @@ -115,6 +116,14 @@ describe('axe.run', function() { axe.run({ someOption: true }, noop); }); + it('should error if axe is already running', function(done) { + axe.run(noop); + axe.run(function(err) { + assert.isTrue(err.indexOf('Axe is already running') !== -1); + done(); + }); + }); + describe('callback', function() { it('gives errors to the first argument on the callback', function(done) { axe._runRules = function(ctxt, opt, resolve, reject) {