Skip to content

Commit

Permalink
feat(ClearTextReporter): Limit the number of tests
Browse files Browse the repository at this point in the history
Make the number of tests logged by the `'clear-text'` reporter configurable using `clearTextReporter: { maxTestsToLog: 3 }`. Default is 3.
  • Loading branch information
simondel authored and nicojs committed Dec 28, 2016
1 parent 931c35f commit 142de71
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ These reporters can be used out of the box: `clear-text`, `progress` and `event-
By default `clear-text` and `progress` are active if no reporter is configured.
You can load additional plugins to get more reporters. See [stryker-mutator.github.io](http://stryker-mutator.github.io)
for an up-to-date list of supported reporter plugins and a description on each reporter.

The `clear-text` reporter supports an additional config option to show more tests that were executed to kill a mutant. The config for your config file is: `clearTextReporter: { maxTestsToLog: 3 },`

#### Plugins
**Command line:** `--plugins stryker-html-reporter,stryker-karma-runner`
Expand Down
30 changes: 26 additions & 4 deletions src/reporters/ClearTextReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,35 @@ export default class ClearTextReporter implements Reporter {
});
logImplementation('');
if (this.options.coverageAnalysis === 'perTest') {
if (result.testsRan && result.testsRan.length > 0) {
this.logExecutedTests(result, logImplementation);
} else if (result.testsRan && result.testsRan.length > 0) {
logImplementation('Ran all tests for this mutant.');
}
}

private logExecutedTests(result: MutantResult, logImplementation: (input: string) => void) {
const clearTextReporterConfig = this.options['clearTextReporter'];

if (result.testsRan && result.testsRan.length > 0) {
let testsToLog = 3;
if (clearTextReporterConfig && typeof clearTextReporterConfig.maxTestsToLog === 'number') {
testsToLog = clearTextReporterConfig.maxTestsToLog;
}

if (testsToLog > 0) {
logImplementation('Tests ran: ');
result.testsRan.forEach(spec => logImplementation(' ' + spec));
for (let i = 0; i < testsToLog; i++) {
if (i > result.testsRan.length - 1) {
break;
}

logImplementation(' ' + result.testsRan[i]);
}
if (testsToLog < result.testsRan.length) {
logImplementation(` and ${result.testsRan.length - testsToLog} more tests!`);
}
logImplementation('');
}
} else if (result.testsRan && result.testsRan.length > 0) {
logImplementation('Ran all tests for this mutant.');
}
}

Expand Down
67 changes: 60 additions & 7 deletions test/unit/reporters/ClearTextReporterSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ClearTextReporter', function () {
beforeEach(() => {
sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));
});
it('should not report the error', () => {
it('should not report the error', () => {
expect(process.stdout.write).to.not.have.been.calledWithMatch(sinon.match(/error/));
});
});
Expand Down Expand Up @@ -60,16 +60,69 @@ describe('ClearTextReporter', function () {

describe('onAllMutantsTested()', () => {

beforeEach(() => {
sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));
});

it('should log individual ran tests', () => {
sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));
expect(process.stdout.write).to.have.been.calledWith('Tests ran: \n');
expect(process.stdout.write).to.have.been.calledWith(' a test\n');
expect(process.stdout.write).to.have.been.calledWith(' a second test\n');
expect(process.stdout.write).to.have.been.calledWith(' a third test\n');
expect(process.stdout.write).to.not.have.been.calledWith('Ran all tests for this mutant.\n');
});

describe('with less tests that may be logged', () => {
it('should log less tests', () => {
sut = new ClearTextReporter({ coverageAnalysis: 'perTest', clearTextReporter: { maxTestsToLog: 1} });

sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));

expect(process.stdout.write).to.have.been.calledWith('Tests ran: \n');
expect(process.stdout.write).to.have.been.calledWith(' a test\n');
expect(process.stdout.write).to.have.been.calledWith(' and 2 more tests!\n');
expect(process.stdout.write).to.not.have.been.calledWith('Ran all tests for this mutant.\n');
});
});

describe('with more tests that may be logged', () => {
it('should log all tests', () => {
sut = new ClearTextReporter({ coverageAnalysis: 'perTest', clearTextReporter: { maxTestsToLog: 10} });

sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));

expect(process.stdout.write).to.have.been.calledWith('Tests ran: \n');
expect(process.stdout.write).to.have.been.calledWith(' a test\n');
expect(process.stdout.write).to.have.been.calledWith(' a second test\n');
expect(process.stdout.write).to.have.been.calledWith(' a third test\n');
expect(process.stdout.write).to.not.have.been.calledWith('Ran all tests for this mutant.\n');
});
});

describe('with the default amount of tests that may be logged', () => {
it('should log all tests', () => {
sut = new ClearTextReporter({ coverageAnalysis: 'perTest', clearTextReporter: { maxTestsToLog: 3} });

sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));

expect(process.stdout.write).to.have.been.calledWith('Tests ran: \n');
expect(process.stdout.write).to.have.been.calledWith(' a test\n');
expect(process.stdout.write).to.have.been.calledWith(' a second test\n');
expect(process.stdout.write).to.have.been.calledWith(' a third test\n');
expect(process.stdout.write).to.not.have.been.calledWith('Ran all tests for this mutant.\n');
});
});

describe('with no tests that may be logged', () => {
it('should not log a test', () => {
sut = new ClearTextReporter({ coverageAnalysis: 'perTest', clearTextReporter: { maxTestsToLog: 0} });

sut.onAllMutantsTested(mutantResults(MutantStatus.Killed, MutantStatus.Survived, MutantStatus.TimedOut, MutantStatus.NoCoverage));

expect(process.stdout.write).to.not.have.been.calledWith('Tests ran: \n');
expect(process.stdout.write).to.not.have.been.calledWith(' a test\n');
expect(process.stdout.write).to.not.have.been.calledWith(' a second test\n');
expect(process.stdout.write).to.not.have.been.calledWith(' a third test\n');
expect(process.stdout.write).to.not.have.been.calledWith('Ran all tests for this mutant.\n');
});
});
});
});

Expand All @@ -91,7 +144,7 @@ describe('ClearTextReporter', function () {
expect(process.stdout.write).to.have.been.calledWith(`Mutation score based on covered code: n/a\n`));

it('should report the average amount of tests ran', () =>
expect(process.stdout.write).to.have.been.calledWith(`Ran 2.00 tests per mutant on average.\n`));
expect(process.stdout.write).to.have.been.calledWith(`Ran 3.00 tests per mutant on average.\n`));
});
});

Expand All @@ -106,7 +159,7 @@ describe('ClearTextReporter', function () {
originalLines: 'original line',
replacement: '',
sourceFilePath: '',
testsRan: ['a test', 'a second test'],
testsRan: ['a test', 'a second test', 'a third test'],
status
};
});
Expand Down

0 comments on commit 142de71

Please sign in to comment.