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

Commit ab90367

Browse files
authored
Merge pull request mochajs#2699 from craigtaub/jsonStreamCoverage
Increase tests coverage for json-stream, markdown and progress reporters
2 parents 9050e41 + b498f78 commit ab90367

File tree

3 files changed

+385
-0
lines changed

3 files changed

+385
-0
lines changed

test/reporters/json-stream.spec.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
'use strict';
2+
3+
var reporters = require('../../').reporters;
4+
var JSONStream = reporters.JSONStream;
5+
6+
describe('Json Stream reporter', function () {
7+
var runner;
8+
var stdout;
9+
var stdoutWrite;
10+
11+
beforeEach(function () {
12+
stdout = [];
13+
runner = {};
14+
stdoutWrite = process.stdout.write;
15+
process.stdout.write = function (string) {
16+
stdout.push(string);
17+
};
18+
});
19+
20+
describe('on start', function () {
21+
it('should write stringified start with expected total', function () {
22+
runner.on = function (event, callback) {
23+
if (event === 'start') {
24+
callback();
25+
}
26+
};
27+
var expectedTotal = 12;
28+
runner.total = expectedTotal;
29+
JSONStream.call({}, runner);
30+
31+
process.stdout.write = stdoutWrite;
32+
33+
stdout[0].should.deepEqual('["start",{"total":' + expectedTotal + '}]\n');
34+
});
35+
});
36+
37+
describe('on pass', function () {
38+
it('should write stringified test data', function () {
39+
var expectedTitle = 'some title';
40+
var expectedFullTitle = 'full title';
41+
var expectedDuration = 1000;
42+
var currentRetry = 1;
43+
var expectedTest = {
44+
title: expectedTitle,
45+
fullTitle: function () { return expectedFullTitle; },
46+
duration: expectedDuration,
47+
currentRetry: function () { return currentRetry; },
48+
slow: function () {}
49+
};
50+
runner.on = function (event, callback) {
51+
if (event === 'pass') {
52+
callback(expectedTest);
53+
}
54+
};
55+
JSONStream.call({}, runner);
56+
57+
process.stdout.write = stdoutWrite;
58+
59+
stdout[0].should.deepEqual('["pass",{"title":"' + expectedTitle + '","fullTitle":"' + expectedFullTitle + '","duration":' + expectedDuration + ',"currentRetry":' + currentRetry + '}]\n');
60+
});
61+
});
62+
63+
describe('on fail', function () {
64+
describe('if error stack exists', function () {
65+
it('should write stringified test data with error data', function () {
66+
var expectedTitle = 'some title';
67+
var expectedFullTitle = 'full title';
68+
var expectedDuration = 1000;
69+
var currentRetry = 1;
70+
var expectedTest = {
71+
title: expectedTitle,
72+
fullTitle: function () { return expectedFullTitle; },
73+
duration: expectedDuration,
74+
currentRetry: function () { return currentRetry; },
75+
slow: function () {}
76+
};
77+
var expectedErrorMessage = 'error message';
78+
var expectedErrorStack = 'error stack';
79+
var expectedError = {
80+
message: expectedErrorMessage,
81+
stack: expectedErrorStack
82+
};
83+
runner.on = function (event, callback) {
84+
if (event === 'fail') {
85+
callback(expectedTest, expectedError);
86+
}
87+
};
88+
JSONStream.call({}, runner);
89+
90+
process.stdout.write = stdoutWrite;
91+
92+
stdout[0].should.deepEqual('["fail",{"title":"' + expectedTitle + '","fullTitle":"' + expectedFullTitle + '","duration":' + expectedDuration + ',"currentRetry":' + currentRetry + ',"err":"' + expectedErrorMessage + '","stack":"' + expectedErrorStack + '"}]\n');
93+
});
94+
});
95+
describe('if error stack does not exist', function () {
96+
it('should write stringified test data with error data', function () {
97+
var expectedTitle = 'some title';
98+
var expectedFullTitle = 'full title';
99+
var expectedDuration = 1000;
100+
var currentRetry = 1;
101+
var expectedTest = {
102+
title: expectedTitle,
103+
fullTitle: function () { return expectedFullTitle; },
104+
duration: expectedDuration,
105+
currentRetry: function () { return currentRetry; },
106+
slow: function () {}
107+
};
108+
var expectedErrorMessage = 'error message';
109+
var expectedError = {
110+
message: expectedErrorMessage
111+
};
112+
runner.on = function (event, callback) {
113+
if (event === 'fail') {
114+
callback(expectedTest, expectedError);
115+
}
116+
};
117+
JSONStream.call({}, runner);
118+
119+
process.stdout.write = stdoutWrite;
120+
121+
stdout[0].should.deepEqual('["fail",{"title":"' + expectedTitle + '","fullTitle":"' + expectedFullTitle + '","duration":' + expectedDuration + ',"currentRetry":' + currentRetry + ',"err":"' + expectedErrorMessage + '","stack":null}]\n');
122+
});
123+
});
124+
});
125+
126+
describe('on end', function () {
127+
it('should write end details', function () {
128+
runner.on = function (event, callback) {
129+
if (event === 'end') {
130+
callback();
131+
}
132+
};
133+
JSONStream.call({}, runner);
134+
135+
process.stdout.write = stdoutWrite;
136+
137+
stdout[0].should.match(/end/);
138+
});
139+
});
140+
});

