fullJsonStreamReporter.cjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. /*---------------------------------------------------------
  3. * Copyright (C) Microsoft Corporation. All rights reserved.
  4. *--------------------------------------------------------*/
  5. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. var desc = Object.getOwnPropertyDescriptor(m, k);
  8. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  9. desc = { enumerable: true, get: function() { return m[k]; } };
  10. }
  11. Object.defineProperty(o, k2, desc);
  12. }) : (function(o, m, k, k2) {
  13. if (k2 === undefined) k2 = k;
  14. o[k2] = m[k];
  15. }));
  16. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  17. Object.defineProperty(o, "default", { enumerable: true, value: v });
  18. }) : function(o, v) {
  19. o["default"] = v;
  20. });
  21. var __importStar = (this && this.__importStar) || (function () {
  22. var ownKeys = function(o) {
  23. ownKeys = Object.getOwnPropertyNames || function (o) {
  24. var ar = [];
  25. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  26. return ar;
  27. };
  28. return ownKeys(o);
  29. };
  30. return function (mod) {
  31. if (mod && mod.__esModule) return mod;
  32. var result = {};
  33. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  34. __setModuleDefault(result, mod);
  35. return result;
  36. };
  37. })();
  38. var __exportStar = (this && this.__exportStar) || function(m, exports) {
  39. for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
  40. };
  41. Object.defineProperty(exports, "__esModule", { value: true });
  42. const Mocha = __importStar(require("mocha"));
  43. const util_1 = require("util");
  44. const fullJsonStreamReporterTypes_cjs_1 = require("./fullJsonStreamReporterTypes.cjs");
  45. __exportStar(require("./fullJsonStreamReporterTypes.cjs"), exports);
  46. /**
  47. * Similar to the mocha JSON stream, but includes additional information
  48. * on failure and when tests run. Specifically, the mocha json-stream does not
  49. * include unmangled expected versus actual results.
  50. *
  51. * Writes a superset of the data that json-stream normally would.
  52. */
  53. module.exports = class FullJsonStreamReporter {
  54. constructor(runner) {
  55. const total = runner.total;
  56. runner.once(Mocha.Runner.constants.EVENT_RUN_BEGIN, () => writeEvent([fullJsonStreamReporterTypes_cjs_1.MochaEvent.Start, { total }]));
  57. runner.once(Mocha.Runner.constants.EVENT_RUN_END, () => writeEvent([fullJsonStreamReporterTypes_cjs_1.MochaEvent.End, {}]));
  58. runner.on(Mocha.Runner.constants.EVENT_SUITE_BEGIN, (suite) => writeEvent([fullJsonStreamReporterTypes_cjs_1.MochaEvent.SuiteStart, { path: suite.titlePath(), file: suite.file }]));
  59. runner.on(Mocha.Runner.constants.EVENT_TEST_BEGIN, (test) => writeEvent([fullJsonStreamReporterTypes_cjs_1.MochaEvent.TestStart, clean(test)]));
  60. runner.on(Mocha.Runner.constants.EVENT_TEST_PASS, (test) => writeEvent([fullJsonStreamReporterTypes_cjs_1.MochaEvent.Pass, clean(test)]));
  61. runner.on(Mocha.Runner.constants.EVENT_TEST_FAIL, (test, err) => {
  62. writeEvent([
  63. fullJsonStreamReporterTypes_cjs_1.MochaEvent.Fail,
  64. {
  65. ...clean(test),
  66. actual: (0, util_1.inspect)(err.actual, { depth: 30 }),
  67. expected: (0, util_1.inspect)(err.expected, { depth: 30 }),
  68. err: err.message,
  69. stack: err.stack || null,
  70. },
  71. ]);
  72. });
  73. }
  74. };
  75. function writeEvent(event) {
  76. process.stdout.write(JSON.stringify(event) + '\n');
  77. }
  78. const clean = (test) => {
  79. return {
  80. path: test.titlePath(),
  81. duration: test.duration,
  82. currentRetry: test.currentRetry(),
  83. file: test.file,
  84. speed: !test.duration || test.duration < test.slow() / 2
  85. ? 'fast'
  86. : test.duration > test.slow()
  87. ? 'slow'
  88. : 'medium',
  89. };
  90. };