reporter.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _jestMessageUtil = require('jest-message-util');
  6. // Try getting the real promise object from the context, if available. Someone
  7. // could have overridden it in a test.
  8. /**
  9. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. *
  14. *
  15. */
  16. const Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  17. class Jasmine2Reporter {
  18. constructor(globalConfig, config, testPath) {
  19. this._globalConfig = globalConfig;
  20. this._config = config;
  21. this._testPath = testPath;
  22. this._testResults = [];
  23. this._currentSuites = [];
  24. this._resolve = null;
  25. this._resultsPromise = new Promise(resolve => (this._resolve = resolve));
  26. this._startTimes = new Map();
  27. }
  28. specStarted(spec) {
  29. this._startTimes.set(spec.id, Date.now());
  30. }
  31. specDone(result) {
  32. this._testResults.push(
  33. this._extractSpecResults(result, this._currentSuites.slice(0))
  34. );
  35. }
  36. suiteStarted(suite) {
  37. this._currentSuites.push(suite.description);
  38. }
  39. suiteDone() {
  40. this._currentSuites.pop();
  41. }
  42. jasmineDone() {
  43. let numFailingTests = 0;
  44. let numPassingTests = 0;
  45. let numPendingTests = 0;
  46. const testResults = this._testResults;
  47. testResults.forEach(testResult => {
  48. if (testResult.status === 'failed') {
  49. numFailingTests++;
  50. } else if (testResult.status === 'pending') {
  51. numPendingTests++;
  52. } else {
  53. numPassingTests++;
  54. }
  55. });
  56. const testResult = {
  57. console: null,
  58. failureMessage: (0, _jestMessageUtil.formatResultsErrors)(
  59. testResults,
  60. this._config,
  61. this._globalConfig,
  62. this._testPath
  63. ),
  64. numFailingTests: numFailingTests,
  65. numPassingTests: numPassingTests,
  66. numPendingTests: numPendingTests,
  67. perfStats: {
  68. end: 0,
  69. start: 0
  70. },
  71. snapshot: {
  72. added: 0,
  73. fileDeleted: false,
  74. matched: 0,
  75. unchecked: 0,
  76. unmatched: 0,
  77. updated: 0
  78. },
  79. testFilePath: this._testPath,
  80. testResults: testResults
  81. };
  82. this._resolve(testResult);
  83. }
  84. getResults() {
  85. return this._resultsPromise;
  86. }
  87. _addMissingMessageToStack(stack, message) {
  88. // Some errors (e.g. Angular injection error) don't prepend error.message
  89. // to stack, instead the first line of the stack is just plain 'Error'
  90. const ERROR_REGEX = /^Error\s*\n/;
  91. if (
  92. stack &&
  93. message &&
  94. ERROR_REGEX.test(stack) &&
  95. stack.indexOf(message) === -1
  96. ) {
  97. return message + stack.replace(ERROR_REGEX, '\n');
  98. }
  99. return stack;
  100. }
  101. _extractSpecResults(specResult, ancestorTitles) {
  102. const start = this._startTimes.get(specResult.id);
  103. const duration = start ? Date.now() - start : undefined;
  104. const status =
  105. specResult.status === 'disabled' ? 'pending' : specResult.status;
  106. const location = specResult.__callsite
  107. ? {
  108. column: specResult.__callsite.getColumnNumber(),
  109. // $FlowFixMe: https://github.com/facebook/flow/issues/5213
  110. line: specResult.__callsite.getLineNumber()
  111. }
  112. : null;
  113. const results = {
  114. ancestorTitles: ancestorTitles,
  115. duration: duration,
  116. failureMessages: [],
  117. fullName: specResult.fullName,
  118. location: location,
  119. numPassingAsserts: 0, // Jasmine2 only returns an array of failed asserts.
  120. status: status,
  121. title: specResult.description
  122. };
  123. specResult.failedExpectations.forEach(failed => {
  124. const message =
  125. !failed.matcherName && failed.stack
  126. ? this._addMissingMessageToStack(failed.stack, failed.message)
  127. : failed.message || '';
  128. results.failureMessages.push(message);
  129. });
  130. return results;
  131. }
  132. }
  133. exports.default = Jasmine2Reporter;