Skip to content
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
8 changes: 8 additions & 0 deletions docs/advanced/api/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
19 changes: 19 additions & 0 deletions test/cli/test/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
})
Loading