From f9ef08e90d78800bfe226753f3ace612a83bef82 Mon Sep 17 00:00:00 2001 From: Mart <119670595+mcdr2k@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:13:51 +0200 Subject: [PATCH] fix(mocha-runner): properly restrict tests to run Correctly restrict tests to run by including the start and end characters (`^` and `$`) inside the `--grep` regex. --- packages/mocha-runner/src/mocha-test-runner.ts | 2 +- packages/mocha-runner/test/unit/mocha-test-runner.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mocha-runner/src/mocha-test-runner.ts b/packages/mocha-runner/src/mocha-test-runner.ts index 2c7b148608..b04b29e393 100644 --- a/packages/mocha-runner/src/mocha-test-runner.ts +++ b/packages/mocha-runner/src/mocha-test-runner.ts @@ -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 { diff --git a/packages/mocha-runner/test/unit/mocha-test-runner.spec.ts b/packages/mocha-runner/test/unit/mocha-test-runner.spec.ts index 9a3a499aba..d4c2b71e96 100644 --- a/packages/mocha-runner/test/unit/mocha-test-runner.spec.ts +++ b/packages/mocha-runner/test/unit/mocha-test-runner.spec.ts @@ -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 () => {