-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reporters: built-in report map object and results method refactor
- Loading branch information
1 parent
9991f65
commit c5a67c5
Showing
9 changed files
with
241 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Build a Pa11y reporter. | ||
* | ||
* Same as 'pa11y/lib/reporter' but reporter methods accept multiple arguments | ||
* @private | ||
* @param {Object} methods - The reporter methods. | ||
* @returns {Promise} Returns a promise which resolves with the new reporter. | ||
*/ | ||
module.exports = function buildReporter(methods) { | ||
const reporter = { | ||
report: new Map(), | ||
supports: methods.supports, | ||
beforeAll: buildReporterMethod(methods.beforeAll), | ||
afterAll: buildReporterMethod(methods.afterAll), | ||
begin: buildReporterMethod(methods.begin), | ||
results: buildReporterMethod(methods.results), | ||
log: { | ||
debug: buildReporterMethod(methods.debug), | ||
error: buildReporterMethod(methods.error, 'error'), | ||
info: buildReporterMethod(methods.info) | ||
} | ||
}; | ||
|
||
return reporter; | ||
}; | ||
|
||
/** | ||
* Build a Pa11y reporter method, making it async and only outputting when | ||
* actual output is returned. | ||
* @private | ||
* @param {Function} method - The reporter method to build. | ||
* @param {String} [consoleMethod='log'] - The console method to use in reporting. | ||
* @returns {Function} Returns a built async reporter method. | ||
*/ | ||
function buildReporterMethod(method, consoleMethod = 'log') { | ||
if (typeof method !== 'function') { | ||
return () => Promise.resolve(); | ||
} | ||
return async (...args) => { | ||
const output = await method(...args); | ||
if (output) { | ||
console[consoleMethod](output); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const buildReporter = require('./reporter'); | ||
|
||
module.exports = function resolveReporters(reporters) { | ||
return [].concat(reporters).map(reporter => { | ||
if (typeof reporter !== 'string') { | ||
return undefined; | ||
} | ||
try { | ||
return require(reporter); | ||
} catch (_) { | ||
const localModule = path.isAbsolute(reporter) ? | ||
reporter : path.resolve(process.cwd(), reporter); | ||
if (fs.existsSync(localModule)) { | ||
return require(localModule); | ||
} | ||
console.error(`Unable to locale reporter "${reporter}"`); | ||
return undefined; | ||
} | ||
}).filter(Boolean).map(reporterModule => { | ||
return buildReporter(reporterModule); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* eslint max-len: 'off' */ | ||
'use strict'; | ||
|
||
const assert = require('proclaim'); | ||
// Const mockery = require('mockery'); | ||
const sinon = require('sinon'); | ||
|
||
describe('lib/helpers/reporter', () => { | ||
describe('buildReporter', () => { | ||
let methods; | ||
const buildReporter = require('../../../lib/helpers/reporter'); | ||
|
||
|
||
before(() => { | ||
sinon.stub(console, 'log'); | ||
}); | ||
|
||
after(() => { | ||
console.log.restore(); | ||
}); | ||
|
||
|
||
beforeEach(() => { | ||
methods = { | ||
supports: '__MOCK__', | ||
beforeAll: sinon.stub(), | ||
afterAll: sinon.stub(), | ||
begin: sinon.stub(), | ||
results: sinon.stub(), | ||
debug: sinon.stub(), | ||
error: sinon.stub(), | ||
info: sinon.stub() | ||
}; | ||
}); | ||
it('adds a "report" property', () => { | ||
const reporter = buildReporter(methods); | ||
assert.isInstanceOf(reporter.report, Map); | ||
}); | ||
|
||
it('wraps function methods in async functions', () => { | ||
const reporter = buildReporter(methods); | ||
assert.isFunction(reporter.beforeAll); | ||
assert.isFunction(reporter.afterAll); | ||
assert.isFunction(reporter.begin); | ||
assert.isFunction(reporter.results); | ||
assert.isFunction(reporter.log.debug); | ||
assert.isFunction(reporter.log.error); | ||
assert.isFunction(reporter.log.info); | ||
assert.isNotFunction(reporter.support); | ||
|
||
assert.isInstanceOf(reporter.beforeAll(), Promise); | ||
assert.isInstanceOf(reporter.afterAll(), Promise); | ||
assert.isInstanceOf(reporter.begin(), Promise); | ||
assert.isInstanceOf(reporter.results(), Promise); | ||
assert.isInstanceOf(reporter.log.debug(), Promise); | ||
assert.isInstanceOf(reporter.log.error(), Promise); | ||
assert.isInstanceOf(reporter.log.info(), Promise); | ||
}); | ||
|
||
it('wrapped function passes its argument to inner function', () => { | ||
const reporter = buildReporter(methods); | ||
const args = ['payload', {}]; | ||
for (const method of ['beforeAll', 'afterAll', 'begin', 'results']) { | ||
reporter[method](...args); | ||
assert.calledWith(methods[method], ...args); | ||
} | ||
|
||
for (const method of ['debug', 'error', 'info']) { | ||
reporter.log[method](...args); | ||
assert.calledWith(methods[method], ...args); | ||
} | ||
}); | ||
|
||
it('methods returning truthy values will be logged to console', async () => { | ||
const reporter = buildReporter(methods); | ||
methods.begin.returns('__MOCK__'); | ||
methods.results.returns(false); | ||
|
||
await reporter.begin(); | ||
assert.calledWith(console.log, '__MOCK__'); | ||
|
||
console.log.resetHistory(); | ||
|
||
await reporter.results(); | ||
assert.notCalled(console.log); | ||
|
||
}); | ||
}); | ||
|
||
}); |
Oops, something went wrong.