Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 9423643

Browse files
authored
Merge pull request mochajs#2691 from craigtaub/reporterCoverageSpec
Increase tests coverage for spec + dot reporters
2 parents 5168c9c + e2abb14 commit 9423643

File tree

2 files changed

+340
-0
lines changed

2 files changed

+340
-0
lines changed

test/reporters/dot.spec.js

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
'use strict';
2+
3+
var reporters = require('../../').reporters;
4+
var Dot = reporters.Dot;
5+
var Base = reporters.Base;
6+
7+
describe('Dot reporter', function () {
8+
var stdout;
9+
var stdoutWrite;
10+
var runner;
11+
var useColors;
12+
var windowWidth;
13+
14+
beforeEach(function () {
15+
stdout = [];
16+
runner = {};
17+
stdoutWrite = process.stdout.write;
18+
process.stdout.write = function (string) {
19+
stdout.push(string);
20+
};
21+
useColors = Base.useColors;
22+
windowWidth = Base.window.width;
23+
Base.useColors = false;
24+
Base.window.width = 0;
25+
});
26+
27+
afterEach(function () {
28+
Base.useColors = useColors;
29+
Base.window.width = windowWidth;
30+
});
31+
32+
describe('on start', function () {
33+
it('should return a new line', function () {
34+
runner.on = function (event, callback) {
35+
if (event === 'start') {
36+
callback();
37+
}
38+
};
39+
Dot.call({epilogue: function () {}}, runner);
40+
process.stdout.write = stdoutWrite;
41+
var expectedArray = [
42+
'\n'
43+
];
44+
stdout.should.deepEqual(expectedArray);
45+
});
46+
});
47+
describe('on pending', function () {
48+
describe('if window width is greater than 1', function () {
49+
beforeEach(function () {
50+
Base.window.width = 2;
51+
});
52+
it('should return a new line and then a coma', function () {
53+
runner.on = function (event, callback) {
54+
if (event === 'pending') {
55+
callback();
56+
}
57+
};
58+
Dot.call({epilogue: function () {}}, runner);
59+
process.stdout.write = stdoutWrite;
60+
var expectedArray = [
61+
'\n ',
62+
Base.symbols.comma
63+
];
64+
stdout.should.deepEqual(expectedArray);
65+
});
66+
});
67+
describe('if window width is equal to or less than 1', function () {
68+
it('should return a coma', function () {
69+
runner.on = function (event, callback) {
70+
if (event === 'pending') {
71+
callback();
72+
}
73+
};
74+
Dot.call({epilogue: function () {}}, runner);
75+
process.stdout.write = stdoutWrite;
76+
var expectedArray = [
77+
Base.symbols.comma
78+
];
79+
stdout.should.deepEqual(expectedArray);
80+
});
81+
});
82+
});
83+
describe('on pass', function () {
84+
describe('if window width is greater than 1', function () {
85+
beforeEach(function () {
86+
Base.window.width = 2;
87+
});
88+
describe('if test speed is fast', function () {
89+
it('should return a new line and then a dot', function () {
90+
var test = {
91+
duration: 1,
92+
slow: function () { return 2; }
93+
};
94+
runner.on = function (event, callback) {
95+
if (event === 'pass') {
96+
callback(test);
97+
}
98+
};
99+
Dot.call({epilogue: function () {}}, runner);
100+
process.stdout.write = stdoutWrite;
101+
var expectedArray = [
102+
'\n ',
103+
Base.symbols.dot
104+
];
105+
stdout.should.deepEqual(expectedArray);
106+
});
107+
});
108+
});
109+
describe('if window width is equal to or less than 1', function () {
110+
describe('if test speed is fast', function () {
111+
it('should return a dot', function () {
112+
var test = {
113+
duration: 1,
114+
slow: function () { return 2; }
115+
};
116+
runner.on = function (event, callback) {
117+
if (event === 'pass') {
118+
callback(test);
119+
}
120+
};
121+
Dot.call({epilogue: function () {}}, runner);
122+
process.stdout.write = stdoutWrite;
123+
var expectedArray = [
124+
Base.symbols.dot
125+
];
126+
stdout.should.deepEqual(expectedArray);
127+
});
128+
});
129+
describe('if test speed is slow', function () {
130+
it('should return a dot', function () {
131+
var test = {
132+
duration: 2,
133+
slow: function () { return 1; }
134+
};
135+
runner.on = function (event, callback) {
136+
if (event === 'pass') {
137+
callback(test);
138+
}
139+
};
140+
Dot.call({epilogue: function () {}}, runner);
141+
process.stdout.write = stdoutWrite;
142+
var expectedArray = [
143+
Base.symbols.dot
144+
];
145+
stdout.should.deepEqual(expectedArray);
146+
});
147+
});
148+
});
149+
});
150+
describe('on fail', function () {
151+
describe('if window width is greater than 1', function () {
152+
beforeEach(function () {
153+
Base.window.width = 2;
154+
});
155+
it('should return a new line and then an exclamation mark', function () {
156+
var test = {
157+
test: {
158+
err: 'some error'
159+
}
160+
};
161+
runner.on = function (event, callback) {
162+
if (event === 'fail') {
163+
callback(test);
164+
}
165+
};
166+
Dot.call({epilogue: function () {}}, runner);
167+
process.stdout.write = stdoutWrite;
168+
var expectedArray = [
169+
'\n ',
170+
Base.symbols.bang
171+
];
172+
stdout.should.deepEqual(expectedArray);
173+
});
174+
});
175+
describe('if window width is equal to or less than 1', function () {
176+
it('should return an exclamation mark', function () {
177+
var test = {
178+
test: {
179+
err: 'some error'
180+
}
181+
};
182+
runner.on = function (event, callback) {
183+
if (event === 'fail') {
184+
callback(test);
185+
}
186+
};
187+
Dot.call({epilogue: function () {}}, runner);
188+
process.stdout.write = stdoutWrite;
189+
var expectedArray = [
190+
Base.symbols.bang
191+
];
192+
stdout.should.deepEqual(expectedArray);
193+
});
194+
});
195+
});
196+
describe('on end', function () {
197+
it('should call the epilogue', function () {
198+
runner.on = function (event, callback) {
199+
if (event === 'end') {
200+
callback();
201+
}
202+
};
203+
var epilogueCalled = false;
204+
var epilogue = function () {
205+
epilogueCalled = true;
206+
};
207+
Dot.call({epilogue: epilogue}, runner);
208+
process.stdout.write = stdoutWrite;
209+
epilogueCalled.should.be.true();
210+
});
211+
});
212+
});

