All files / repo-tools/src/vitest-reporters silent-reporter.ts

0% Statements 0/8
0% Branches 0/6
0% Functions 0/7
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                           
/**
 * Silent Vitest reporter based on the DotReporter implementation.
 * Outputs nothing during test execution and only displays errors at the end
 * if there are failures. Outputs nothing if all tests pass.
 *
 * @see https://github.com/vitest-dev/vitest/blob/20e00ef7808de6d330c5e2fda530f686e08f1c8d/packages/vitest/src/node/reporters/dot.ts
 */
import type { File as VitestFile } from '@vitest/runner';
import type {
  SerializedError,
  TestModule,
  TestRunEndReason,
} from 'vitest/node';
import { DotReporter } from 'vitest/reporters';
import type { BaseReporter } from 'vitest/reporters';
 
/**
 * A silent Vitest reporter that outputs nothing during test execution and
 * only displays errors at the end if there are failures. Outputs nothing if
 * all tests pass.
 */
export class SilentReporter extends DotReporter {
  /**
   * Initializes the reporter without printing the banner.
   *
   * @param ctx - The Vitest context.
   */
  onInit(ctx: (typeof this)['ctx']): void {
    // Set up context without calling super.onInit which prints the banner
    this.ctx = ctx;
  }
 
  /**
   * Suppresses dot output during test case completion.
   */
  onTestCaseResult(): void {
    // Silent - no dots during execution
  }
 
  /**
   * Suppresses module end output.
   */
  onTestModuleEnd(): void {
    // Silent - no output during execution
  }
 
  /**
   * Suppresses the final dot line that DotReporter prints.
   *
   * @param testModules - The test modules that were run.
   * @param unhandledErrors - Any unhandled errors that occurred.
   * @param reason - The reason the test run ended.
   */
  onTestRunEnd(
    testModules: readonly TestModule[],
    unhandledErrors: readonly SerializedError[],
    reason: TestRunEndReason,
  ): void {
    // Skip DotReporter's onTestRunEnd which prints dots,
    // call grandparent's onTestRunEnd directly via prototype.
    // Chain: this -> SilentReporter.prototype -> DotReporter.prototype -> BaseReporter.prototype
    const baseReporterProto = Object.getPrototypeOf(
      Object.getPrototypeOf(Object.getPrototypeOf(this)),
    ) as BaseReporter;
    baseReporterProto.onTestRunEnd.call(
      this,
      testModules,
      unhandledErrors,
      reason,
    );
  }
 
  /**
   * Reports summary only when there are failures.
   *
   * @param files - The test files that were run.
   * @param errors - Any errors that occurred during the run.
   */
  reportSummary(files: VitestFile[], errors: unknown[]): void {
    const hasFailed = files.some(
      (file) =>
        file.result?.state === 'fail' ||
        file.tasks.some((task) => task.result?.state === 'fail'),
    );
 
    if (hasFailed || errors.length > 0) {
      super.reportSummary(files, errors);
    }
    // Silent when all pass
  }
}
 
export default SilentReporter;