test/reporters/markdown.spec.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
3+
var reporters = require('../../').reporters;
4+
var Markdown = reporters.Markdown;
5+
6+
describe('Markdown reporter', function () {
7+
var stdout;
8+
var stdoutWrite;
9+
var runner;
10+
11+
beforeEach(function () {
12+
stdout = [];
13+
runner = {};
14+
stdoutWrite = process.stdout.write;
15+
process.stdout.write = function (string) {
16+
stdout.push(string);
17+
};
18+
});
19+
20+
describe('on \'suite\'', function () {
21+
it('should write expected slugged titles on \'end\' event', function () {
22+
var expectedTitle = 'expected title';
23+
var expectedFullTitle = 'full title';
24+
var sluggedFullTitle = 'full-title';
25+
var expectedSuite = {
26+
title: expectedTitle,
27+
fullTitle: function () { return expectedFullTitle; },
28+
suites: [{
29+
title: expectedTitle,
30+
fullTitle: function () { return expectedFullTitle; },
31+
suites: []
32+
}]
33+
};
34+
runner.on = function (event, callback) {
35+
if (event === 'suite') {
36+
callback(expectedSuite);
37+
}
38+
if (event === 'suite end') {
39+
callback();
40+
}
41+
if (event === 'end') {
42+
callback();
43+
}
44+
};
45+
runner.suite = expectedSuite;
46+
Markdown.call({}, runner);
47+
process.stdout.write = stdoutWrite;
48+
49+
var expectedArray = [
50+
'# TOC\n',
51+
' - [' + expectedTitle + '](#' + sluggedFullTitle + ')\n - [' + expectedTitle + '](#' + sluggedFullTitle + ')\n',
52+
'<a name="' + sluggedFullTitle + '"></a>\n ' + expectedTitle + '\n'
53+
];
54+
55+
stdout.should.deepEqual(expectedArray);
56+
});
57+
});
58+
describe('on \'pass\'', function () {
59+
it('should write test code inside js code block, on \'end\' event', function () {
60+
var expectedTitle = 'expected title';
61+
var expectedFullTitle = 'full title';
62+
var sluggedFullTitle = 'full-title';
63+
var expectedSuite = {
64+
title: expectedTitle,
65+
fullTitle: function () { return expectedFullTitle; },
66+
suites: []
67+
};
68+
var expectedDuration = 1000;
69+
var currentRetry = 1;
70+
var expectedBody = 'some body';
71+
var expectedTest = {
72+
title: expectedTitle,
73+
fullTitle: function () { return expectedFullTitle; },
74+
duration: expectedDuration,
75+
currentRetry: function () { return currentRetry; },
76+
slow: function () {},
77+
body: expectedBody
78+
};
79+
runner.on = function (event, callback) {
80+
if (event === 'pass') {
81+
callback(expectedTest);
82+
}
83+
if (event === 'end') {
84+
callback();
85+
}
86+
};
87+
runner.suite = expectedSuite;
88+
Markdown.call({}, runner);
89+
process.stdout.write = stdoutWrite;
90+
91+
var expectedArray = [
92+
'# TOC\n',
93+
' - [' + expectedTitle + '](#' + sluggedFullTitle + ')\n',
94+
expectedTitle + '.\n\n```js\n' + expectedBody + '\n```\n\n'
95+
];
96+
97+
stdout.should.deepEqual(expectedArray);
98+
});
99+
});
100+
});

