Skip to content

Commit

Permalink
fix(mocha-runner): properly restrict tests to run
Browse files Browse the repository at this point in the history
Correctly restrict tests to run by including the start and end characters (`^` and `$`) inside the `--grep` regex.
  • Loading branch information
mcdr2k authored Aug 1, 2024
1 parent 23dcc8f commit f9ef08e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/mocha-runner/src/mocha-test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class MochaTestRunner implements TestRunner {
this.instrumenterContext.hitLimit = hitLimit;
this.instrumenterContext.hitCount = hitLimit ? 0 : undefined;
if (testFilter) {
const metaRegExp = testFilter.map((testId) => `(${escapeRegExp(testId)})`).join('|');
const metaRegExp = testFilter.map((testId) => `(^${escapeRegExp(testId)}$)`).join('|');
const regex = new RegExp(metaRegExp);
this.mocha.grep(regex);
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/mocha-runner/test/unit/mocha-test-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ describe(MochaTestRunner.name, () => {

it('should use `grep` to when the test filter is specified', async () => {
await actMutantRun(factory.mutantRunOptions({ testFilter: ['foo should be bar', 'baz should be qux'] }));
expect(mocha.grep).calledWith(new RegExp('(foo should be bar)|(baz should be qux)'));
expect(mocha.grep).calledWith(new RegExp('(^foo should be bar$)|(^baz should be qux$)'));
});

it('should escape regex characters when filtering', async () => {
await actMutantRun(factory.mutantRunOptions({ testFilter: ['should escape *.\\, but not /'] }));
expect(mocha.grep).calledWith(new RegExp('(should escape \\*\\.\\\\, but not /)'));
expect(mocha.grep).calledWith(new RegExp('(^should escape \\*\\.\\\\, but not /$)'));
});

it('should be able to report a killed mutant when a test fails', async () => {
Expand Down

0 comments on commit f9ef08e

Please sign in to comment.