Skip to content

Commit

Permalink
feat(mocha-runner): report the test's file name (#3504)
Browse files Browse the repository at this point in the history
Add support for reporting a test's file name in the mocha reporter. Reporting a file name was added in mocha 10. This usually results in the test file being reported. The test location isn't reported, so Stryker's HTML report won't be able to point to the exact location.

Note that the file being reported by mocha might not be your source file when using a `--buildCommand`. 

Mocha support filenames, see: mochajs/mocha@3dd1cdb

Co-authored-by: Nico Jansen <jansennico@gmail.com>
  • Loading branch information
IvanGoncharov and nicojs authored Jun 26, 2022
1 parent 90910cd commit 34d8e70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion e2e/test/reporters-e2e/verify/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Verify stryker has ran correctly', () => {
});
});
});
const createTestsRegex = () => /All tests\s* Add should be able to add two numbers \(killed 2\)/;
const createTestsRegex = () => /All tests\s*AddSpec\.js\s*\s* Add should be able to add two numbers \(killed 2\)/;
const createNoCoverageMutantRegex = () => /#6\.\s*\[NoCoverage\]/;
const createSurvivedMutantRegex = () => /#20\.\s*\[Survived\]/;
const createClearTextTableSummaryRowRegex = () => /All files\s*\|\s*64\.00\s*\|\s*16\s*\|\s*0\s*\|\s*1\s*\|\s*8\s*\|\s*0\s*\|/;
1 change: 1 addition & 0 deletions packages/mocha-runner/src/stryker-mocha-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class StrykerMochaReporter {
name: title,
status: TestStatus.Success,
timeSpentMs: this.timer.elapsedMs(),
fileName: test.file,
};
this.tests.push(result);
this.passedCount++;
Expand Down
24 changes: 14 additions & 10 deletions packages/mocha-runner/test/integration/sample-project.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('Running a sample project', () => {
});

afterEach(async () => {
if (sut) {
await sut.dispose();
}
await sandbox.dispose();
});

Expand All @@ -40,9 +43,6 @@ describe('Running a sample project', () => {
await sut.init();
});

afterEach(async () => {
await sut.dispose();
});
it('should report completed tests', async () => {
const runResult = await sut.dryRun(factory.dryRunOptions());
assertions.expectCompleted(runResult);
Expand All @@ -51,6 +51,17 @@ describe('Running a sample project', () => {
runResult.tests.forEach((t) => expect(t.timeSpentMs).to.be.greaterThan(-1).and.to.be.lessThan(1000));
});

it('should report test files', async () => {
const specFileName = resolveTestFile('MyMathSpec.js');
const runResult = await sut.dryRun(factory.dryRunOptions());
assertions.expectCompleted(runResult);
expect(runResult.tests[0].fileName).eq(specFileName);
expect(runResult.tests[1].fileName).eq(specFileName);
expect(runResult.tests[2].fileName).eq(specFileName);
expect(runResult.tests[3].fileName).eq(specFileName);
expect(runResult.tests[4].fileName).eq(specFileName);
});

it('should be able to run 2 times in a row', async () => {
await sut.dryRun(factory.dryRunOptions());
const runResult = await sut.dryRun(factory.dryRunOptions());
Expand All @@ -66,9 +77,6 @@ describe('Running a sample project', () => {
sut = createSut();
return sut.init();
});
afterEach(async () => {
await sut.dispose();
});

it('should only report the first failure (bail)', async () => {
const runResult = await sut.dryRun(factory.dryRunOptions());
Expand All @@ -90,10 +98,6 @@ describe('Running a sample project', () => {
sut = createSut();
return sut.init();
});

afterEach(async () => {
await sut.dispose();
});
it('should report no completed tests', async () => {
const runResult = await sut.dryRun(factory.dryRunOptions());
assertions.expectCompleted(runResult);
Expand Down

0 comments on commit 34d8e70

Please sign in to comment.