test/reporters/progress.spec.js

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
'use strict';
2+
3+
var reporters = require('../../').reporters;
4+
var Progress = reporters.Progress;
5+
var Base = reporters.Base;
6+
7+
describe('Progress reporter', function () {
8+
var stdout;
9+
var stdoutWrite;
10+
var runner;
11+
12+
beforeEach(function () {
13+
stdout = [];
14+
runner = {};
15+
stdoutWrite = process.stdout.write;
16+
process.stdout.write = function (string) {
17+
stdout.push(string);
18+
};
19+
});
20+
21+
describe('on start', function () {
22+
it('should call cursor hide', function () {
23+
var cachedCursor = Base.cursor;
24+
var calledCursorHide = false;
25+
Base.cursor.hide = function () {
26+
calledCursorHide = true;
27+
};
28+
runner.on = function (event, callback) {
29+
if (event === 'start') {
30+
callback();
31+
}
32+
};
33+
Progress.call({}, runner);
34+
35+
process.stdout.write = stdoutWrite;
36+
calledCursorHide.should.be.true();
37+
38+
Base.cursor = cachedCursor;
39+
});
40+
});
41+
42+
describe('on test end', function () {
43+
describe('if line has not changed', function () {
44+
it('should return and not write anything', function () {
45+
var cachedCursor = Base.cursor;
46+
var useColors = Base.useColors;
47+
Base.useColors = false;
48+
Base.cursor.CR = function () {};
49+
var windowWidth = Base.window.width;
50+
Base.window.width = -3;
51+
52+
var expectedTotal = 1;
53+
var expectedOptions = {};
54+
runner.total = expectedTotal;
55+
runner.on = function (event, callback) {
56+
if (event === 'test end') {
57+
callback();
58+
}
59+
};
60+
Progress.call({}, runner, expectedOptions);
61+
62+
process.stdout.write = stdoutWrite;
63+
64+
stdout.should.deepEqual([]);
65+
66+
Base.cursor = cachedCursor;
67+
Base.useColors = useColors;
68+
Base.window.width = windowWidth;
69+
});
70+
});
71+
describe('if line has changed', function () {
72+
it('should write expected progress of open and close options', function () {
73+
var calledCursorCR = false;
74+
var cachedCursor = Base.cursor;
75+
var useColors = Base.useColors;
76+
Base.useColors = false;
77+
Base.cursor.CR = function () {
78+
calledCursorCR = true;
79+
};
80+
var windowWidth = Base.window.width;
81+
Base.window.width = 5;
82+
83+
var expectedTotal = 12;
84+
var expectedOpen = 'OpEn';
85+
var expectedClose = 'cLoSe';
86+
var expectedIncomplete = 'iNcOmPlEtE';
87+
var expectedOptions = {
88+
open: expectedOpen,
89+
complete: 'cOmPlEtE',
90+
incomplete: expectedIncomplete,
91+
close: expectedClose
92+
};
93+
runner.total = expectedTotal;
94+
runner.on = function (event, callback) {
95+
if (event === 'test end') {
96+
callback();
97+
}
98+
};
99+
Progress.call({}, runner, expectedOptions);
100+
101+
process.stdout.write = stdoutWrite;
102+
var expectedArray = [
103+
'\u001b[J',
104+
' ' + expectedOpen,
105+
'',
106+
expectedIncomplete,
107+
expectedClose
108+
];
109+
calledCursorCR.should.be.true();
110+
stdout.should.deepEqual(expectedArray);
111+
112+
Base.cursor = cachedCursor;
113+
Base.useColors = useColors;
114+
Base.window.width = windowWidth;
115+
});
116+
});
117+
});
118+
119+
describe('on end', function () {
120+
it('should call cursor show and epilogue', function () {
121+
var cachedCursor = Base.cursor;
122+
var calledCursorShow = false;
123+
Base.cursor.show = function () {
124+
calledCursorShow = true;
125+
};
126+
runner.on = function (event, callback) {
127+
if (event === 'end') {
128+
callback();
129+
}
130+
};
131+
var calledEpilogue = false;
132+
Progress.call({
133+
epilogue: function () {
134+
calledEpilogue = true;
135+
}
136+
}, runner);
137+
138+
process.stdout.write = stdoutWrite;
139+
calledEpilogue.should.be.true();
140+
calledCursorShow.should.be.true();
141+
142+
Base.cursor = cachedCursor;
143+
});
144+
});
145+
});

0 commit comments

Comments
 (0)