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

spec-reporter prints retries #5099

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Empty file modified bin/mocha.js 100644 → 100755
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
30 changes: 22 additions & 8 deletions lib/reporters/spec.js
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -45,46 +45,60 @@ function Spec(runner, options) {
return Array(indents).join(' ');
}

runner.on(EVENT_RUN_BEGIN, function () {
runner.on(EVENT_RUN_BEGIN, function() {
Base.consoleLog();
});

runner.on(EVENT_SUITE_BEGIN, function (suite) {
runner.on(EVENT_SUITE_BEGIN, function(suite) {
++indents;
Base.consoleLog(color('suite', '%s%s'), indent(), suite.title);
});

runner.on(EVENT_SUITE_END, function () {
runner.on(EVENT_SUITE_END, function() {
--indents;
if (indents === 1) {
Base.consoleLog();
}
});

runner.on(EVENT_TEST_PENDING, function (test) {
runner.on(EVENT_TEST_PENDING, function(test) {
var fmt = indent() + color('pending', ' - %s');
Base.consoleLog(fmt, test.title);
});

runner.on(EVENT_TEST_PASS, function (test) {
runner.on(EVENT_TEST_PASS, function(test) {
function logRetries() {
if (test._currentRetry > 0) {
/*
Included message within format so it can be passed into `Base.consoleLog` as one argument
*/
var retryFmt = color(
'bright yellow',
` (retry x${test._currentRetry})`
);
return retryFmt;
} else {
return '';
}
}
var fmt;
if (test.speed === 'fast') {
fmt =
indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s');
Base.consoleLog(fmt, test.title);
Base.consoleLog(fmt, test.title, logRetries());
} else {
fmt =
indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
color(test.speed, ' (%dms)');
Base.consoleLog(fmt, test.title, test.duration);
Base.consoleLog(fmt, test.title, test.duration, logRetries());
}
});

runner.on(EVENT_TEST_FAIL, function (test) {
runner.on(EVENT_TEST_FAIL, function(test) {
Base.consoleLog(indent() + color('fail', ' %d) %s'), ++n, test.title);
});

Expand Down
63 changes: 48 additions & 15 deletions test/integration/retries.spec.js
Expand Up @@ -6,12 +6,12 @@ var runJSON = helpers.runMochaJSON;
var args = [];
var bang = require('../../lib/reporters/base').symbols.bang;

describe('retries', function () {
it('are ran in correct order', function (done) {
describe('retries', function() {
it('are ran in correct order', function(done) {
helpers.runMocha(
'retries/hooks.fixture.js',
['--reporter', 'dot'],
function (err, res) {
function(err, res) {
var lines, expected;

if (err) {
Expand All @@ -21,10 +21,10 @@ describe('retries', function () {

lines = res.output
.split(helpers.SPLIT_DOT_REPORTER_REGEXP)
.map(function (line) {
.map(function(line) {
return line.trim();
})
.filter(function (line) {
.filter(function(line) {
return line.length;
})
.slice(0, -1);
Expand All @@ -49,7 +49,7 @@ describe('retries', function () {
'after'
];

expected.forEach(function (line, i) {
expected.forEach(function(line, i) {
assert.strictEqual(lines[i], line);
});

Expand All @@ -59,8 +59,8 @@ describe('retries', function () {
);
});

it('should exit early if test passes', function (done) {
runJSON('retries/early-pass.fixture.js', args, function (err, res) {
it('should exit early if test passes', function(done) {
runJSON('retries/early-pass.fixture.js', args, function(err, res) {
if (err) {
return done(err);
}
Expand All @@ -74,8 +74,8 @@ describe('retries', function () {
});
});

it('should let test override', function (done) {
runJSON('retries/nested.fixture.js', args, function (err, res) {
it('should let test override', function(done) {
runJSON('retries/nested.fixture.js', args, function(err, res) {
if (err) {
done(err);
return;
Expand All @@ -89,11 +89,11 @@ describe('retries', function () {
});
});

it('should not hang w/ async test', function (done) {
it('should not hang w/ async test', function(done) {
helpers.runMocha(
'retries/async.fixture.js',
['--reporter', 'dot'],
function (err, res) {
function(err, res) {
var lines, expected;

if (err) {
Expand All @@ -103,10 +103,10 @@ describe('retries', function () {

lines = res.output
.split(helpers.SPLIT_DOT_REPORTER_REGEXP)
.map(function (line) {
.map(function(line) {
return line.trim();
})
.filter(function (line) {
.filter(function(line) {
return line.length;
})
.slice(0, -1);
Expand All @@ -125,7 +125,7 @@ describe('retries', function () {
'after'
];

expected.forEach(function (line, i) {
expected.forEach(function(line, i) {
assert.strictEqual(lines[i], line);
});

Expand All @@ -134,4 +134,37 @@ describe('retries', function () {
}
);
});

it('should return current retry count out of total retry count', function(done) {
helpers.runMocha(
'retries/early-pass.fixture.js',
['--reporter', 'spec'],
function(err, res) {
var lines, expected;

if (err) {
done(err);
return;
}

lines = res.output
.split(helpers.SPLIT_DOT_REPORTER_REGEXP)
.map(function(line) {
return line.trim();
})
.filter(function(line) {
return line.length;
})
.slice(0, -1);

var retryPattern = /\(retry x\d+\)/;

var actual = lines[1].match(retryPattern)[0];
var expected = '(retry x1)';

assert.equal(actual, expected);
done();
}
);
});
});
50 changes: 25 additions & 25 deletions test/reporters/spec.spec.js
Expand Up @@ -15,22 +15,22 @@ var EVENT_TEST_FAIL = events.EVENT_TEST_FAIL;
var EVENT_TEST_PASS = events.EVENT_TEST_PASS;
var EVENT_TEST_PENDING = events.EVENT_TEST_PENDING;

describe('Spec reporter', function () {
describe('Spec reporter', function() {
var runReporter = makeRunReporter(Spec);
var expectedTitle = 'expectedTitle';
var noop = function () {};
var noop = function() { };

beforeEach(function () {
beforeEach(function() {
sinon.stub(Base, 'useColors').value(false);
});

afterEach(function () {
afterEach(function() {
sinon.restore();
});

describe('event handlers', function () {
describe("on 'suite' event", function () {
it('should return title', function () {
describe('event handlers', function() {
describe("on 'suite' event", function() {
it('should return title', function() {
var suite = {
title: expectedTitle
};
Expand All @@ -42,16 +42,16 @@ describe('Spec reporter', function () {
suite
);
var options = {};
var stdout = runReporter({epilogue: noop}, runner, options);
var stdout = runReporter({ epilogue: noop }, runner, options);
sinon.restore();

var expectedArray = [expectedTitle + '\n'];
expect(stdout, 'to equal', expectedArray);
});
});

describe("on 'pending' event", function () {
it('should return title', function () {
describe("on 'pending' event", function() {
it('should return title', function() {
var suite = {
title: expectedTitle
};
Expand All @@ -63,22 +63,22 @@ describe('Spec reporter', function () {
suite
);
var options = {};
var stdout = runReporter({epilogue: noop}, runner, options);
var stdout = runReporter({ epilogue: noop }, runner, options);
sinon.restore();

var expectedArray = [' - ' + expectedTitle + '\n'];
expect(stdout, 'to equal', expectedArray);
});
});

describe("on 'pass' event", function () {
describe('when test speed is slow', function () {
it('should return expected tick, title, and duration', function () {
describe("on 'pass' event", function() {
describe('when test speed is slow', function() {
it('should return expected tick, title, and duration', function() {
var expectedDuration = 2;
var test = {
title: expectedTitle,
duration: expectedDuration,
slow: function () {
slow: function() {
return 1;
}
};
Expand All @@ -90,7 +90,7 @@ describe('Spec reporter', function () {
test
);
var options = {};
var stdout = runReporter({epilogue: noop}, runner, options);
var stdout = runReporter({ epilogue: noop }, runner, options);
sinon.restore();

var expectedString =
Expand All @@ -101,18 +101,18 @@ describe('Spec reporter', function () {
' (' +
expectedDuration +
'ms)' +
'\n';
' \n';
expect(stdout[0], 'to be', expectedString);
});
});

describe('when test speed is fast', function () {
it('should return expected tick, title without a duration', function () {
describe('when test speed is fast', function() {
it('should return expected tick, title without a duration', function() {
var expectedDuration = 1;
var test = {
title: expectedTitle,
duration: expectedDuration,
slow: function () {
slow: function() {
return 2;
}
};
Expand All @@ -124,18 +124,18 @@ describe('Spec reporter', function () {
test
);
var options = {};
var stdout = runReporter({epilogue: noop}, runner, options);
var stdout = runReporter({ epilogue: noop }, runner, options);
sinon.restore();

var expectedString =
' ' + Base.symbols.ok + ' ' + expectedTitle + '\n';
' ' + Base.symbols.ok + ' ' + expectedTitle + ' \n';
expect(stdout[0], 'to be', expectedString);
});
});
});

describe("on 'fail' event", function () {
it('should return title and function count', function () {
describe("on 'fail' event", function() {
it('should return title and function count', function() {
var functionCount = 1;
var test = {
title: expectedTitle
Expand All @@ -148,7 +148,7 @@ describe('Spec reporter', function () {
test
);
var options = {};
var stdout = runReporter({epilogue: noop}, runner, options);
var stdout = runReporter({ epilogue: noop }, runner, options);
sinon.restore();

var expectedArray = [
Expand Down