|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +var fs = require('fs'); |
3 | 4 | var sinon = require('sinon');
|
| 5 | +var JSONReporter = require('../../lib/reporters/json'); |
| 6 | +var utils = require('../../lib/utils'); |
4 | 7 | var Mocha = require('../../');
|
5 | 8 | var Suite = Mocha.Suite;
|
6 | 9 | var Runner = Mocha.Runner;
|
7 | 10 | var Test = Mocha.Test;
|
8 | 11 |
|
9 | 12 | describe('JSON reporter', function() {
|
| 13 | + var mocha; |
10 | 14 | var suite;
|
11 | 15 | var runner;
|
12 | 16 | var testTitle = 'json test 1';
|
13 | 17 | var testFile = 'someTest.spec.js';
|
14 | 18 | var noop = function() {};
|
15 | 19 |
|
16 | 20 | beforeEach(function() {
|
17 |
| - var mocha = new Mocha({ |
| 21 | + mocha = new Mocha({ |
18 | 22 | reporter: 'json'
|
19 | 23 | });
|
20 | 24 | suite = new Suite('JSON suite', 'root');
|
21 | 25 | runner = new Runner(suite);
|
22 |
| - var options = {}; |
23 |
| - /* eslint no-unused-vars: off */ |
24 |
| - var mochaReporter = new mocha._reporter(runner, options); |
25 |
| - }); |
26 |
| - |
27 |
| - beforeEach(function() { |
28 |
| - sinon.stub(process.stdout, 'write').callsFake(noop); |
29 | 26 | });
|
30 | 27 |
|
31 | 28 | afterEach(function() {
|
32 | 29 | sinon.restore();
|
33 | 30 | });
|
34 | 31 |
|
35 |
| - it('should have 1 test failure', function(done) { |
36 |
| - var error = {message: 'oh shit'}; |
| 32 | + describe('test results', function() { |
| 33 | + beforeEach(function() { |
| 34 | + var options = {}; |
| 35 | + /* eslint no-unused-vars: off */ |
| 36 | + var mochaReporter = new mocha._reporter(runner, options); |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(function() { |
| 40 | + sinon.stub(process.stdout, 'write').callsFake(noop); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should have 1 test failure', function(done) { |
| 44 | + var error = {message: 'oh shit'}; |
37 | 45 |
|
38 |
| - var test = new Test(testTitle, function(done) { |
39 |
| - done(new Error(error.message)); |
| 46 | + var test = new Test(testTitle, function(done) { |
| 47 | + done(new Error(error.message)); |
| 48 | + }); |
| 49 | + |
| 50 | + test.file = testFile; |
| 51 | + suite.addTest(test); |
| 52 | + |
| 53 | + runner.run(function(failureCount) { |
| 54 | + sinon.restore(); |
| 55 | + expect(runner, 'to satisfy', { |
| 56 | + testResults: { |
| 57 | + failures: [ |
| 58 | + { |
| 59 | + title: testTitle, |
| 60 | + file: testFile, |
| 61 | + err: { |
| 62 | + message: error.message |
| 63 | + } |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + }); |
| 68 | + expect(failureCount, 'to be', 1); |
| 69 | + done(); |
| 70 | + }); |
40 | 71 | });
|
41 | 72 |
|
42 |
| - test.file = testFile; |
43 |
| - suite.addTest(test); |
44 |
| - |
45 |
| - runner.run(function(failureCount) { |
46 |
| - sinon.restore(); |
47 |
| - expect(runner, 'to satisfy', { |
48 |
| - testResults: { |
49 |
| - failures: [ |
50 |
| - { |
51 |
| - title: testTitle, |
52 |
| - file: testFile, |
53 |
| - err: { |
54 |
| - message: error.message |
| 73 | + it('should have 1 test pending', function(done) { |
| 74 | + var test = new Test(testTitle); |
| 75 | + test.file = testFile; |
| 76 | + suite.addTest(test); |
| 77 | + |
| 78 | + runner.run(function(failureCount) { |
| 79 | + sinon.restore(); |
| 80 | + expect(runner, 'to satisfy', { |
| 81 | + testResults: { |
| 82 | + pending: [ |
| 83 | + { |
| 84 | + title: testTitle, |
| 85 | + file: testFile |
55 | 86 | }
|
56 |
| - } |
57 |
| - ] |
58 |
| - } |
| 87 | + ] |
| 88 | + } |
| 89 | + }); |
| 90 | + expect(failureCount, 'to be', 0); |
| 91 | + done(); |
59 | 92 | });
|
60 |
| - expect(failureCount, 'to be', 1); |
61 |
| - done(); |
62 | 93 | });
|
63 |
| - }); |
64 | 94 |
|
65 |
| - it('should have 1 test pending', function(done) { |
66 |
| - var test = new Test(testTitle); |
67 |
| - test.file = testFile; |
68 |
| - suite.addTest(test); |
69 |
| - |
70 |
| - runner.run(function(failureCount) { |
71 |
| - sinon.restore(); |
72 |
| - expect(runner, 'to satisfy', { |
73 |
| - testResults: { |
74 |
| - pending: [ |
75 |
| - { |
76 |
| - title: testTitle, |
77 |
| - file: testFile |
78 |
| - } |
79 |
| - ] |
80 |
| - } |
| 95 | + it('should have 1 test pass', function(done) { |
| 96 | + const test = new Test(testTitle, () => {}); |
| 97 | + |
| 98 | + test.file = testFile; |
| 99 | + suite.addTest(test); |
| 100 | + |
| 101 | + runner.run(function(failureCount) { |
| 102 | + sinon.restore(); |
| 103 | + expect(runner, 'to satisfy', { |
| 104 | + testResults: { |
| 105 | + passes: [ |
| 106 | + { |
| 107 | + title: testTitle, |
| 108 | + file: testFile, |
| 109 | + speed: /(slow|medium|fast)/ |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + }); |
| 114 | + expect(failureCount, 'to be', 0); |
| 115 | + done(); |
81 | 116 | });
|
82 |
| - expect(failureCount, 'to be', 0); |
83 |
| - done(); |
84 | 117 | });
|
85 |
| - }); |
86 | 118 |
|
87 |
| - it('should have 1 test pass', function(done) { |
88 |
| - const test = new Test(testTitle, () => {}); |
89 |
| - |
90 |
| - test.file = testFile; |
91 |
| - suite.addTest(test); |
92 |
| - |
93 |
| - runner.run(function(failureCount) { |
94 |
| - expect(runner, 'to satisfy', { |
95 |
| - testResults: { |
96 |
| - passes: [ |
97 |
| - { |
98 |
| - title: testTitle, |
99 |
| - file: testFile, |
100 |
| - speed: /(slow|medium|fast)/ |
101 |
| - } |
102 |
| - ] |
103 |
| - } |
| 119 | + it('should handle circular objects in errors', function(done) { |
| 120 | + var testTitle = 'json test 1'; |
| 121 | + function CircleError() { |
| 122 | + this.message = 'oh shit'; |
| 123 | + this.circular = this; |
| 124 | + } |
| 125 | + var error = new CircleError(); |
| 126 | + |
| 127 | + var test = new Test(testTitle, function(done) { |
| 128 | + throw error; |
| 129 | + }); |
| 130 | + |
| 131 | + test.file = testFile; |
| 132 | + suite.addTest(test); |
| 133 | + |
| 134 | + runner.run(function(failureCount) { |
| 135 | + sinon.restore(); |
| 136 | + expect(runner, 'to satisfy', { |
| 137 | + testResults: { |
| 138 | + failures: [ |
| 139 | + { |
| 140 | + title: testTitle, |
| 141 | + file: testFile, |
| 142 | + err: { |
| 143 | + message: error.message |
| 144 | + } |
| 145 | + } |
| 146 | + ] |
| 147 | + } |
| 148 | + }); |
| 149 | + expect(failureCount, 'to be', 1); |
| 150 | + done(); |
104 | 151 | });
|
105 |
| - expect(failureCount, 'to be', 0); |
106 |
| - done(); |
107 | 152 | });
|
108 | 153 | });
|
109 | 154 |
|
110 |
| - it('should handle circular objects in errors', function(done) { |
111 |
| - var testTitle = 'json test 1'; |
112 |
| - function CircleError() { |
113 |
| - this.message = 'oh shit'; |
114 |
| - this.circular = this; |
115 |
| - } |
116 |
| - var error = new CircleError(); |
| 155 | + describe('when "reporterOption.output" is provided', function() { |
| 156 | + var expectedDirName = 'reports'; |
| 157 | + var expectedFileName = 'reports/test-results.json'; |
| 158 | + var options = { |
| 159 | + reporterOption: { |
| 160 | + output: expectedFileName |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + beforeEach(function() { |
| 165 | + /* eslint no-unused-vars: off */ |
| 166 | + var mochaReporter = new mocha._reporter(runner, options); |
| 167 | + }); |
117 | 168 |
|
118 |
| - var test = new Test(testTitle, function(done) { |
119 |
| - throw error; |
| 169 | + beforeEach(function() { |
| 170 | + // Add one test to suite to avoid assertions against empty test results |
| 171 | + var test = new Test(testTitle, () => {}); |
| 172 | + test.file = testFile; |
| 173 | + suite.addTest(test); |
120 | 174 | });
|
121 | 175 |
|
122 |
| - test.file = testFile; |
123 |
| - suite.addTest(test); |
124 |
| - |
125 |
| - runner.run(function(failureCount) { |
126 |
| - sinon.restore(); |
127 |
| - expect(runner, 'to satisfy', { |
128 |
| - testResults: { |
129 |
| - failures: [ |
130 |
| - { |
131 |
| - title: testTitle, |
132 |
| - file: testFile, |
133 |
| - err: { |
134 |
| - message: error.message |
135 |
| - } |
136 |
| - } |
137 |
| - ] |
138 |
| - } |
| 176 | + it('should write test results to file', function(done) { |
| 177 | + const fsMkdirSync = sinon.stub(fs, 'mkdirSync'); |
| 178 | + const fsWriteFileSync = sinon.stub(fs, 'writeFileSync'); |
| 179 | + |
| 180 | + fsWriteFileSync.callsFake(function(filename, content) { |
| 181 | + const expectedJson = JSON.stringify(runner.testResults, null, 2); |
| 182 | + expect(expectedFileName, 'to be', filename); |
| 183 | + expect(content, 'to be', expectedJson); |
139 | 184 | });
|
140 |
| - expect(failureCount, 'to be', 1); |
141 |
| - done(); |
| 185 | + |
| 186 | + runner.run(function() { |
| 187 | + expect( |
| 188 | + fsMkdirSync.calledWith(expectedDirName, {recursive: true}), |
| 189 | + 'to be true' |
| 190 | + ); |
| 191 | + expect(fsWriteFileSync.calledOnce, 'to be true'); |
| 192 | + done(); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should warn and write test results to console', function(done) { |
| 197 | + const fsMkdirSync = sinon.stub(fs, 'mkdirSync'); |
| 198 | + const fsWriteFileSync = sinon.stub(fs, 'writeFileSync'); |
| 199 | + |
| 200 | + fsWriteFileSync.throws('unable to write file'); |
| 201 | + |
| 202 | + const outLog = []; |
| 203 | + const fake = chunk => outLog.push(chunk); |
| 204 | + sinon.stub(process.stderr, 'write').callsFake(fake); |
| 205 | + sinon.stub(process.stdout, 'write').callsFake(fake); |
| 206 | + |
| 207 | + runner.run(function() { |
| 208 | + sinon.restore(); |
| 209 | + expect( |
| 210 | + fsMkdirSync.calledWith(expectedDirName, {recursive: true}), |
| 211 | + 'to be true' |
| 212 | + ); |
| 213 | + expect(fsWriteFileSync.calledOnce, 'to be true'); |
| 214 | + expect( |
| 215 | + outLog[0], |
| 216 | + 'to contain', |
| 217 | + `[mocha] writing output to "${expectedFileName}" failed:` |
| 218 | + ); |
| 219 | + expect(outLog[1], 'to match', /"fullTitle": "JSON suite json test 1"/); |
| 220 | + done(); |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should throw "unsupported error" in browser', function() { |
| 225 | + sinon.stub(utils, 'isBrowser').callsFake(() => true); |
| 226 | + expect( |
| 227 | + () => new JSONReporter(runner, options), |
| 228 | + 'to throw', |
| 229 | + 'file output not supported in browser' |
| 230 | + ); |
142 | 231 | });
|
143 | 232 | });
|
144 | 233 | });
|
0 commit comments