Skip to content

Commit 26881ee

Browse files
refactor retries
1 parent b5d4d68 commit 26881ee

File tree

3 files changed

+68
-66
lines changed

3 files changed

+68
-66
lines changed

lib/reporters/spec.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,40 @@ function Spec(runner, options) {
4545
return Array(indents).join(' ');
4646
}
4747

48-
runner.on(EVENT_RUN_BEGIN, function () {
48+
runner.on(EVENT_RUN_BEGIN, function() {
4949
Base.consoleLog();
5050
});
5151

52-
runner.on(EVENT_SUITE_BEGIN, function (suite) {
52+
runner.on(EVENT_SUITE_BEGIN, function(suite) {
5353
++indents;
5454
Base.consoleLog(color('suite', '%s%s'), indent(), suite.title);
5555
});
5656

57-
runner.on(EVENT_SUITE_END, function () {
57+
runner.on(EVENT_SUITE_END, function() {
5858
--indents;
5959
if (indents === 1) {
6060
Base.consoleLog();
6161
}
6262
});
6363

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

69-
runner.on(EVENT_TEST_PASS, function (test) {
69+
runner.on(EVENT_TEST_PASS, function(test) {
7070
function logRetries() {
71-
if (test._retries > 0) {
72-
var retryFmt = indent() + color('bright yellow', ' %s');
73-
var retryMessage =
74-
test._currentRetry > 0
75-
? `(Succeeded after ${test._currentRetry} / ${test._retries} retries)`
76-
: '';
77-
78-
if (retryMessage) {
79-
Base.consoleLog(retryFmt, retryMessage);
80-
}
71+
if (test._currentRetry > 0) {
72+
/*
73+
Included message within format so it can be passed into `Base.consoleLog` as one argument
74+
*/
75+
var retryFmt = color(
76+
'bright yellow',
77+
` (retry x${test._currentRetry})`
78+
);
79+
return retryFmt;
80+
} else {
81+
return '';
8182
}
8283
}
8384
var fmt;
@@ -86,20 +87,18 @@ function Spec(runner, options) {
8687
indent() +
8788
color('checkmark', ' ' + Base.symbols.ok) +
8889
color('pass', ' %s');
89-
Base.consoleLog(fmt, test.title);
90-
logRetries();
90+
Base.consoleLog(fmt, test.title, logRetries());
9191
} else {
9292
fmt =
9393
indent() +
9494
color('checkmark', ' ' + Base.symbols.ok) +
9595
color('pass', ' %s') +
9696
color(test.speed, ' (%dms)');
97-
Base.consoleLog(fmt, test.title, test.duration);
98-
logRetries();
97+
Base.consoleLog(fmt, test.title, test.duration, logRetries());
9998
}
10099
});
101100

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

test/integration/retries.spec.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ var runJSON = helpers.runMochaJSON;
66
var args = [];
77
var bang = require('../../lib/reporters/base').symbols.bang;
88

9-
describe('retries', function () {
10-
it('are ran in correct order', function (done) {
9+
describe('retries', function() {
10+
it('are ran in correct order', function(done) {
1111
helpers.runMocha(
1212
'retries/hooks.fixture.js',
1313
['--reporter', 'dot'],
14-
function (err, res) {
14+
function(err, res) {
1515
var lines, expected;
1616

1717
if (err) {
@@ -21,10 +21,10 @@ describe('retries', function () {
2121

2222
lines = res.output
2323
.split(helpers.SPLIT_DOT_REPORTER_REGEXP)
24-
.map(function (line) {
24+
.map(function(line) {
2525
return line.trim();
2626
})
27-
.filter(function (line) {
27+
.filter(function(line) {
2828
return line.length;
2929
})
3030
.slice(0, -1);
@@ -49,7 +49,7 @@ describe('retries', function () {
4949
'after'
5050
];
5151

52-
expected.forEach(function (line, i) {
52+
expected.forEach(function(line, i) {
5353
assert.strictEqual(lines[i], line);
5454
});
5555

@@ -59,8 +59,8 @@ describe('retries', function () {
5959
);
6060
});
6161

62-
it('should exit early if test passes', function (done) {
63-
runJSON('retries/early-pass.fixture.js', args, function (err, res) {
62+
it('should exit early if test passes', function(done) {
63+
runJSON('retries/early-pass.fixture.js', args, function(err, res) {
6464
if (err) {
6565
return done(err);
6666
}
@@ -74,8 +74,8 @@ describe('retries', function () {
7474
});
7575
});
7676

77-
it('should let test override', function (done) {
78-
runJSON('retries/nested.fixture.js', args, function (err, res) {
77+
it('should let test override', function(done) {
78+
runJSON('retries/nested.fixture.js', args, function(err, res) {
7979
if (err) {
8080
done(err);
8181
return;
@@ -89,11 +89,11 @@ describe('retries', function () {
8989
});
9090
});
9191

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

9999
if (err) {
@@ -103,10 +103,10 @@ describe('retries', function () {
103103

104104
lines = res.output
105105
.split(helpers.SPLIT_DOT_REPORTER_REGEXP)
106-
.map(function (line) {
106+
.map(function(line) {
107107
return line.trim();
108108
})
109-
.filter(function (line) {
109+
.filter(function(line) {
110110
return line.length;
111111
})
112112
.slice(0, -1);
@@ -125,7 +125,7 @@ describe('retries', function () {
125125
'after'
126126
];
127127

128-
expected.forEach(function (line, i) {
128+
expected.forEach(function(line, i) {
129129
assert.strictEqual(lines[i], line);
130130
});
131131

@@ -135,11 +135,11 @@ describe('retries', function () {
135135
);
136136
});
137137

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

145145
if (err) {
@@ -149,17 +149,20 @@ describe('retries', function () {
149149

150150
lines = res.output
151151
.split(helpers.SPLIT_DOT_REPORTER_REGEXP)
152-
.map(function (line) {
152+
.map(function(line) {
153153
return line.trim();
154154
})
155-
.filter(function (line) {
155+
.filter(function(line) {
156156
return line.length;
157157
})
158158
.slice(0, -1);
159159

160-
var expected = '(Succeeded after 1 / 1 retries)';
160+
var retryPattern = /\(retry x\d+\)/;
161161

162-
assert.equal(lines[2], expected);
162+
var actual = lines[1].match(retryPattern)[0];
163+
var expected = '(retry x1)';
164+
165+
assert.equal(actual, expected);
163166
done();
164167
}
165168
);

test/reporters/spec.spec.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ var EVENT_TEST_FAIL = events.EVENT_TEST_FAIL;
1515
var EVENT_TEST_PASS = events.EVENT_TEST_PASS;
1616
var EVENT_TEST_PENDING = events.EVENT_TEST_PENDING;
1717

18-
describe('Spec reporter', function () {
18+
describe('Spec reporter', function() {
1919
var runReporter = makeRunReporter(Spec);
2020
var expectedTitle = 'expectedTitle';
21-
var noop = function () {};
21+
var noop = function() { };
2222

23-
beforeEach(function () {
23+
beforeEach(function() {
2424
sinon.stub(Base, 'useColors').value(false);
2525
});
2626

27-
afterEach(function () {
27+
afterEach(function() {
2828
sinon.restore();
2929
});
3030

31-
describe('event handlers', function () {
32-
describe("on 'suite' event", function () {
33-
it('should return title', function () {
31+
describe('event handlers', function() {
32+
describe("on 'suite' event", function() {
33+
it('should return title', function() {
3434
var suite = {
3535
title: expectedTitle
3636
};
@@ -42,16 +42,16 @@ describe('Spec reporter', function () {
4242
suite
4343
);
4444
var options = {};
45-
var stdout = runReporter({epilogue: noop}, runner, options);
45+
var stdout = runReporter({ epilogue: noop }, runner, options);
4646
sinon.restore();
4747

4848
var expectedArray = [expectedTitle + '\n'];
4949
expect(stdout, 'to equal', expectedArray);
5050
});
5151
});
5252

53-
describe("on 'pending' event", function () {
54-
it('should return title', function () {
53+
describe("on 'pending' event", function() {
54+
it('should return title', function() {
5555
var suite = {
5656
title: expectedTitle
5757
};
@@ -63,22 +63,22 @@ describe('Spec reporter', function () {
6363
suite
6464
);
6565
var options = {};
66-
var stdout = runReporter({epilogue: noop}, runner, options);
66+
var stdout = runReporter({ epilogue: noop }, runner, options);
6767
sinon.restore();
6868

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

74-
describe("on 'pass' event", function () {
75-
describe('when test speed is slow', function () {
76-
it('should return expected tick, title, and duration', function () {
74+
describe("on 'pass' event", function() {
75+
describe('when test speed is slow', function() {
76+
it('should return expected tick, title, and duration', function() {
7777
var expectedDuration = 2;
7878
var test = {
7979
title: expectedTitle,
8080
duration: expectedDuration,
81-
slow: function () {
81+
slow: function() {
8282
return 1;
8383
}
8484
};
@@ -90,7 +90,7 @@ describe('Spec reporter', function () {
9090
test
9191
);
9292
var options = {};
93-
var stdout = runReporter({epilogue: noop}, runner, options);
93+
var stdout = runReporter({ epilogue: noop }, runner, options);
9494
sinon.restore();
9595

9696
var expectedString =
@@ -101,18 +101,18 @@ describe('Spec reporter', function () {
101101
' (' +
102102
expectedDuration +
103103
'ms)' +
104-
'\n';
104+
' \n';
105105
expect(stdout[0], 'to be', expectedString);
106106
});
107107
});
108108

109-
describe('when test speed is fast', function () {
110-
it('should return expected tick, title without a duration', function () {
109+
describe('when test speed is fast', function() {
110+
it('should return expected tick, title without a duration', function() {
111111
var expectedDuration = 1;
112112
var test = {
113113
title: expectedTitle,
114114
duration: expectedDuration,
115-
slow: function () {
115+
slow: function() {
116116
return 2;
117117
}
118118
};
@@ -124,18 +124,18 @@ describe('Spec reporter', function () {
124124
test
125125
);
126126
var options = {};
127-
var stdout = runReporter({epilogue: noop}, runner, options);
127+
var stdout = runReporter({ epilogue: noop }, runner, options);
128128
sinon.restore();
129129

130130
var expectedString =
131-
' ' + Base.symbols.ok + ' ' + expectedTitle + '\n';
131+
' ' + Base.symbols.ok + ' ' + expectedTitle + ' \n';
132132
expect(stdout[0], 'to be', expectedString);
133133
});
134134
});
135135
});
136136

137-
describe("on 'fail' event", function () {
138-
it('should return title and function count', function () {
137+
describe("on 'fail' event", function() {
138+
it('should return title and function count', function() {
139139
var functionCount = 1;
140140
var test = {
141141
title: expectedTitle
@@ -148,7 +148,7 @@ describe('Spec reporter', function () {
148148
test
149149
);
150150
var options = {};
151-
var stdout = runReporter({epilogue: noop}, runner, options);
151+
var stdout = runReporter({ epilogue: noop }, runner, options);
152152
sinon.restore();
153153

154154
var expectedArray = [

0 commit comments

Comments
 (0)