All files reporter.js

83.78% Statements 31/37
88.89% Branches 16/18
100% Functions 3/3
83.78% Lines 31/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                6x                   6x   6x 6x   6x 6x 6x 6x 6x 6x   6x   6x 50x   50x 5x               5x                 50x 153x               153x           153x                   153x   153x                   153x   130x 130x   10x 10x   13x 13x               6x 6x        
//@flow
import path from 'path';
import { helpers } from './helpers';
import fs from 'fs';
import type { AggregatedResultWithoutCoverage } from './jest-types/TestResult';
 
 
/* eslint-disable no-unused-vars */
/**
 * A function that is used to process our jest reports.
 * @param report
 * @param contexts
 * @returns {AggregatedResultWithoutCoverage}
 */
function reporter(report : AggregatedResultWithoutCoverage, contexts? : Object) : AggregatedResultWithoutCoverage {
  /* eslint-enable no-unused-vars */
  const output = {
    stats: {},
    failures: [],
    passes: [],
    skipped: [],
  };
 
  /**
   * We will need to update this support defining a configuration.
   */
  const filename = process.env.JEST_REPORT_FILE || 'test-report.json';
  const suiteNameTemplate =
    process.env.JEST_BAMBOO_SUITE_NAME || '{firstAncestorTitle|filePath}';
  const nameSeparator = process.env.JEST_BAMBOO_NAME_SEPARATOR || ' – ';
 
  output.stats.tests = report.numTotalTests;
  output.stats.passes = report.numPassedTests;
  output.stats.failures = report.numFailedTests;
  output.stats.duration = Date.now() - report.startTime;
  output.stats.start = new Date(report.startTime);
  output.stats.end = new Date();
 
  const existingTestTitles = Object.create(null);
 
  report.testResults.forEach(function(suiteResult) {
    const testFileName = path.basename(suiteResult.testFilePath);
 
    if (suiteResult.failureMessage && suiteResult.testResults.length === 0) {
      const suiteName = helpers.replaceCharsNotSupportedByBamboo(
        helpers.replaceVariables(suiteNameTemplate, {
          firstAncestorTitle: suiteResult.displayName,
          filePath: suiteResult.testFilePath,
          fileName: testFileName,
          fileNameWithoutExtension: path.parse(testFileName).name,
        }),
      );
      output.failures.push({
        title: suiteName,
        fullTitle: suiteName,
        duration: suiteResult.perfStats.end - suiteResult.perfStats.start,
        errorCount: 1,
        error: suiteResult.failureMessage,
      });
    }
 
    suiteResult.testResults.forEach(function(testResult) {
      const suiteName = helpers.replaceCharsNotSupportedByBamboo(
        helpers.replaceVariables(suiteNameTemplate, {
          firstAncestorTitle: testResult.ancestorTitles[0],
          filePath: suiteResult.testFilePath,
          fileName: testFileName,
          fileNameWithoutExtension: path.parse(testFileName).name,
        }),
      );
      let testTitle = helpers.replaceCharsNotSupportedByBamboo(
        testResult.ancestorTitles
          .concat([testResult.title])
          .join(nameSeparator),
      );
 
      Iif (testTitle in existingTestTitles) {
        let newTestTitle;
        let counter = 1;
        do {
          counter++;
          newTestTitle = testTitle + ' (' + counter + ')';
        } while (newTestTitle in existingTestTitles);
        testTitle = newTestTitle;
      }
 
      existingTestTitles[testTitle] = true;
 
      var result = {
        title: testTitle,
        fullTitle: suiteName,
        duration: suiteResult.perfStats.end - suiteResult.perfStats.start,
        errorCount: testResult.failureMessages.length,
        error: testResult.failureMessages.length
          ? helpers.formatErrorMessages(testResult.failureMessages)
          : undefined,
      };
 
      switch (testResult.status) {
        case 'passed':
          output.passes.push(result);
          break;
        case 'failed':
          output.failures.push(result);
          break;
        case 'pending':
          output.skipped.push(result);
          break;
        default:
          throw new Error(
            'Unexpected test result status: ' + testResult.status,
          );
      }
    });
  });
  fs.writeFileSync(filename, JSON.stringify(output, null, 2), 'utf8');
  return report;
}
 
export { reporter };