test/reporters/spec.spec.js

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
3+
var reporters = require('../../').reporters;
4+
var Spec = reporters.Spec;
5+
var Base = reporters.Base;
6+
7+
describe('Spec reporter', function () {
8+
var stdout;
9+
var stdoutWrite;
10+
var runner;
11+
var useColors;
12+
13+
beforeEach(function () {
14+
stdout = [];
15+
runner = {};
16+
stdoutWrite = process.stdout.write;
17+
process.stdout.write = function (string) {
18+
stdout.push(string);
19+
};
20+
useColors = Base.useColors;
21+
Base.useColors = false;
22+
});
23+
24+
afterEach(function () {
25+
Base.useColors = useColors;
26+
});
27+
28+
describe('on suite', function () {
29+
it('should return title', function () {
30+
var expectedTitle = 'expectedTitle';
31+
var suite = {
32+
title: expectedTitle
33+
};
34+
runner.on = function (event, callback) {
35+
if (event === 'suite') {
36+
callback(suite);
37+
}
38+
};
39+
Spec.call({epilogue: function () {}}, runner);
40+
process.stdout.write = stdoutWrite;
41+
var expectedArray = [
42+
expectedTitle + '\n'
43+
];
44+
stdout.should.deepEqual(expectedArray);
45+
});
46+
});
47+
describe('on pending', function () {
48+
it('should return title', function () {
49+
var expectedTitle = 'expectedTitle';
50+
var suite = {
51+
title: expectedTitle
52+
};
53+
runner.on = function (event, callback) {
54+
if (event === 'pending') {
55+
callback(suite);
56+
}
57+
};
58+
Spec.call({epilogue: function () {}}, runner);
59+
process.stdout.write = stdoutWrite;
60+
var expectedArray = [
61+
' - ' + expectedTitle + '\n'
62+
];
63+
stdout.should.deepEqual(expectedArray);
64+
});
65+
});
66+
describe('on pass', function () {
67+
describe('if test speed is slow', function () {
68+
it('should return expected tick, title and duration', function () {
69+
var expectedTitle = 'expectedTitle';
70+
var expectedDuration = 2;
71+
var test = {
72+
title: expectedTitle,
73+
duration: expectedDuration,
74+
slow: function () { return 1; }
75+
};
76+
runner.on = function (event, callback) {
77+
if (event === 'pass') {
78+
callback(test);
79+
}
80+
};
81+
Spec.call({epilogue: function () {}}, runner);
82+
process.stdout.write = stdoutWrite;
83+
var expectedString = ' ' + Base.symbols.ok + ' ' + expectedTitle + ' (' + expectedDuration + 'ms)' + '\n';
84+
stdout[0].should.equal(expectedString);
85+
});
86+
});
87+
describe('if test speed is fast', function () {
88+
it('should return expected tick, title and without a duration', function () {
89+
var expectedTitle = 'expectedTitle';
90+
var expectedDuration = 1;
91+
var test = {
92+
title: expectedTitle,
93+
duration: expectedDuration,
94+
slow: function () { return 2; }
95+
};
96+
runner.on = function (event, callback) {
97+
if (event === 'pass') {
98+
callback(test);
99+
}
100+
};
101+
Spec.call({epilogue: function () {}}, runner);
102+
process.stdout.write = stdoutWrite;
103+
var expectedString = ' ' + Base.symbols.ok + ' ' + expectedTitle + '\n';
104+
stdout[0].should.equal(expectedString);
105+
});
106+
});
107+
});
108+
describe('on fail', function () {
109+
it('should return title and function count', function () {
110+
var expectedTitle = 'expectedTitle';
111+
var functionCount = 1;
112+
var test = {
113+
title: expectedTitle
114+
};
115+
runner.on = function (event, callback) {
116+
if (event === 'fail') {
117+
callback(test);
118+
}
119+
};
120+
Spec.call({epilogue: function () {}}, runner);
121+
process.stdout.write = stdoutWrite;
122+
var expectedArray = [
123+
' ' + functionCount + ') ' + expectedTitle + '\n'
124+
];
125+
stdout.should.deepEqual(expectedArray);
126+
});
127+
});
128+
});

0 commit comments

Comments
 (0)