Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
feat: add option to not print iframe error to console
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Jul 3, 2019
1 parent cddebec commit b4f2494
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
28 changes: 22 additions & 6 deletions lib/axe-injector.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class AxeInjector {
constructor({ driver, config, axeSource = null }) {
constructor({ driver, config, axeSource = null, options = {} }) {
this.driver = driver;
this.axeSource = axeSource || require('axe-core').source;
this.config = config ? JSON.stringify(config) : '';
this.options = options;

// default stdout to true so it retains the original behavior
this.options.stdout =
typeof this.options.stdout !== 'undefined' ? this.options.stdout : true;

this.didLogError = false;
this.errorHandler = this.errorHandler.bind(this);
Expand All @@ -16,8 +21,15 @@ class AxeInjector {
}

this.didLogError = true;
// eslint-disable-next-line no-console
console.error('Failed to inject axe-core into one of the iframes!');
const msg = 'Failed to inject axe-core into one of the iframes!';

if (this.options.stdout) {
// eslint-disable-next-line no-console
console.error(msg);
return;
}

throw new Error(msg);
}

// Get axe-core source (and configuration)
Expand Down Expand Up @@ -73,9 +85,13 @@ class AxeInjector {
inject(callback) {
this.injectIntoAllFrames()
.then(() => callback())
// For now, we intentionally ignore errors here, as
// allowing them to bubble up would be a breaking change.
.catch(() => callback());
.catch(e => {
if (this.options.stdout) {
return callback();
}

callback(e);
});
}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var normalizeContext = require('./normalize-context');
* Constructor for chainable WebDriver API
* @param {WebDriver} driver WebDriver instance to analyze
*/
function AxeBuilder(driver, source) {
function AxeBuilder(driver, source, builderOptions = {}) {
if (!(this instanceof AxeBuilder)) {
return new AxeBuilder(driver, source);
}
Expand All @@ -17,6 +17,7 @@ function AxeBuilder(driver, source) {
this._excludes = [];
this._options = null;
this._config = null;
this._builderOptions = builderOptions;
}

/**
Expand Down Expand Up @@ -141,7 +142,12 @@ AxeBuilder.prototype.analyze = function(callback) {
}

return new Promise((resolve, reject) => {
var injector = new AxeInjector({ driver, axeSource: source, config });
var injector = new AxeInjector({
driver,
axeSource: source,
config,
options: this._builderOptions
});
injector.inject(() => {
driver
.executeAsyncScript(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions test/unit/axe-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ describe('AxeInjector', () => {

spy.restore();
});

it('throws the error if stdout is set to false', () => {
const injector = new AxeInjector({
driver: new MockWebDriver(),
options: { stdout: false }
});

assert.throws(injector.errorHandler);
});
});

describe('script', () => {
Expand Down Expand Up @@ -215,5 +224,23 @@ describe('AxeInjector', () => {

injector.inject(done);
});

it('passes the error if stdout is set to false', done => {
const injector = new AxeInjector({
driver: new MockWebDriver(),
options: { stdout: false }
});
const stub = sinon.stub(injector, 'injectIntoAllFrames');

stub.rejects(new Error('oh no!'));

injector.inject(err => {
if (err) {
return done();
}

done('injector did not pass error to callback');
});
});
});
});

0 comments on commit b4f2494

Please sign in to comment.