Skip to content

Commit a03e23a

Browse files
outsiderisBamieh
authored andcommitted
fix slow test properly fot dot reporter
Signed-off-by: Outsider <[email protected]>
1 parent 5ec3010 commit a03e23a

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed

lib/reporters/dot.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
var Base = require('./base');
1010
var inherits = require('../utils').inherits;
11-
var color = Base.color;
1211

1312
/**
1413
* Expose `Dot`.
@@ -41,25 +40,25 @@ function Dot(runner) {
4140
if (++n % width === 0) {
4241
process.stdout.write('\n ');
4342
}
44-
process.stdout.write(color('pending', Base.symbols.comma));
43+
process.stdout.write(Base.color('pending', Base.symbols.comma));
4544
});
4645

4746
runner.on('pass', function(test) {
4847
if (++n % width === 0) {
4948
process.stdout.write('\n ');
5049
}
5150
if (test.speed === 'slow') {
52-
process.stdout.write(color('bright yellow', Base.symbols.dot));
51+
process.stdout.write(Base.color('bright yellow', Base.symbols.dot));
5352
} else {
54-
process.stdout.write(color(test.speed, Base.symbols.dot));
53+
process.stdout.write(Base.color(test.speed, Base.symbols.dot));
5554
}
5655
});
5756

5857
runner.on('fail', function() {
5958
if (++n % width === 0) {
6059
process.stdout.write('\n ');
6160
}
62-
process.stdout.write(color('fail', Base.symbols.bang));
61+
process.stdout.write(Base.color('fail', Base.symbols.bang));
6362
});
6463

6564
runner.once('end', function() {

test/reporters/dot.spec.js

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,60 @@ var createMockRunner = require('./helpers.js').createMockRunner;
88

99
describe('Dot reporter', function() {
1010
var stdout;
11-
var stdoutWrite;
1211
var runner;
1312
var useColors;
1413
var windowWidth;
14+
var color;
15+
var showOutput = false;
1516

16-
beforeEach(function() {
17-
stdout = [];
18-
stdoutWrite = process.stdout.write;
17+
/**
18+
* Run reporter using stream reassignment to capture output.
19+
*
20+
* @param {Object} stubSelf - Reporter-like stub instance
21+
* @param {Runner} runner - Mock instance
22+
* @param {boolean} [tee=false] - If `true`, echo captured output to screen
23+
*/
24+
function runReporter(stubSelf, runner, tee) {
25+
// Reassign stream in order to make a copy of all reporter output
26+
var stdoutWrite = process.stdout.write;
1927
process.stdout.write = function(string, enc, callback) {
2028
stdout.push(string);
21-
stdoutWrite.call(process.stdout, string, enc, callback);
29+
if (tee) {
30+
stdoutWrite.call(process.stdout, string, enc, callback);
31+
}
2232
};
33+
34+
// Invoke reporter
35+
Dot.call(stubSelf, runner);
36+
37+
// Revert stream reassignment here so reporter output
38+
// can't be corrupted if any test assertions throw
39+
process.stdout.write = stdoutWrite;
40+
}
41+
42+
beforeEach(function() {
43+
stdout = [];
2344
useColors = Base.useColors;
2445
windowWidth = Base.window.width;
46+
color = Base.color;
2547
Base.useColors = false;
2648
Base.window.width = 0;
49+
Base.color = function(type, str) {
50+
return type.replace(/ /g, '-') + '_' + str;
51+
};
2752
});
2853

2954
afterEach(function() {
3055
Base.useColors = useColors;
3156
Base.window.width = windowWidth;
32-
process.stdout.write = stdoutWrite;
57+
Base.color = color;
58+
runner = undefined;
3359
});
3460

