Skip to content

Commit 98c7e51

Browse files
authored
refactor: deprecate environmentMatchGlobs and poolMatchGlobs in favor of workspace (#6922)
1 parent fe2a187 commit 98c7e51

File tree

8 files changed

+112
-14
lines changed

8 files changed

+112
-14
lines changed

docs/advanced/pool.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Vitest runs tests in pools. By default, there are several pools:
1414

1515
You can provide your own pool by specifying a file path:
1616

17-
```ts
17+
```ts [vitest.config.ts]
1818
import { defineConfig } from 'vitest/config'
1919

2020
export default defineConfig({
@@ -27,14 +27,31 @@ export default defineConfig({
2727
customProperty: true,
2828
},
2929
},
30-
// you can also specify pool for a subset of files
31-
poolMatchGlobs: [
32-
['**/*.custom.test.ts', './my-custom-pool.ts'],
30+
},
31+
})
32+
```
33+
34+
If you need to run tests in different pools, use the [workspace](/guide/workspace) feature:
35+
36+
```ts [vitest.config.ts]
37+
export default defineConfig({
38+
test: {
39+
workspace: [
40+
{
41+
extends: true,
42+
test: {
43+
pool: 'threads',
44+
},
45+
},
3346
],
3447
},
3548
})
3649
```
3750

51+
::: info
52+
The `workspace` field was introduced in Vitest 2.2. To define a workspace in Vitest <2.2, create a separate `vitest.workspace.ts` file.
53+
:::
54+
3855
## API
3956

4057
The file specified in `pool` option should export a function (can be async) that accepts `Vitest` interface as its first option. This function needs to return an object matching `ProcessPool` interface:

docs/config/index.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,28 @@ These options are passed down to `setup` method of current [`environment`](#envi
581581
- **Type:** `[string, EnvironmentName][]`
582582
- **Default:** `[]`
583583

584+
::: warning
585+
This API was deprecated in Vitest 2.2. Use [workspace](/guide/workspace) to define different configurations instead.
586+
587+
```ts
588+
export default defineConfig({
589+
test: {
590+
environmentMatchGlobs: [ // [!code --]
591+
['./*.jsdom.test.ts', 'jsdom'], // [!code --]
592+
], // [!code --]
593+
workspace: [ // [!code ++]
594+
{ // [!code ++]
595+
extends: true, // [!code ++]
596+
test: { // [!code ++]
597+
environment: 'jsdom', // [!code ++]
598+
}, // [!code ++]
599+
}, // [!code ++]
600+
], // [!code ++]
601+
},
602+
})
603+
```
604+
:::
605+
584606
Automatically assign environment based on globs. The first match will be used.
585607

586608
For example:
@@ -606,6 +628,28 @@ export default defineConfig({
606628
- **Type:** `[string, 'threads' | 'forks' | 'vmThreads' | 'vmForks' | 'typescript'][]`
607629
- **Default:** `[]`
608630

631+
::: warning
632+
This API was deprecated in Vitest 2.2. Use [workspace](/guide/workspace) to define different configurations instead:
633+
634+
```ts
635+
export default defineConfig({
636+
test: {
637+
poolMatchGlobs: [ // [!code --]
638+
['./*.threads.test.ts', 'threads'], // [!code --]
639+
], // [!code --]
640+
workspace: [ // [!code ++]
641+
{ // [!code ++]
642+
test: { // [!code ++]
643+
extends: true, // [!code ++]
644+
pool: 'threads', // [!code ++]
645+
}, // [!code ++]
646+
}, // [!code ++]
647+
], // [!code ++]
648+
},
649+
})
650+
```
651+
:::
652+
609653
Automatically assign pool in which tests will run based on globs. The first match will be used.
610654

611655
For example:
@@ -1713,7 +1757,7 @@ This is an experimental feature. Breaking changes might not follow SemVer, pleas
17131757
- **Default:** `false`
17141758
- **CLI:** `--browser`, `--browser.enabled=false`
17151759

1716-
Run all tests inside a browser by default. Can be overridden with [`poolMatchGlobs`](#poolmatchglobs) option.
1760+
Run all tests inside a browser by default.
17171761

17181762
#### browser&#46;name
17191763

docs/guide/improving-performance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ By default Vitest runs every test file in an isolated environment based on the [
88
- `forks` pool runs every test file in a separate [forked child process](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
99
- `vmThreads` pool runs every test file in a separate [VM context](https://nodejs.org/api/vm.html#vmcreatecontextcontextobject-options), but it uses workers for parallelism
1010

11-
This greatly increases test times, which might not be desirable for projects that don't rely on side effects and properly cleanup their state (which is usually true for projects with `node` environment). In this case disabling isolation will improve the speed of your tests. To do that, you can provide `--no-isolate` flag to the CLI or set [`test.isolate`](/config/#isolate) property in the config to `false`. If you are using several pools at once with `poolMatchGlobs`, you can also disable isolation for a specific pool you are using.
11+
This greatly increases test times, which might not be desirable for projects that don't rely on side effects and properly cleanup their state (which is usually true for projects with `node` environment). In this case disabling isolation will improve the speed of your tests. To do that, you can provide `--no-isolate` flag to the CLI or set [`test.isolate`](/config/#isolate) property in the config to `false`.
1212

1313
::: code-group
1414
```bash [CLI]

packages/vitest/src/node/config/resolveConfig.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ export function resolveConfig(
532532
if (!builtinPools.includes(resolved.pool as BuiltinPool)) {
533533
resolved.pool = resolvePath(resolved.pool, resolved.root)
534534
}
535+
if (resolved.poolMatchGlobs) {
536+
logger.warn(
537+
c.yellow(
538+
`${c.inverse(
539+
c.yellow(' Vitest '),
540+
)} "poolMatchGlobs" is deprecated. Use "workspace" to define different configurations instead.`,
541+
),
542+
)
543+
}
535544
resolved.poolMatchGlobs = (resolved.poolMatchGlobs || []).map(
536545
([glob, pool]) => {
537546
if (!builtinPools.includes(pool as BuiltinPool)) {
@@ -725,6 +734,15 @@ export function resolveConfig(
725734
...resolved.typecheck,
726735
}
727736

737+
if (resolved.environmentMatchGlobs) {
738+
logger.warn(
739+
c.yellow(
740+
`${c.inverse(
741+
c.yellow(' Vitest '),
742+
)} "environmentMatchGlobs" is deprecated. Use "workspace" to define different configurations instead.`,
743+
),
744+
)
745+
}
728746
resolved.environmentMatchGlobs = (resolved.environmentMatchGlobs || []).map(
729747
i => [resolve(resolved.root, i[0]), i[1]],
730748
)

packages/vitest/src/node/types/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ export interface InlineConfig {
314314
*
315315
* Format: [glob, environment-name]
316316
*
317+
* @deprecated use [`workspace`](https://vitest.dev/config/#environmentmatchglobs) instead
317318
* @default []
318319
* @example [
319320
* // all tests in tests/dom will run in jsdom
@@ -370,6 +371,7 @@ export interface InlineConfig {
370371
*
371372
* Format: [glob, pool-name]
372373
*
374+
* @deprecated use [`workspace`](https://vitest.dev/config/#poolmatchglobs) instead
373375
* @default []
374376
* @example [
375377
* // all tests in "forks" directory will run using "poolOptions.forks" API

test/cli/fixtures/custom-pool/vitest.config.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@ import { defineConfig } from 'vitest/config'
22

33
export default defineConfig({
44
test: {
5-
name: 'custom-pool-test',
6-
pool: './pool/custom-pool.ts',
75
poolOptions: {
86
custom: {
97
print: 'options are respected',
108
array: [1, 2, 3],
119
},
1210
},
13-
poolMatchGlobs: [
14-
['**/*.threads.spec.ts', 'threads'],
15-
],
11+
workspace: [
12+
{
13+
extends: true,
14+
test: {
15+
name: 'custom-pool-test',
16+
pool: './pool/custom-pool.ts',
17+
exclude: ['**/*.threads.spec.ts'],
18+
},
19+
},
20+
{
21+
extends: true,
22+
test: {
23+
name: 'threads-pool-test',
24+
include: ['**/*.threads.spec.ts'],
25+
pool: 'threads',
26+
},
27+
},
28+
]
1629
},
1730
})

test/cli/test/custom-pool.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('can run custom pools with Vitest', async () => {
1616
`)
1717

1818
expect(vitest.stdout).toContain('✓ |custom-pool-test| tests/custom-not-run.spec.ts')
19-
expect(vitest.stdout).toContain('✓ |custom-pool-test| tests/custom-run.threads.spec.ts')
19+
expect(vitest.stdout).toContain('✓ |threads-pool-test| tests/custom-run.threads.spec.ts')
2020
expect(vitest.stdout).toContain('Test Files 2 passed')
2121
expect(vitest.stdout).toContain('Tests 2 passed')
2222
})

test/config/test/pool-isolation.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe.each(['forks', 'threads'] as const)('%s', async (pool) => {
3131
env: { TESTED_POOL: pool },
3232
})
3333

34-
expect(stderr).toBe('')
34+
expect(stderr).toBe(deprecatedPoolMatchGlob())
3535
expect(exitCode).toBe(0)
3636
})
3737

@@ -62,11 +62,15 @@ describe.each(['forks', 'threads'] as const)('%s', async (pool) => {
6262
env: { TESTED_POOL: pool },
6363
})
6464

65-
expect(stderr).toBe('')
65+
expect(stderr).toBe(deprecatedPoolMatchGlob())
6666
expect(exitCode).toBe(0)
6767
})
6868
})
6969

7070
function invertPool(pool: 'threads' | 'forks') {
7171
return pool === 'threads' ? 'forks' : 'threads'
7272
}
73+
74+
function deprecatedPoolMatchGlob() {
75+
return ' Vitest "poolMatchGlobs" is deprecated. Use "workspace" to define different configurations instead.\n'
76+
}

0 commit comments

Comments
 (0)