Skip to content

Commit

Permalink
extract runReporter to capture output under tests
Browse files Browse the repository at this point in the history
  • Loading branch information
outsideris committed Oct 21, 2018
1 parent ceff283 commit 98cfe50
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 274 deletions.
43 changes: 14 additions & 29 deletions test/reporters/doc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ var reporters = require('../../').reporters;
var Doc = reporters.Doc;

var createMockRunner = require('./helpers.js').createMockRunner;
var createRunReporterFunction = require('./helpers.js')
.createRunReporterFunction;

describe('Doc reporter', function() {
var stdout;
var stdoutWrite;
var runner;
beforeEach(function() {
stdout = [];
stdoutWrite = process.stdout.write;
process.stdout.write = function(string, enc, callback) {
stdout.push(string);
stdoutWrite.call(process.stdout, string, enc, callback);
};
});
var showOutput = false;
var runReporter = createRunReporterFunction(Doc, showOutput);

afterEach(function() {
process.stdout.write = stdoutWrite;
runner = undefined;
});

describe('on suite', function() {
Expand All @@ -32,8 +26,7 @@ describe('Doc reporter', function() {
};
it('should log html with indents and expected title', function() {
runner = createMockRunner('suite', 'suite', null, null, suite);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [
' <section class="suite">\n',
' <h1>' + expectedTitle + '</h1>\n',
Expand All @@ -48,8 +41,7 @@ describe('Doc reporter', function() {
};
expectedTitle = '&#x3C;div&#x3E;' + expectedTitle + '&#x3C;/div&#x3E;';
runner = createMockRunner('suite', 'suite', null, null, suite);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [
' <section class="suite">\n',
' <h1>' + expectedTitle + '</h1>\n',
Expand All @@ -64,8 +56,7 @@ describe('Doc reporter', function() {
};
it('should not log any html', function() {
runner = createMockRunner('suite', 'suite', null, null, suite);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
expect(stdout, 'to be empty');
});
});
Expand All @@ -78,8 +69,7 @@ describe('Doc reporter', function() {
};
it('should log expected html with indents', function() {
runner = createMockRunner('suite end', 'suite end', null, null, suite);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [' </dl>\n', '</section>\n'];
expect(stdout, 'to equal', expectedArray);
});
Expand All @@ -90,8 +80,7 @@ describe('Doc reporter', function() {
};
it('should not log any html', function() {
runner = createMockRunner('suite end', 'suite end', null, null, suite);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
expect(stdout, 'to be empty');
});
});
Expand All @@ -109,8 +98,7 @@ describe('Doc reporter', function() {
};
it('should log html with indents and expected title and body', function() {
runner = createMockRunner('pass', 'pass', null, null, test);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [
' <dt>' + expectedTitle + '</dt>\n',
' <dd><pre><code>' + expectedBody + '</code></pre></dd>\n'
Expand All @@ -128,8 +116,7 @@ describe('Doc reporter', function() {
var expectedEscapedBody =
'&#x3C;div&#x3E;' + expectedBody + '&#x3C;/div&#x3E;';
runner = createMockRunner('pass', 'pass', null, null, test);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [
' <dt>' + expectedEscapedTitle + '</dt>\n',
' <dd><pre><code>' + expectedEscapedBody + '</code></pre></dd>\n'
Expand Down Expand Up @@ -158,8 +145,7 @@ describe('Doc reporter', function() {
test,
expectedError
);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [
' <dt class="error">' + expectedTitle + '</dt>\n',
' <dd class="error"><pre><code>' +
Expand Down Expand Up @@ -190,8 +176,7 @@ describe('Doc reporter', function() {
test,
unescapedError
);
Doc.call(this, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
var expectedArray = [
' <dt class="error">' + expectedEscapedTitle + '</dt>\n',
' <dd class="error"><pre><code>' +
Expand Down
50 changes: 13 additions & 37 deletions test/reporters/dot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,18 @@ var Dot = reporters.Dot;
var Base = reporters.Base;

var createMockRunner = require('./helpers.js').createMockRunner;
var createRunReporterFunction = require('./helpers.js')
.createRunReporterFunction;

describe('Dot reporter', function() {
var stdout;
var runner;
var useColors;
var windowWidth;
var color;
var showOutput = false;

/**
* Run reporter using stream reassignment to capture output.
*
* @param {Object} stubSelf - Reporter-like stub instance
* @param {Runner} runner - Mock instance
* @param {boolean} [tee=false] - If `true`, echo captured output to screen
*/
function runReporter(stubSelf, runner, tee) {
// 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
Dot.call(stubSelf, runner);

// Revert stream reassignment here so reporter output
// can't be corrupted if any test assertions throw
process.stdout.write = stdoutWrite;
}
var runReporter = createRunReporterFunction(Dot, showOutput);

beforeEach(function() {
stdout = [];
useColors = Base.useColors;
windowWidth = Base.window.width;
color = Base.color;
Expand All @@ -61,7 +37,7 @@ describe('Dot reporter', function() {
describe('on start', function() {
it('should write a newline', function() {
runner = createMockRunner('start', 'start');
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
var expectedArray = ['\n'];
expect(stdout, 'to equal', expectedArray);
});
Expand All @@ -73,15 +49,15 @@ describe('Dot reporter', function() {
});
it('should write a newline followed by a comma', function() {
runner = createMockRunner('pending', 'pending');
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
var expectedArray = ['\n ', 'pending_' + Base.symbols.comma];
expect(stdout, 'to equal', expectedArray);
});
});
describe('if window width is equal to or less than 1', function() {
it('should write a comma', function() {
runner = createMockRunner('pending', 'pending');
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
var expectedArray = ['pending_' + Base.symbols.comma];
expect(stdout, 'to equal', expectedArray);
});
Expand All @@ -101,7 +77,7 @@ describe('Dot reporter', function() {
describe('if test speed is fast', function() {
it('should write a newline followed by a dot', function() {
runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
expect(test.speed, 'to equal', 'fast');
var expectedArray = ['\n ', 'fast_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
Expand All @@ -112,7 +88,7 @@ describe('Dot reporter', function() {
describe('if test speed is fast', function() {
it('should write a grey dot', function() {
runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
expect(test.speed, 'to equal', 'fast');
var expectedArray = ['fast_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
Expand All @@ -122,7 +98,7 @@ describe('Dot reporter', function() {
it('should write a yellow dot', function() {
test.duration = 2;
runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
expect(test.speed, 'to equal', 'medium');
var expectedArray = ['medium_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
Expand All @@ -132,7 +108,7 @@ describe('Dot reporter', function() {
it('should write a bright yellow dot', function() {
test.duration = 3;
runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
expect(test.speed, 'to equal', 'slow');
var expectedArray = ['bright-yellow_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
Expand All @@ -152,15 +128,15 @@ describe('Dot reporter', function() {
});
it('should write a newline followed by an exclamation mark', function() {
runner = createMockRunner('fail', 'fail', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
var expectedArray = ['\n ', 'fail_' + Base.symbols.bang];
expect(stdout, 'to equal', expectedArray);
});
});
describe('if window width is equal to or less than 1', function() {
it('should write an exclamation mark', function() {
runner = createMockRunner('fail', 'fail', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
var stdout = runReporter({epilogue: function() {}}, runner);
var expectedArray = ['fail_' + Base.symbols.bang];
expect(stdout, 'to equal', expectedArray);
});
Expand All @@ -173,7 +149,7 @@ describe('Dot reporter', function() {
var epilogue = function() {
epilogueCalled = true;
};
runReporter({epilogue: epilogue}, runner, showOutput);
runReporter({epilogue: epilogue}, runner);
expect(epilogueCalled, 'to be', true);
});
});
Expand Down
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
* @param {boolean} [tee=false] - Whether to echo output to screen
* @return {createRunReporterFunction~runReporter}
*/
function createRunReporterFunction(ctor, tee) {
/**
* 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
* @return {string[]} Lines of output written to `stdout`
*/
var runReporter = function(stubSelf, runner, options) {
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
};
38 changes: 11 additions & 27 deletions test/reporters/json-stream.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ var JSONStream = reporters.JSONStream;

var createMockRunner = require('./helpers').createMockRunner;
var makeExpectedTest = require('./helpers').makeExpectedTest;
var createRunReporterFunction = require('./helpers.js')
.createRunReporterFunction;

describe('Json Stream reporter', function() {
describe('JSON Stream reporter', function() {
var runner;
var stdout;
var stdoutWrite;

var showOutput = false;
var runReporter = createRunReporterFunction(JSONStream, showOutput);
var expectedTitle = 'some title';
var expectedFullTitle = 'full title';
var expectedDuration = 1000;
Expand All @@ -27,27 +28,16 @@ describe('Json Stream reporter', function() {
message: expectedErrorMessage
};

beforeEach(function() {
stdout = [];
stdoutWrite = process.stdout.write;
process.stdout.write = function(string, enc, callback) {
stdout.push(string);
stdoutWrite.call(process.stdout, string, enc, callback);
};
});

afterEach(function() {
process.stdout.write = stdoutWrite;
runner = undefined;
});

describe('on start', function() {
it('should write stringified start with expected total', function() {
runner = createMockRunner('start', 'start');
var expectedTotal = 12;
runner.total = expectedTotal;
JSONStream.call({}, runner);

process.stdout.write = stdoutWrite;
var stdout = runReporter({}, runner);

expect(
stdout[0],
Expand All @@ -60,9 +50,7 @@ describe('Json Stream reporter', function() {
describe('on pass', function() {
it('should write stringified test data', function() {
runner = createMockRunner('pass', 'pass', null, null, expectedTest);
JSONStream.call({}, runner);

process.stdout.write = stdoutWrite;
var stdout = runReporter({}, runner);

expect(
stdout[0],
Expand Down Expand Up @@ -93,9 +81,7 @@ describe('Json Stream reporter', function() {
expectedError
);

JSONStream.call({}, runner);

process.stdout.write = stdoutWrite;
var stdout = runReporter({}, runner);

expect(
stdout[0],
Expand Down Expand Up @@ -129,8 +115,7 @@ describe('Json Stream reporter', function() {
expectedError
);

JSONStream.call({}, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);

expect(
stdout[0],
Expand All @@ -154,8 +139,7 @@ describe('Json Stream reporter', function() {
describe('on end', function() {
it('should write end details', function() {
runner = createMockRunner('end', 'end');
JSONStream.call({}, runner);
process.stdout.write = stdoutWrite;
var stdout = runReporter(this, runner);
expect(stdout[0], 'to match', /end/);
});
});
Expand Down
Loading

0 comments on commit 98cfe50

Please sign in to comment.