UNPKG

1.31 kBPlain TextView Raw
1import path from 'path';
2
3import { schema, StrykerOptions } from '@stryker-mutator/api/core';
4import { Logger } from '@stryker-mutator/api/logging';
5import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
6import { Reporter } from '@stryker-mutator/api/report';
7
8import fileUrl from 'file-url';
9
10import { reporterUtil } from './reporter-util.js';
11
12const INDENTION_LEVEL = 0;
13export const RESOURCES_DIR_NAME = 'strykerResources';
14
15export class JsonReporter implements Reporter {
16 private mainPromise: Promise<void> | undefined;
17
18 constructor(private readonly options: StrykerOptions, private readonly log: Logger) {}
19
20 public static readonly inject = tokens(commonTokens.options, commonTokens.logger);
21
22 public onMutationTestReportReady(report: schema.MutationTestResult): void {
23 this.mainPromise = this.generateReport(report);
24 }
25
26 public wrapUp(): Promise<void> | undefined {
27 return this.mainPromise;
28 }
29
30 private async generateReport(report: schema.MutationTestResult) {
31 const filePath = path.normalize(this.options.jsonReporter.fileName);
32 this.log.debug(`Using relative path ${filePath}`);
33 await reporterUtil.writeFile(path.resolve(filePath), JSON.stringify(report, null, INDENTION_LEVEL));
34 this.log.info(`Your report can be found at: ${fileUrl(filePath)}`);
35 }
36}