Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix slow test for dot reporter #3486

Merged
merged 1 commit into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/reporters/dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

var Base = require('./base');
var inherits = require('../utils').inherits;
var color = Base.color;

/**
* Expose `Dot`.
Expand Down Expand Up @@ -41,25 +40,25 @@ function Dot(runner) {
if (++n % width === 0) {
process.stdout.write('\n ');
}
process.stdout.write(color('pending', Base.symbols.comma));
process.stdout.write(Base.color('pending', Base.symbols.comma));
});

runner.on('pass', function(test) {
if (++n % width === 0) {
process.stdout.write('\n ');
}
if (test.speed === 'slow') {
process.stdout.write(color('bright yellow', Base.symbols.dot));
process.stdout.write(Base.color('bright yellow', Base.symbols.dot));
} else {
process.stdout.write(color(test.speed, Base.symbols.dot));
process.stdout.write(Base.color(test.speed, Base.symbols.dot));
}
});

runner.on('fail', function() {
if (++n % width === 0) {
process.stdout.write('\n ');
}
process.stdout.write(color('fail', Base.symbols.bang));
process.stdout.write(Base.color('fail', Base.symbols.bang));
});

runner.once('end', function() {
Expand Down
110 changes: 70 additions & 40 deletions test/reporters/dot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,60 @@ var createMockRunner = require('./helpers.js').createMockRunner;

describe('Dot reporter', function() {
var stdout;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  var showOutput = false;

var stdoutWrite;
var runner;
var useColors;
var windowWidth;
var color;
var showOutput = false;

Copy link
Contributor

@plroebuck plroebuck Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity of comparison, it might be easier not to be so literal.

function speedString(type, str) {
  return '##' + type.replace(/ /g, '-') + '_' + str;
};

Base.color = speedString;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  /**
   * 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
   */

beforeEach(function() {
stdout = [];
stdoutWrite = process.stdout.write;
/**
* 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);
stdoutWrite.call(process.stdout, string, enc, callback);
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;
}

beforeEach(function() {
stdout = [];
useColors = Base.useColors;
windowWidth = Base.window.width;
color = Base.color;
Base.useColors = false;
Base.window.width = 0;
Base.color = function(type, str) {
return type.replace(/ /g, '-') + '_' + str;
};
});

afterEach(function() {
Base.useColors = useColors;
Base.window.width = windowWidth;
process.stdout.write = stdoutWrite;
Base.color = color;
runner = undefined;
});

describe('on start', function() {
it('should return a new line', function() {
it('should write a newline', function() {
runner = createMockRunner('start', 'start');
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
runReporter({epilogue: function() {}}, runner, showOutput);
var expectedArray = ['\n'];
expect(stdout, 'to equal', expectedArray);
});
Expand All @@ -46,20 +71,18 @@ describe('Dot reporter', function() {
beforeEach(function() {
Base.window.width = 2;
});
it('should return a new line and then a coma', function() {
it('should write a newline followed by a comma', function() {
runner = createMockRunner('pending', 'pending');
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = ['\n ', Base.symbols.comma];
runReporter({epilogue: function() {}}, runner, showOutput);
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 return a coma', function() {
it('should write a comma', function() {
runner = createMockRunner('pending', 'pending');
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = [Base.symbols.comma];
runReporter({epilogue: function() {}}, runner, showOutput);
var expectedArray = ['pending_' + Base.symbols.comma];
expect(stdout, 'to equal', expectedArray);
});
});
Expand All @@ -76,32 +99,42 @@ describe('Dot reporter', function() {
Base.window.width = 2;
});
describe('if test speed is fast', function() {
it('should return a new line and then a dot', function() {
it('should write a newline followed by a dot', function() {
runner = createMockRunner('pass', 'pass', null, null, test);
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = ['\n ', Base.symbols.dot];
runReporter({epilogue: function() {}}, runner, showOutput);
expect(test.speed, 'to equal', 'fast');
var expectedArray = ['\n ', 'fast_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
});
});
});
describe('if window width is equal to or less than 1', function() {
describe('if test speed is fast', function() {
it('should return a dot', function() {
it('should write a grey dot', function() {
runner = createMockRunner('pass', 'pass', null, null, test);
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = [Base.symbols.dot];
runReporter({epilogue: function() {}}, runner, showOutput);
expect(test.speed, 'to equal', 'fast');
var expectedArray = ['fast_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
});
});
describe('if test speed is slow', function() {
it('should return a dot', function() {
describe('if test speed is medium', function() {
it('should write a yellow dot', function() {
test.duration = 2;
runner = createMockRunner('pass', 'pass', null, null, test);
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = [Base.symbols.dot];
runReporter({epilogue: function() {}}, runner, showOutput);
expect(test.speed, 'to equal', 'medium');
var expectedArray = ['medium_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
});
});
describe('if test speed is slow', function() {
it('should write a bright yellow dot', function() {
test.duration = 3;
runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, showOutput);
expect(test.speed, 'to equal', 'slow');
var expectedArray = ['bright-yellow_' + Base.symbols.dot];
expect(stdout, 'to equal', expectedArray);
});
});
Expand All @@ -117,20 +150,18 @@ describe('Dot reporter', function() {
beforeEach(function() {
Base.window.width = 2;
});
it('should return a new line and then an exclamation mark', function() {
it('should write a newline followed by an exclamation mark', function() {
runner = createMockRunner('fail', 'fail', null, null, test);
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = ['\n ', Base.symbols.bang];
runReporter({epilogue: function() {}}, runner, showOutput);
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 return an exclamation mark', function() {
it('should write an exclamation mark', function() {
runner = createMockRunner('fail', 'fail', null, null, test);
Dot.call({epilogue: function() {}}, runner);
process.stdout.write = stdoutWrite;
var expectedArray = [Base.symbols.bang];
runReporter({epilogue: function() {}}, runner, showOutput);
var expectedArray = ['fail_' + Base.symbols.bang];
expect(stdout, 'to equal', expectedArray);
});
});
Expand All @@ -142,8 +173,7 @@ describe('Dot reporter', function() {
var epilogue = function() {
epilogueCalled = true;
};
Dot.call({epilogue: epilogue}, runner);
process.stdout.write = stdoutWrite;
runReporter({epilogue: epilogue}, runner, showOutput);
expect(epilogueCalled, 'to be', true);
});
});
Expand Down