| 1 | "use strict"; |
| 2 | |
| 3 | /** |
| 4 | * @typedef {import('../runner.js')} Runner |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @module JSON |
| 9 | */ |
| 10 | /** |
| 11 | * Module dependencies. |
| 12 | */ |
| 13 | |
| 14 | var Base = require("./base"); |
| 15 | var fs = require("node:fs"); |
| 16 | var path = require("node:path"); |
| 17 | const createUnsupportedError = require("../errors").createUnsupportedError; |
| 18 | const utils = require("../utils"); |
| 19 | var constants = require("../runner").constants; |
| 20 | var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; |
| 21 | var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; |
| 22 | var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; |
| 23 | var EVENT_TEST_END = constants.EVENT_TEST_END; |
| 24 | var EVENT_RUN_END = constants.EVENT_RUN_END; |
| 25 | |
| 26 | /** |
| 27 | * Expose `JSON`. |
| 28 | */ |
| 29 | |
| 30 | exports = module.exports = JSONReporter; |
| 31 | |
| 32 | /** |
| 33 | * Constructs a new `JSON` reporter instance. |
| 34 | * |
| 35 | * @public |
| 36 | * @class JSON |
| 37 | * @memberof Mocha.reporters |
| 38 | * @extends Mocha.reporters.Base |
| 39 | * @param {Runner} runner - Instance triggers reporter actions. |
| 40 | * @param {Object} [options] - runner options |
| 41 | */ |
| 42 | function JSONReporter(runner, options = {}) { |
| 43 | Base.call(this, runner, options); |
| 44 | |
| 45 | var self = this; |
| 46 | var tests = []; |
| 47 | var pending = []; |
| 48 | var failures = []; |
| 49 | var passes = []; |
| 50 | var output; |
| 51 | |
| 52 | if (options.reporterOption && options.reporterOption.output) { |
| 53 | if (utils.isBrowser()) { |
| 54 | throw createUnsupportedError("file output not supported in browser"); |
| 55 | } |
| 56 | output = options.reporterOption.output; |
| 57 | } |
| 58 | |
| 59 | runner.on(EVENT_TEST_END, function (test) { |
| 60 | tests.push(test); |
| 61 | }); |
| 62 | |
| 63 | runner.on(EVENT_TEST_PASS, function (test) { |
| 64 | passes.push(test); |
| 65 | }); |
| 66 | |
| 67 | runner.on(EVENT_TEST_FAIL, function (test) { |
| 68 | failures.push(test); |
| 69 | }); |
| 70 | |
| 71 | runner.on(EVENT_TEST_PENDING, function (test) { |
| 72 | pending.push(test); |
| 73 | }); |
| 74 | |
| 75 | runner.once(EVENT_RUN_END, function () { |
| 76 | var obj = { |
| 77 | stats: self.stats, |
| 78 | tests: tests.map(clean), |
| 79 | pending: pending.map(clean), |
| 80 | failures: failures.map(clean), |
| 81 | passes: passes.map(clean), |
| 82 | }; |
| 83 | |
| 84 | runner.testResults = obj; |
| 85 | |
| 86 | var json = JSON.stringify(obj, null, 2); |
| 87 | if (output) { |
| 88 | try { |
| 89 | fs.mkdirSync(path.dirname(output), { recursive: true }); |
| 90 | fs.writeFileSync(output, json); |
| 91 | } catch (err) { |
| 92 | console.error( |
| 93 | `${Base.symbols.err} [mocha] writing output to "${output}" failed: ${err.message}\n`, |
| 94 | ); |
| 95 | process.stdout.write(json); |
| 96 | } |
| 97 | } else { |
| 98 | process.stdout.write(json); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return a plain-object representation of `test` |
| 105 | * free of cyclic properties etc. |
| 106 | * |
| 107 | * @private |
| 108 | * @param {Object} test |
| 109 | * @return {Object} |
| 110 | */ |
| 111 | function clean(test) { |
| 112 | var err = test.err || {}; |
| 113 | if (err instanceof Error) { |
| 114 | err = errorJSON(err); |
| 115 | } |
| 116 | |
| 117 | return { |
| 118 | title: test.title, |
| 119 | fullTitle: test.fullTitle(), |
| 120 | file: test.file, |
| 121 | duration: test.duration, |
| 122 | currentRetry: test.currentRetry(), |
| 123 | speed: test.speed, |
| 124 | err: cleanCycles(err), |
| 125 | }; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Replaces any circular references inside `obj` with '[object Object]' |
| 130 | * |
| 131 | * @private |
| 132 | * @param {Object} obj |
| 133 | * @return {Object} |
| 134 | */ |
| 135 | function cleanCycles(obj) { |
| 136 | var cache = []; |
| 137 | return JSON.parse( |
| 138 | JSON.stringify(obj, function (key, value) { |
| 139 | if (typeof value === "object" && value !== null) { |
| 140 | if (cache.indexOf(value) !== -1) { |
| 141 | // Instead of going in a circle, we'll print [object Object] |
| 142 | return "" + value; |
| 143 | } |
| 144 | cache.push(value); |
| 145 | } |
| 146 | |
| 147 | return value; |
| 148 | }), |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Transform an Error object into a JSON object. |
| 154 | * |
| 155 | * @private |
| 156 | * @param {Error} err |
| 157 | * @return {Object} |
| 158 | */ |
| 159 | function errorJSON(err) { |
| 160 | var res = {}; |
| 161 | Object.getOwnPropertyNames(err).forEach(function (key) { |
| 162 | res[key] = err[key]; |
| 163 | }, err); |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | JSONReporter.description = "single JSON object"; |