reporter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _jestMessageUtil = require('jest-message-util');
  7. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  8. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  9. var jestNow = global[Symbol.for('jest-native-now')] || global.Date.now;
  10. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  11. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  12. function _defineProperty(obj, key, value) {
  13. if (key in obj) {
  14. Object.defineProperty(obj, key, {
  15. value: value,
  16. enumerable: true,
  17. configurable: true,
  18. writable: true
  19. });
  20. } else {
  21. obj[key] = value;
  22. }
  23. return obj;
  24. }
  25. class Jasmine2Reporter {
  26. constructor(globalConfig, config, testPath) {
  27. _defineProperty(this, '_testResults', void 0);
  28. _defineProperty(this, '_globalConfig', void 0);
  29. _defineProperty(this, '_config', void 0);
  30. _defineProperty(this, '_currentSuites', void 0);
  31. _defineProperty(this, '_resolve', void 0);
  32. _defineProperty(this, '_resultsPromise', void 0);
  33. _defineProperty(this, '_startTimes', void 0);
  34. _defineProperty(this, '_testPath', void 0);
  35. this._globalConfig = globalConfig;
  36. this._config = config;
  37. this._testPath = testPath;
  38. this._testResults = [];
  39. this._currentSuites = [];
  40. this._resolve = null;
  41. this._resultsPromise = new Promise(resolve => (this._resolve = resolve));
  42. this._startTimes = new Map();
  43. }
  44. jasmineStarted(_runDetails) {}
  45. specStarted(spec) {
  46. this._startTimes.set(spec.id, jestNow());
  47. }
  48. specDone(result) {
  49. this._testResults.push(
  50. this._extractSpecResults(result, this._currentSuites.slice(0))
  51. );
  52. }
  53. suiteStarted(suite) {
  54. this._currentSuites.push(suite.description);
  55. }
  56. suiteDone(_result) {
  57. this._currentSuites.pop();
  58. }
  59. jasmineDone(_runDetails) {
  60. let numFailingTests = 0;
  61. let numPassingTests = 0;
  62. let numPendingTests = 0;
  63. let numTodoTests = 0;
  64. const testResults = this._testResults;
  65. testResults.forEach(testResult => {
  66. if (testResult.status === 'failed') {
  67. numFailingTests++;
  68. } else if (testResult.status === 'pending') {
  69. numPendingTests++;
  70. } else if (testResult.status === 'todo') {
  71. numTodoTests++;
  72. } else {
  73. numPassingTests++;
  74. }
  75. });
  76. const testResult = {
  77. console: null,
  78. failureMessage: (0, _jestMessageUtil.formatResultsErrors)(
  79. testResults,
  80. this._config,
  81. this._globalConfig,
  82. this._testPath
  83. ),
  84. numFailingTests,
  85. numPassingTests,
  86. numPendingTests,
  87. numTodoTests,
  88. perfStats: {
  89. end: 0,
  90. start: 0
  91. },
  92. snapshot: {
  93. added: 0,
  94. fileDeleted: false,
  95. matched: 0,
  96. unchecked: 0,
  97. unmatched: 0,
  98. updated: 0
  99. },
  100. testFilePath: this._testPath,
  101. testResults
  102. };
  103. this._resolve(testResult);
  104. }
  105. getResults() {
  106. return this._resultsPromise;
  107. }
  108. _addMissingMessageToStack(stack, message) {
  109. // Some errors (e.g. Angular injection error) don't prepend error.message
  110. // to stack, instead the first line of the stack is just plain 'Error'
  111. const ERROR_REGEX = /^Error:?\s*\n/;
  112. if (stack && message && !stack.includes(message)) {
  113. return message + stack.replace(ERROR_REGEX, '\n');
  114. }
  115. return stack;
  116. }
  117. _extractSpecResults(specResult, ancestorTitles) {
  118. const start = this._startTimes.get(specResult.id);
  119. const duration = start ? jestNow() - start : undefined;
  120. const status =
  121. specResult.status === 'disabled' ? 'pending' : specResult.status;
  122. const location = specResult.__callsite
  123. ? {
  124. column: specResult.__callsite.getColumnNumber(),
  125. line: specResult.__callsite.getLineNumber()
  126. }
  127. : null;
  128. const results = {
  129. ancestorTitles,
  130. duration,
  131. failureMessages: [],
  132. fullName: specResult.fullName,
  133. location,
  134. numPassingAsserts: 0,
  135. // Jasmine2 only returns an array of failed asserts.
  136. status,
  137. title: specResult.description
  138. };
  139. specResult.failedExpectations.forEach(failed => {
  140. const message =
  141. !failed.matcherName && failed.stack
  142. ? this._addMissingMessageToStack(failed.stack, failed.message)
  143. : failed.message || '';
  144. results.failureMessages.push(message);
  145. });
  146. return results;
  147. }
  148. }
  149. exports.default = Jasmine2Reporter;