Skip to content

Commit

Permalink
test(reporters/helpers.js): Add update to "helpers.js" from PR #3528
Browse files Browse the repository at this point in the history
Tried this via rebase, but new function was missing afterwards.
  • Loading branch information
plroebuck committed Nov 6, 2018
1 parent c179a34 commit df03a00
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions test/reporters/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,51 @@ function makeExpectedTest(
};
}

/**
* Creates closure with reference to the reporter class constructor.
*
* @param {Function} ctor - Reporter class constructor
* @return {createRunReporterFunction~runReporter}
*/
function createRunReporterFunction(ctor) {
/**
* Run reporter using stream reassignment to capture output.
*
* @param {Object} stubSelf - Reporter-like stub instance
* @param {Runner} runner - Mock instance
* @param {Object} [options] - Reporter configuration settings
* @param {boolean} [tee=false] - Whether to echo output to screen
* @return {string[]} Lines of output written to `stdout`
*/
var runReporter = function(stubSelf, runner, options, tee) {
var stdout = [];

// Reassign stream in order to make a copy of all reporter output
var stdoutWrite = process.stdout.write;
process.stdout.write = function(string, enc, callback) {
stdout.push(string);
if (tee) {
stdoutWrite.call(process.stdout, string, enc, callback);
}
};

// Invoke reporter
ctor.call(stubSelf, runner, options);

// Revert stream reassignment here so reporter output
// can't be corrupted if any test assertions throw
process.stdout.write = stdoutWrite;

return stdout;
};

return runReporter;
}

module.exports = {
createMockRunner: createMockRunner,
makeTest: makeTest,
createElements: createElements,
makeExpectedTest: makeExpectedTest
createMockRunner: createMockRunner,
createRunReporterFunction: createRunReporterFunction,
makeExpectedTest: makeExpectedTest,
makeTest: makeTest
};

0 comments on commit df03a00

Please sign in to comment.