constructor()

in packages/synthetics-sdk-mocha/src/gcm_synthetics_mocha_reporter.ts [55:127]


  constructor(runner: Mocha.Runner, options: GcmSyntheticsReporterOptions) {
    const output = options?.reporterOption?.output;

    const testFrameworkResult: TestFrameworkResultV1 = {
      suite_count: 0,
      test_count: 0,
      passing_test_count: 0,
      pending_test_count: 0,
      failing_test_count: 0,
      test_results: [],
    };

    const syntheticResult: SyntheticResult = {
      start_time: '',
      end_time: '',
      runtime_metadata: {},
    };

    runner
      .on(EVENT_RUN_BEGIN, () => {
        syntheticResult.start_time = new Date().toISOString();
      })
      .on(EVENT_SUITE_BEGIN, (suite: Mocha.Suite) => {
        // Only add root suite when it has tests directly in it.
        if (!suite.root || suite.tests.length > 0) {
          testFrameworkResult.suite_count =
            (testFrameworkResult.suite_count ?? 0) + 1;
        }
      })
      .on(EVENT_TEST_BEGIN, () => {
        testFrameworkResult.test_count =
          (testFrameworkResult.test_count ?? 0) + 1;
      })
      .on(EVENT_TEST_PASS, (test: Mocha.Test) => {
        testFrameworkResult.test_results.push(serializeTest(test, undefined));
        testFrameworkResult.passing_test_count =
          (testFrameworkResult.passing_test_count ?? 0) + 1;
      })
      .on(EVENT_TEST_FAIL, (test: Mocha.Test, err: Error) => {
        testFrameworkResult.test_results.push(serializeTest(test, err));
        testFrameworkResult.failing_test_count =
          (testFrameworkResult.failing_test_count ?? 0) + 1;
      })
      .on(EVENT_TEST_PENDING, () => {
        testFrameworkResult.pending_test_count =
          (testFrameworkResult.pending_test_count ?? 0) + 1;
      })
      .on(EVENT_RUN_END, () => {
        syntheticResult.end_time = new Date().toISOString();
        syntheticResult.synthetic_test_framework_result_v1 =
          testFrameworkResult;

        const json = JSON.stringify(
          SyntheticResult.toJSON(syntheticResult),
          null,
          2
        );
        if (output) {
          try {
            fs.mkdirSync(path.dirname(output), { recursive: true });
            fs.writeFileSync(output, json);
          } catch (err: unknown) {
            const message = err instanceof Error ? err.message : '';
            process.stderr.write(
              `[mocha] writing output to '${output}' failed: ${message}\n`
            );
            process.stdout.write(`${json}\n`);
          }
        } else {
          process.stdout.write(`${json}\n`);
        }
      });
  }