Skip to content

feat(jest-circus): support literal string match in testNamePattern with "@:" prefix #15667

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- `[jest-circus, jest-cli]` Support literal string test name matching via `@:` prefix in `testNamePattern` (Fixes [#15574](https://github.com/jestjs/jest/issues/15574)) ([#15667](https://github.com/jestjs/jest/pull/15667))
- `[*]` Renamed `globalsCleanupMode` to `globalsCleanup` and `--waitNextEventLoopTurnForUnhandledRejectionEvents` to `--waitForUnhandledRejections`
- `[expect]` Add `ArrayOf` asymmetric matcher for validating array elements. ([#15567](https://github.com/jestjs/jest/pull/15567))
- `[babel-jest]` Add option `excludeJestPreset` to allow opting out of `babel-preset-jest` ([#15164](https://github.com/jestjs/jest/pull/15164))
Expand Down
14 changes: 14 additions & 0 deletions e2e/literal-pattern/__tests__/literal_pattern.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Only runs if the name is exactly 'literal match'
test('literal match', () => {
expect(true).toBe(true);
});

// Includes special character '*', useful for literal matching test
test('check * literal asterisk', () => {
expect(1).toBe(1);
});

// Includes parentheses, used to test literal handling of regex-sensitive characters
test('special (characters)', () => {
expect('value').toBe('value');
});
10 changes: 10 additions & 0 deletions e2e/literal-pattern/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
testMatch: ['**/literal_pattern.test.js'],
};
8 changes: 7 additions & 1 deletion packages/jest-circus/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
makeDescribe,
makeTest,
} from './utils';
import {escapeStrForRegex} from 'jest-regex-util';

const eventHandler: Circus.EventHandler = (event, state) => {
switch (event.name) {
Expand Down Expand Up @@ -250,7 +251,12 @@
state.parentProcess,
);
if (event.testNamePattern) {
state.testNamePattern = new RegExp(event.testNamePattern, 'i');
if (event.testNamePattern.startsWith('@:')) {
const raw = event.testNamePattern.slice(2);
state.testNamePattern = new RegExp(escapeStrForRegex(raw), 'i');
} else {
state.testNamePattern = new RegExp(event.testNamePattern, 'i');

Check warning on line 258 in packages/jest-circus/src/eventHandler.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-circus/src/eventHandler.ts#L255-L258

Added lines #L255 - L258 were not covered by tests
}
}
break;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-cli/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ export const options: {[key: string]: Options} = {
},
testNamePattern: {
alias: 't',
description: 'Run only tests with a name that matches the regex pattern.',
description:
'Run only tests with a name that matches the regex pattern. ' +
'Prefix with "@:" to match literal string instead of regex.',
requiresArg: true,
type: 'string',
},
Expand Down
Loading