| 1 |
"use strict"; |
| 2 |
|
| 3 |
var fs = require("node:fs"); |
| 4 |
var sinon = require("sinon"); |
| 5 |
var JSONReporter = require("../../lib/reporters/json"); |
| 6 |
var utils = require("../../lib/utils"); |
| 7 |
var Mocha = require("../../"); |
| 8 |
var Suite = Mocha.Suite; |
| 9 |
var Runner = Mocha.Runner; |
| 10 |
var Test = Mocha.Test; |
| 11 |
|
| 12 |
describe("JSON reporter", function () { |
| 13 |
var mocha; |
| 14 |
var suite; |
| 15 |
var runner; |
| 16 |
var testTitle = "json test 1"; |
| 17 |
var testFile = "someTest.spec.js"; |
| 18 |
var noop = function () {}; |
| 19 |
|
| 20 |
beforeEach(function () { |
| 21 |
mocha = new Mocha({ |
| 22 |
reporter: "json", |
| 23 |
}); |
| 24 |
suite = new Suite("JSON suite", "root"); |
| 25 |
runner = new Runner(suite); |
| 26 |
}); |
| 27 |
|
| 28 |
afterEach(function () { |
| 29 |
sinon.restore(); |
| 30 |
}); |
| 31 |
|
| 32 |
describe("test results", function () { |
| 33 |
beforeEach(function () { |
| 34 |
var options = {}; |
| 35 |
|
| 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" }; |
| 45 |
|
| 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 |
}); |
| 71 |
}); |
| 72 |
|
| 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, |
| 86 |
}, |
| 87 |
], |
| 88 |
}, |
| 89 |
}); |
| 90 |
expect(failureCount, "to be", 0); |
| 91 |
done(); |
| 92 |
}); |
| 93 |
}); |
| 94 |
|
| 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(); |
| 116 |
}); |
| 117 |
}); |
| 118 |
|
| 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(); |
| 151 |
}); |
| 152 |
}); |
| 153 |
}); |
| 154 |
|
| 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 |
|
| 166 |
var mochaReporter = new mocha._reporter(runner, options); |
| 167 |
}); |
| 168 |
|
| 169 |
beforeEach(function () { |
| 170 |
|
| 171 |
var test = new Test(testTitle, () => {}); |
| 172 |
test.file = testFile; |
| 173 |
suite.addTest(test); |
| 174 |
}); |
| 175 |
|
| 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); |
| 184 |
}); |
| 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 |
); |
| 231 |
}); |
| 232 |
}); |
| 233 |
}); |