Skip to content

Commit bdb7067

Browse files
authored
feat(api): add getGlobalTestNamePattern method (#8438)
1 parent 21622b5 commit bdb7067

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

docs/advanced/api/vitest.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,14 @@ This methods overrides the global [test name pattern](/config/#testnamepattern).
375375
This method doesn't start running any tests. To run tests with updated pattern, call [`runTestSpecifications`](#runtestspecifications).
376376
:::
377377

378+
## getGlobalTestNamePattern
379+
380+
```ts
381+
function getGlobalTestNamePattern(): RegExp | undefined
382+
```
383+
384+
Returns the regexp used for the global test name pattern.
385+
378386
## resetGlobalTestNamePattern
379387

380388
```ts

packages/vitest/src/node/core.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,16 @@ export class Vitest {
10011001
}
10021002
}
10031003

1004+
/**
1005+
* Returns the regexp used for the global test name pattern.
1006+
*/
1007+
public getGlobalTestNamePattern(): RegExp | undefined {
1008+
if (this.configOverride.testNamePattern != null) {
1009+
return this.configOverride.testNamePattern
1010+
}
1011+
return this.config.testNamePattern
1012+
}
1013+
10041014
/**
10051015
* Resets the global test name pattern. This method doesn't run any tests.
10061016
*/

test/cli/test/public-api.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,22 @@ it.each([
9494
})
9595
})
9696
})
97+
98+
it('can modify the global test name pattern', async () => {
99+
const { ctx } = await runVitest({
100+
testNamePattern: 'custom',
101+
include: ['non-existing'],
102+
})
103+
104+
expect(ctx?.getGlobalTestNamePattern()).toEqual(/custom/)
105+
106+
// reset just removes the override, user config is not touched
107+
ctx?.resetGlobalTestNamePattern()
108+
expect(ctx?.getGlobalTestNamePattern()).toEqual(/custom/)
109+
110+
ctx?.setGlobalTestNamePattern(/new pattern/)
111+
expect(ctx?.getGlobalTestNamePattern()).toEqual(/new pattern/)
112+
113+
ctx?.resetGlobalTestNamePattern()
114+
expect(ctx?.getGlobalTestNamePattern()).toEqual(/custom/)
115+
})

0 commit comments

Comments
 (0)