From 358cffae29c9a685ff5b03f9e1cb529cc8cdd574 Mon Sep 17 00:00:00 2001 From: kweonsikyung Date: Wed, 11 Jun 2025 04:23:14 +0900 Subject: [PATCH 1/3] feat(jest-circus): support literal string matching via "@:" prefix in testNamePattern --- packages/jest-circus/src/eventHandler.ts | 8 +++++++- packages/jest-cli/src/args.ts | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/jest-circus/src/eventHandler.ts b/packages/jest-circus/src/eventHandler.ts index da66a6bde13e..8832b1741fff 100644 --- a/packages/jest-circus/src/eventHandler.ts +++ b/packages/jest-circus/src/eventHandler.ts @@ -19,6 +19,7 @@ import { makeDescribe, makeTest, } from './utils'; +import {escapeStrForRegex} from 'jest-regex-util'; const eventHandler: Circus.EventHandler = (event, state) => { switch (event.name) { @@ -250,7 +251,12 @@ const eventHandler: Circus.EventHandler = (event, state) => { 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'); + } } break; } diff --git a/packages/jest-cli/src/args.ts b/packages/jest-cli/src/args.ts index f6993afcd784..d6cd5c598ade 100644 --- a/packages/jest-cli/src/args.ts +++ b/packages/jest-cli/src/args.ts @@ -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', }, From 975daa3328b997b50cb733162471c84b638bcb84 Mon Sep 17 00:00:00 2001 From: kweonsikyung Date: Wed, 11 Jun 2025 04:23:46 +0900 Subject: [PATCH 2/3] test(e2e): add tests for literal string matching with "@:" prefix --- .../__tests__/literal_pattern.test.js | 14 ++++++++++++++ e2e/literal-pattern/jest.config.js | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 e2e/literal-pattern/__tests__/literal_pattern.test.js create mode 100644 e2e/literal-pattern/jest.config.js diff --git a/e2e/literal-pattern/__tests__/literal_pattern.test.js b/e2e/literal-pattern/__tests__/literal_pattern.test.js new file mode 100644 index 000000000000..59fcc4189f4a --- /dev/null +++ b/e2e/literal-pattern/__tests__/literal_pattern.test.js @@ -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'); +}); diff --git a/e2e/literal-pattern/jest.config.js b/e2e/literal-pattern/jest.config.js new file mode 100644 index 000000000000..b6ffeb383367 --- /dev/null +++ b/e2e/literal-pattern/jest.config.js @@ -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'], +}; From 3207280c35c7938462a62779a8b40e8903c25fd8 Mon Sep 17 00:00:00 2001 From: kweonsikyung Date: Wed, 11 Jun 2025 04:45:03 +0900 Subject: [PATCH 3/3] docs(changelog): add entry for testNamePattern literal match via "@:" prefix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4226465eb81..37cc5666ce5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,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)) - `[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)) - `[expect]` Revert [#15038](https://github.com/jestjs/jest/pull/15038) to fix `expect(fn).toHaveBeenCalledWith(expect.objectContaining(...))` when there are multiple calls ([#15508](https://github.com/jestjs/jest/pull/15508))