| 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 Basescip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/Base. = require("./base"scip-typescript npm mocha 11.7.7 lib/reporters/`base.js`/); |
| 15 | var fsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/fs. = require("node:fs"); |
| 16 | var pathscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/path. = require("node:path"); |
| 17 | const createUnsupportedErrorscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/createUnsupportedError. = require("../errors"scip-typescript npm mocha 11.7.7 lib/`errors.js`/).createUnsupportedErrorscip-typescript npm mocha 11.7.7 lib/`errors.js`/createUnsupportedError0:; |
| 18 | const utilsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/utils. = require("../utils"scip-typescript npm mocha 11.7.7 lib/`utils.js`/); |
| 19 | var constantsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/constants. = require("../runner"scip-typescript npm mocha 11.7.7 lib/`runner.js`/).constants; |
| 20 | var EVENT_TEST_PASSscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_PASS. = constants.EVENT_TEST_PASS; |
| 21 | var EVENT_TEST_PENDINGscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_PENDING. = constants.EVENT_TEST_PENDING; |
| 22 | var EVENT_TEST_FAILscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_FAIL. = constants.EVENT_TEST_FAIL; |
| 23 | var EVENT_TEST_ENDscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_END. = constants.EVENT_TEST_END; |
| 24 | var EVENT_RUN_ENDscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_RUN_END. = constants.EVENT_RUN_END; |
| 25 | |
| 26 | /** |
| 27 | * Expose `JSON`. |
| 28 | */ |
| 29 | |
| 30 | exportsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/ = module.exports = JSONReporterscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/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 JSONReporterscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner), optionsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(options) = {}) { |
| 43 | Basescip-typescript npm mocha 11.7.7 lib/reporters/`base.js`/Base()..callscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Function#call().(this, runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner), optionsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(options)); |
| 44 | |
| 45 | var self = this; |
| 46 | var tests = []; |
| 47 | var pending = []; |
| 48 | var failures = []; |
| 49 | var passes = []; |
| 50 | var output; |
| 51 | |
| 52 | if (optionsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(options).reporterOption && optionsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(options).reporterOption.output) { |
| 53 | if (utilsscip-typescript npm mocha 11.7.7 lib/`utils.js`/.isBrowser()) { |
| 54 | throw createUnsupportedErrorscip-typescript npm mocha 11.7.7 lib/`errors.js`/createUnsupportedError().("file output not supported in browser"); |
| 55 | } |
| 56 | output = optionsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(options).reporterOption.output; |
| 57 | } |
| 58 | |
| 59 | runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner).on(EVENT_TEST_ENDscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_END., function (test) { |
| 60 | tests.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(test); |
| 61 | }); |
| 62 | |
| 63 | runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner).on(EVENT_TEST_PASSscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_PASS., function (test) { |
| 64 | passes.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(test); |
| 65 | }); |
| 66 | |
| 67 | runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner).on(EVENT_TEST_FAILscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_FAIL., function (test) { |
| 68 | failures.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(test); |
| 69 | }); |
| 70 | |
| 71 | runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner).on(EVENT_TEST_PENDINGscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_TEST_PENDING., function (test) { |
| 72 | pending.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(test); |
| 73 | }); |
| 74 | |
| 75 | runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner).once(EVENT_RUN_ENDscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/EVENT_RUN_END., function () { |
| 76 | var obj = { |
| 77 | statsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/stats0:: self.stats, |
| 78 | testsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/tests0:: tests.mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(cleanscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().), |
| 79 | pendingscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/pending0:: pending.mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(cleanscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().), |
| 80 | failuresscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/failures0:: failures.mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(cleanscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().), |
| 81 | passesscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/passes0:: passes.mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(cleanscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().), |
| 82 | }; |
| 83 | |
| 84 | runnerscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter().(runner).testResults = obj; |
| 85 | |
| 86 | var json = JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(obj, null, 2); |
| 87 | if (output) { |
| 88 | try { |
| 89 | fsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/fs..mkdirSync(pathscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/path..dirname(output), { recursivescip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/recursive0:: true }); |
| 90 | fsscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/fs..writeFileSync(output, json); |
| 91 | } catch (err) { |
| 92 | consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..errorscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#error().( |
| 93 | `${Basescip-typescript npm mocha 11.7.7 lib/reporters/`base.js`/Base()..symbols.errscip-typescript npm mocha 11.7.7 lib/reporters/`base.js`/err0:} [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 cleanscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test)) { |
| 112 | var err = testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).err || {}; |
| 113 | if (err instanceof Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#) { |
| 114 | err = errorJSONscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/errorJSON().(err); |
| 115 | } |
| 116 | |
| 117 | return { |
| 118 | titlescip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/title0:: testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).title, |
| 119 | fullTitlescip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/fullTitle0:: testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).fullTitle(), |
| 120 | filescip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/file0:: testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).file, |
| 121 | durationscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/duration0:: testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).duration, |
| 122 | currentRetryscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/currentRetry0:: testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).currentRetry(), |
| 123 | speedscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/speed0:: testscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/clean().(test).speed, |
| 124 | errscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/err0:: cleanCyclesscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/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 cleanCyclesscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/cleanCycles().(objscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/cleanCycles().(obj)) { |
| 136 | var cache = []; |
| 137 | return JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.parsescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#parse().( |
| 138 | JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(objscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/cleanCycles().(obj), function (key, value) { |
| 139 | if (typeof value === "object" && value !== null) { |
| 140 | if (cache.indexOfscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#indexOf().(value) !== -1) { |
| 141 | // Instead of going in a circle, we'll print [object Object] |
| 142 | return "" + value; |
| 143 | } |
| 144 | cache.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#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 errorJSONscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/errorJSON().(errscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/errorJSON().(err)) { |
| 160 | var res = {}; |
| 161 | Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.getOwnPropertyNamesscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ObjectConstructor#getOwnPropertyNames().(errscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/errorJSON().(err)).forEachscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#forEach().(function (key) { |
| 162 | res[key] = errscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/errorJSON().(err)[key]; |
| 163 | }, errscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/errorJSON().(err)); |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | JSONReporterscip-typescript npm mocha 11.7.7 lib/reporters/`json.js`/JSONReporter()..description = "single JSON object"; |