Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(karma-runner): mocha filtering with / #2290

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as url from 'url';
import { RequestHandler } from 'express';
import { CoverageAnalysis } from '@stryker-mutator/api/core';
import { MutantRunOptions } from '@stryker-mutator/api/test_runner2';
import { escapeRegExp } from '@stryker-mutator/util';
import { escapeRegExpLiteral } from '@stryker-mutator/util';

export const TEST_HOOKS_FILE_NAME = __filename;

Expand Down Expand Up @@ -62,7 +62,7 @@ export default class TestHooksMiddleware {
}})`;
break;
case 'mocha':
const metaRegExp = testFilter.map((testId) => `(${escapeRegExp(testId)})`).join('|');
const metaRegExp = testFilter.map((testId) => `(${escapeRegExpLiteral(testId)})`).join('|');
this.currentTestHooks += `mocha.grep(/${metaRegExp}/)`;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ describe(TestHooksMiddleware.name, () => {
sut.configureActiveMutant(factory.mutantRunOptions({ testFilter: ['foo.spec', 'bar?spec'] }));
expect(sut.currentTestHooks).contains('mocha.grep(/(foo\\.spec)|(bar\\?spec)/)');
});
it('should escape `/` in the regex literal', () => {
sut.configureTestFramework(['mocha']);
sut.configureActiveMutant(
factory.mutantRunOptions({ testFilter: ['MutationTestReportTotalsComponent should show N/A when no mutation score is available'] })
);
expect(sut.currentTestHooks).contains(
'mocha.grep(/(MutationTestReportTotalsComponent should show N\\/A when no mutation score is available)/)'
);
});
});

describe('handler', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export function propertyPath<T>(prop: keyof Pick<T, KnownKeys<T>>, prop2?: keyof
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
*/
export function escapeRegExp(input: string) {
return input.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
export function escapeRegExpLiteral(input: string) {
return input.replace(/[.*+\-?^${}()|[\]\\/]/g, '\\$&'); // $& means the whole matched string
}
10 changes: 5 additions & 5 deletions packages/util/test/unit/stringUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

import { normalizeWhitespaces, propertyPath, escapeRegExp } from '../../src';
import { normalizeWhitespaces, propertyPath, escapeRegExpLiteral } from '../../src';

describe('stringUtils', () => {
describe(normalizeWhitespaces.name, () => {
Expand Down Expand Up @@ -29,14 +29,14 @@ describe('stringUtils', () => {
});
});

describe(escapeRegExp.name, () => {
describe(escapeRegExpLiteral.name, () => {
it('should return input if no special chars are found', () => {
expect(escapeRegExp('something normal')).eq('something normal');
expect(escapeRegExpLiteral('something normal')).eq('something normal');
});

for (const letter of '.*+-?^${}()|[]\\') {
for (const letter of '.*+-?^${}()|[]\\/') {
it(`should escape "${letter}"`, () => {
expect(escapeRegExp(letter)).eq(`\\${letter}`);
expect(escapeRegExpLiteral(letter)).eq(`\\${letter}`);
});
}
});
Expand Down