diff --git a/CHANGELOG.md b/CHANGELOG.md index 51e9ae404ccb..b1e2f006b643 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) 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'], +}; 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 a1cdd59516bf..03d1caded4ce 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', },