reporter.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. const Promise = global[Symbol.for('jest-native-promise')] || global.Promise; /**
  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. class Jasmine2Reporter {
  17. constructor(globalConfig, config, environment, testPath) {
  18. this._globalConfig = globalConfig;
  19. this._config = config;
  20. this._testPath = testPath;
  21. this._testResults = [];
  22. this._currentSuites = [];
  23. this._resolve = null;
  24. this._resultsPromise = new Promise(resolve => this._resolve = resolve);
  25. this._startTimes = new Map();
  26. }
  27. specStarted(spec) {
  28. this._startTimes.set(spec.id, Date.now());
  29. }
  30. specDone(result) {
  31. this._testResults.push(this._extractSpecResults(result, this._currentSuites.slice(0)));
  32. }
  33. suiteStarted(suite) {
  34. this._currentSuites.push(suite.description);
  35. }
  36. suiteDone() {
  37. this._currentSuites.pop();
  38. }
  39. jasmineDone() {
  40. let numFailingTests = 0;
  41. let numPassingTests = 0;
  42. let numPendingTests = 0;
  43. const testResults = this._testResults;
  44. testResults.forEach(testResult => {
  45. if (testResult.status === 'failed') {
  46. numFailingTests++;
  47. } else if (testResult.status === 'pending') {
  48. numPendingTests++;
  49. } else {
  50. numPassingTests++;
  51. }
  52. });
  53. const testResult = {
  54. console: null,
  55. failureMessage: (0, _jestMessageUtil.formatResultsErrors)(testResults, this._config, this._globalConfig, this._testPath),
  56. numFailingTests,
  57. numPassingTests,
  58. numPendingTests,
  59. perfStats: {
  60. end: 0,
  61. start: 0
  62. },
  63. snapshot: {
  64. added: 0,
  65. fileDeleted: false,
  66. matched: 0,
  67. unchecked: 0,
  68. unmatched: 0,
  69. updated: 0
  70. },
  71. testFilePath: this._testPath,
  72. testResults
  73. };
  74. this._resolve(testResult);
  75. }
  76. getResults() {
  77. return this._resultsPromise;
  78. }
  79. _addMissingMessageToStack(stack, message) {
  80. // Some errors (e.g. Angular injection error) don't prepend error.message
  81. // to stack, instead the first line of the stack is just plain 'Error'
  82. const ERROR_REGEX = /^Error\s*\n/;
  83. if (stack && message && ERROR_REGEX.test(stack) && stack.indexOf(message) === -1) {
  84. return message + stack.replace(ERROR_REGEX, '\n');
  85. }
  86. return stack;
  87. }
  88. _extractSpecResults(specResult, ancestorTitles) {
  89. const start = this._startTimes.get(specResult.id);
  90. const duration = start ? Date.now() - start : undefined;
  91. const status = specResult.status === 'disabled' ? 'pending' : specResult.status;
  92. const location = specResult.__callsite ? {
  93. column: specResult.__callsite.getColumnNumber(),
  94. // $FlowFixMe: https://github.com/facebook/flow/issues/5213
  95. line: specResult.__callsite.getLineNumber()
  96. } : null;
  97. const results = {
  98. ancestorTitles,
  99. duration,
  100. failureMessages: [],
  101. fullName: specResult.fullName,
  102. location,
  103. numPassingAsserts: 0, // Jasmine2 only returns an array of failed asserts.
  104. status,
  105. title: specResult.description
  106. };
  107. specResult.failedExpectations.forEach(failed => {
  108. const message = !failed.matcherName && failed.stack ? this._addMissingMessageToStack(failed.stack, failed.message) : failed.message || '';
  109. results.failureMessages.push(message);
  110. });
  111. return results;
  112. }
  113. }
  114. exports.default = Jasmine2Reporter;