3561
describe('on start', function() {
36-
it('should return a new line', function() {
62+
it('should write a newline', function() {
3763
runner = createMockRunner('start', 'start');
38-
Dot.call({epilogue: function() {}}, runner);
39-
process.stdout.write = stdoutWrite;
64+
runReporter({epilogue: function() {}}, runner, showOutput);
4065
var expectedArray = ['\n'];
4166
expect(stdout, 'to equal', expectedArray);
4267
});
@@ -46,20 +71,18 @@ describe('Dot reporter', function() {
4671
beforeEach(function() {
4772
Base.window.width = 2;
4873
});
49-
it('should return a new line and then a coma', function() {
74+
it('should write a newline followed by a comma', function() {
5075
runner = createMockRunner('pending', 'pending');
51-
Dot.call({epilogue: function() {}}, runner);
52-
process.stdout.write = stdoutWrite;
53-
var expectedArray = ['\n ', Base.symbols.comma];
76+
runReporter({epilogue: function() {}}, runner, showOutput);
77+
var expectedArray = ['\n ', 'pending_' + Base.symbols.comma];
5478
expect(stdout, 'to equal', expectedArray);
5579
});
5680
});
5781
describe('if window width is equal to or less than 1', function() {
58-
it('should return a coma', function() {
82+
it('should write a comma', function() {
5983
runner = createMockRunner('pending', 'pending');
60-
Dot.call({epilogue: function() {}}, runner);
61-
process.stdout.write = stdoutWrite;
62-
var expectedArray = [Base.symbols.comma];
84+
runReporter({epilogue: function() {}}, runner, showOutput);
85+
var expectedArray = ['pending_' + Base.symbols.comma];
6386
expect(stdout, 'to equal', expectedArray);
6487
});
6588
});
@@ -76,32 +99,42 @@ describe('Dot reporter', function() {
7699
Base.window.width = 2;
77100
});
78101
describe('if test speed is fast', function() {
79-
it('should return a new line and then a dot', function() {
102+
it('should write a newline followed by a dot', function() {
80103
runner = createMockRunner('pass', 'pass', null, null, test);
81-
Dot.call({epilogue: function() {}}, runner);
82-
process.stdout.write = stdoutWrite;
83-
var expectedArray = ['\n ', Base.symbols.dot];
104+
runReporter({epilogue: function() {}}, runner, showOutput);
105+
expect(test.speed, 'to equal', 'fast');
106+
var expectedArray = ['\n ', 'fast_' + Base.symbols.dot];
84107
expect(stdout, 'to equal', expectedArray);
85108
});
86109
});
87110
});
88111
describe('if window width is equal to or less than 1', function() {
89112
describe('if test speed is fast', function() {
90-
it('should return a dot', function() {
113+
it('should write a grey dot', function() {
91114
runner = createMockRunner('pass', 'pass', null, null, test);
92-
Dot.call({epilogue: function() {}}, runner);
93-
process.stdout.write = stdoutWrite;
94-
var expectedArray = [Base.symbols.dot];
115+
runReporter({epilogue: function() {}}, runner, showOutput);
116+
expect(test.speed, 'to equal', 'fast');
117+
var expectedArray = ['fast_' + Base.symbols.dot];
95118
expect(stdout, 'to equal', expectedArray);
96119
});
97120
});
98-
describe('if test speed is slow', function() {
99-
it('should return a dot', function() {
121+
describe('if test speed is medium', function() {
122+
it('should write a yellow dot', function() {
100123
test.duration = 2;
101124
runner = createMockRunner('pass', 'pass', null, null, test);
102-
Dot.call({epilogue: function() {}}, runner);
103-
process.stdout.write = stdoutWrite;
104-
var expectedArray = [Base.symbols.dot];
125+
runReporter({epilogue: function() {}}, runner, showOutput);
126+
expect(test.speed, 'to equal', 'medium');
127+
var expectedArray = ['medium_' + Base.symbols.dot];
128+
expect(stdout, 'to equal', expectedArray);
129+
});
130+
});
131+
describe('if test speed is slow', function() {
132+
it('should write a bright yellow dot', function() {
133+
test.duration = 3;
134+
runner = createMockRunner('pass', 'pass', null, null, test);
135+
runReporter({epilogue: function() {}}, runner, showOutput);
136+
expect(test.speed, 'to equal', 'slow');
137+
var expectedArray = ['bright-yellow_' + Base.symbols.dot];
105138
expect(stdout, 'to equal', expectedArray);
106139
});
107140
});
@@ -117,20 +150,18 @@ describe('Dot reporter', function() {
117150
beforeEach(function() {
118151
Base.window.width = 2;
119152
});
120-
it('should return a new line and then an exclamation mark', function() {
153+
it('should write a newline followed by an exclamation mark', function() {
121154
runner = createMockRunner('fail', 'fail', null, null, test);
122-
Dot.call({epilogue: function() {}}, runner);
123-
process.stdout.write = stdoutWrite;
124-
var expectedArray = ['\n ', Base.symbols.bang];
155+
runReporter({epilogue: function() {}}, runner, showOutput);
156+
var expectedArray = ['\n ', 'fail_' + Base.symbols.bang];
125157
expect(stdout, 'to equal', expectedArray);
126158
});
127159
});
128160
describe('if window width is equal to or less than 1', function() {
129-
it('should return an exclamation mark', function() {
161+
it('should write an exclamation mark', function() {
130162
runner = createMockRunner('fail', 'fail', null, null, test);
131-
Dot.call({epilogue: function() {}}, runner);
132-
process.stdout.write = stdoutWrite;
133-
var expectedArray = [Base.symbols.bang];
163+
runReporter({epilogue: function() {}}, runner, showOutput);
164+
var expectedArray = ['fail_' + Base.symbols.bang];
134165
expect(stdout, 'to equal', expectedArray);
135166
});
136167
});
@@ -142,8 +173,7 @@ describe('Dot reporter', function() {
142173
var epilogue = function() {
143174
epilogueCalled = true;
144175
};
145-
Dot.call({epilogue: epilogue}, runner);
146-
process.stdout.write = stdoutWrite;
176+
runReporter({epilogue: epilogue}, runner, showOutput);
147177
expect(epilogueCalled, 'to be', true);
148178
});
149179
});

0 commit comments

Comments
 (0)