Skip to content

Commit

Permalink
fix(run): throw error if axe.run is called after a run has started bu…
Browse files Browse the repository at this point in the history
…t not completed (#1914)

* fix(run): throw error if axe.run is called after a run has started but not completed

* remove comment
  • Loading branch information
straker committed Nov 21, 2019
1 parent da03ca3 commit 3252a02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/core/public/run.js
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions test/core/public/run.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3252a02

Please sign in to comment.