|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { |
| 9 | + AggregatedResult, |
| 10 | + Test, |
| 11 | + TestCaseResult, |
| 12 | + TestResult, |
| 13 | +} from '@jest/test-result'; |
| 14 | +import BaseReporter from './BaseReporter'; |
| 15 | +import DefaultReporter from './DefaultReporter'; |
| 16 | +import type {ReporterOnStartOptions} from './types'; |
| 17 | + |
| 18 | +/** |
| 19 | + * A reporter optimized for AI coding agents that reduces token usage by only |
| 20 | + * printing failing tests and the final summary. Automatically activated when |
| 21 | + * an AI agent environment is detected (via the AI_AGENT env var or std-env). |
| 22 | + */ |
| 23 | +export default class AgentReporter extends DefaultReporter { |
| 24 | + static override readonly filename = __filename; |
| 25 | + |
| 26 | + /* eslint-disable @typescript-eslint/no-empty-function */ |
| 27 | + protected override __wrapStdio(): void {} |
| 28 | + protected override __clearStatus(): void {} |
| 29 | + protected override __printStatus(): void {} |
| 30 | + override onTestStart(_test: Test): void {} |
| 31 | + override onTestCaseResult( |
| 32 | + _test: Test, |
| 33 | + _testCaseResult: TestCaseResult, |
| 34 | + ): void {} |
| 35 | + /* eslint-enable */ |
| 36 | + |
| 37 | + override onRunStart( |
| 38 | + aggregatedResults: AggregatedResult, |
| 39 | + options: ReporterOnStartOptions, |
| 40 | + ): void { |
| 41 | + BaseReporter.prototype.onRunStart.call(this, aggregatedResults, options); |
| 42 | + } |
| 43 | + |
| 44 | + override onTestResult( |
| 45 | + test: Test, |
| 46 | + testResult: TestResult, |
| 47 | + aggregatedResults: AggregatedResult, |
| 48 | + ): void { |
| 49 | + this.testFinished(test.context.config, testResult, aggregatedResults); |
| 50 | + |
| 51 | + // Only print output for test files that have failures. |
| 52 | + if ( |
| 53 | + !testResult.skipped && |
| 54 | + (testResult.numFailingTests > 0 || testResult.testExecError) |
| 55 | + ) { |
| 56 | + this.printTestFileHeader( |
| 57 | + testResult.testFilePath, |
| 58 | + test.context.config, |
| 59 | + testResult, |
| 60 | + ); |
| 61 | + this.printTestFileFailureMessage( |
| 62 | + testResult.testFilePath, |
| 63 | + test.context.config, |
| 64 | + testResult, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + this.forceFlushBufferedOutput(); |
| 69 | + } |
| 70 | +} |
0 commit comments