diff --git a/docs/advanced/api/vitest.md b/docs/advanced/api/vitest.md index cb625e6143c1..90dc0eae2b38 100644 --- a/docs/advanced/api/vitest.md +++ b/docs/advanced/api/vitest.md @@ -375,6 +375,14 @@ This methods overrides the global [test name pattern](/config/#testnamepattern). This method doesn't start running any tests. To run tests with updated pattern, call [`runTestSpecifications`](#runtestspecifications). ::: +## getGlobalTestNamePattern + +```ts +function getGlobalTestNamePattern(): RegExp | undefined +``` + +Returns the regexp used for the global test name pattern. + ## resetGlobalTestNamePattern ```ts diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index eab2baa9fb85..0cdb1da303ae 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -1001,6 +1001,16 @@ export class Vitest { } } + /** + * Returns the regexp used for the global test name pattern. + */ + public getGlobalTestNamePattern(): RegExp | undefined { + if (this.configOverride.testNamePattern != null) { + return this.configOverride.testNamePattern + } + return this.config.testNamePattern + } + /** * Resets the global test name pattern. This method doesn't run any tests. */ diff --git a/test/cli/test/public-api.test.ts b/test/cli/test/public-api.test.ts index f7cb60ff4618..5f87fbcb1a9c 100644 --- a/test/cli/test/public-api.test.ts +++ b/test/cli/test/public-api.test.ts @@ -94,3 +94,22 @@ it.each([ }) }) }) + +it('can modify the global test name pattern', async () => { + const { ctx } = await runVitest({ + testNamePattern: 'custom', + include: ['non-existing'], + }) + + expect(ctx?.getGlobalTestNamePattern()).toEqual(/custom/) + + // reset just removes the override, user config is not touched + ctx?.resetGlobalTestNamePattern() + expect(ctx?.getGlobalTestNamePattern()).toEqual(/custom/) + + ctx?.setGlobalTestNamePattern(/new pattern/) + expect(ctx?.getGlobalTestNamePattern()).toEqual(/new pattern/) + + ctx?.resetGlobalTestNamePattern() + expect(ctx?.getGlobalTestNamePattern()).toEqual(/custom/) +})