Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resporter): add json reporter (#2582)
Add support for `--reporters json`. It writes the mutation testing report to a JSON file, which validates against the [mutation-testing-report-schema](https://github.com/stryker-mutator/mutation-testing-elements/tree/master/packages/mutation-testing-report-schema). The default location for this file is `reports/mutation/mutation.json`, but you can change this location, see README.md for more details.
- Loading branch information
1 parent
494e821
commit d18c4aa
Showing
8 changed files
with
147 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as path from 'path'; | ||
|
||
import { StrykerOptions } from '@stryker-mutator/api/core'; | ||
import { Logger } from '@stryker-mutator/api/logging'; | ||
import { commonTokens, tokens } from '@stryker-mutator/api/plugin'; | ||
import { mutationTestReportSchema, Reporter } from '@stryker-mutator/api/report'; | ||
|
||
import fileUrl = require('file-url'); | ||
|
||
import * as ReporterUtil from './reporter-util'; | ||
|
||
const INDENTION_LEVEL = 0; | ||
export const RESOURCES_DIR_NAME = 'strykerResources'; | ||
|
||
export default class JsonReporter implements Reporter { | ||
private mainPromise: Promise<void> | undefined; | ||
|
||
constructor(private readonly options: StrykerOptions, private readonly log: Logger) {} | ||
|
||
public static readonly inject = tokens(commonTokens.options, commonTokens.logger); | ||
|
||
public onMutationTestReportReady(report: mutationTestReportSchema.MutationTestResult) { | ||
this.mainPromise = this.generateReport(report); | ||
} | ||
|
||
public wrapUp() { | ||
return this.mainPromise; | ||
} | ||
|
||
private async generateReport(report: mutationTestReportSchema.MutationTestResult) { | ||
const filePath = path.normalize(this.options.jsonReporter.fileName); | ||
this.log.debug(`Using relative path ${filePath}`); | ||
await ReporterUtil.writeFile(path.resolve(filePath), JSON.stringify(report, null, INDENTION_LEVEL)); | ||
this.log.info(`Your report can be found at: ${fileUrl(filePath)}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as path from 'path'; | ||
|
||
import { mutationTestReportSchema } from '@stryker-mutator/api/report'; | ||
import { testInjector } from '@stryker-mutator/test-helpers'; | ||
import { expect } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
import JsonReporter from '../../../src/reporters/json-reporter'; | ||
import * as JsonReporterUtil from '../../../src/reporters/reporter-util'; | ||
|
||
describe(JsonReporter.name, () => { | ||
let writeFileStub: sinon.SinonStub; | ||
let sut: JsonReporter; | ||
|
||
beforeEach(() => { | ||
writeFileStub = sinon.stub(JsonReporterUtil, 'writeFile'); | ||
sut = testInjector.injector.injectClass(JsonReporter); | ||
}); | ||
|
||
describe('onMutationTestReportReady', () => { | ||
it('should use configured file path', async () => { | ||
const fileName = 'foo/bar/myReport.json'; | ||
const expectedPath = path.normalize(fileName); | ||
testInjector.options.jsonReporter = { | ||
fileName, | ||
}; | ||
actReportReady(); | ||
await sut.wrapUp(); | ||
expect(testInjector.logger.debug).calledWith(`Using relative path ${expectedPath}`); | ||
}); | ||
|
||
it('should use default base directory when no override is configured', async () => { | ||
const expectedPath = path.normalize('reports/mutation/mutation.json'); | ||
actReportReady(); | ||
await sut.wrapUp(); | ||
expect(testInjector.logger.debug).calledWith(`Using relative path ${expectedPath}`); | ||
}); | ||
|
||
it('should write the mutation report to disk', async () => { | ||
const report: mutationTestReportSchema.MutationTestResult = { | ||
files: {}, | ||
schemaVersion: '1.0', | ||
thresholds: { | ||
high: 80, | ||
low: 60, | ||
}, | ||
}; | ||
sut.onMutationTestReportReady(report); | ||
await sut.wrapUp(); | ||
expect(writeFileStub).calledWith(path.resolve('reports', 'mutation', 'mutation.json'), JSON.stringify(report)); | ||
}); | ||
}); | ||
|
||
describe('wrapUp', () => { | ||
it('should resolve when everything is OK', () => { | ||
actReportReady(); | ||
return expect(sut.wrapUp()).eventually.undefined; | ||
}); | ||
|
||
it('should reject when "writeFile" rejects', () => { | ||
const expectedError = new Error('writeFile'); | ||
writeFileStub.rejects(expectedError); | ||
actReportReady(); | ||
return expect(sut.wrapUp()).rejectedWith(expectedError); | ||
}); | ||
}); | ||
|
||
function actReportReady() { | ||
sut.onMutationTestReportReady({ files: {}, schemaVersion: '', thresholds: { high: 0, low: 0 } }); | ||
} | ||
}); |