-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add --no-isolate flag to improve performance, add documentation…
… about performance (#4777) Co-authored-by: Ari Perkkiö <[email protected]> Co-authored-by: Pascal Jufer <[email protected]>
- Loading branch information
1 parent
f43fdd8
commit 4d55a02
Showing
11 changed files
with
195 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Performance | ||
|
||
By default Vitest runs every test file in an isolated environment based on the [pool](/config/#pool): | ||
|
||
- `threads` pool runs every test file in a separate [`Worker`](https://nodejs.org/api/worker_threads.html#class-worker) | ||
- `forks` pool runs every test file in a separate [forked child process](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options) | ||
- `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 | ||
|
||
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. | ||
|
||
::: code-group | ||
```bash [CLI] | ||
vitest --no-isolate | ||
``` | ||
```ts [vitest.config.js] | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
isolate: false, | ||
// you can also disable isolation only for specific pools | ||
poolOptions: { | ||
forks: { | ||
isolate: false, | ||
}, | ||
}, | ||
}, | ||
}) | ||
``` | ||
::: | ||
|
||
::: note | ||
If you are using `vmThreads` pool, you cannot disable isolation. Use `threads` pool instead to improve your tests performance. | ||
::: | ||
|
||
For some projects, it might also be desirable to disable parallelism to improve startup time. To do that, provide `--no-file-parallelism` flag to the CLI or set [`test.fileParallelism`](/config/#fileParallelism) property in the config to `false`. | ||
|
||
::: code-group | ||
```bash [CLI] | ||
vitest --no-file-parallelism | ||
``` | ||
```ts [vitest.config.js] | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
fileParallelism: false, | ||
}, | ||
}) | ||
``` | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { UserConfig } from 'vitest' | ||
import { describe, expect, it } from 'vitest' | ||
import { createVitest } from 'vitest/node' | ||
|
||
async function config(cliOptions: UserConfig, configValue: UserConfig = {}) { | ||
const vitest = await createVitest('test', { ...cliOptions, watch: false }, { test: configValue }) | ||
return vitest.config | ||
} | ||
|
||
describe('correctly defines isolated flags', async () => { | ||
it('prefers CLI poolOptions flags over config', async () => { | ||
const c = await config({ | ||
isolate: true, | ||
poolOptions: { | ||
threads: { | ||
isolate: false, | ||
}, | ||
forks: { | ||
isolate: false, | ||
}, | ||
}, | ||
}) | ||
expect(c.poolOptions?.threads?.isolate).toBe(false) | ||
expect(c.poolOptions?.forks?.isolate).toBe(false) | ||
expect(c.isolate).toBe(true) | ||
}) | ||
|
||
it('override CLI poolOptions flags over isolate', async () => { | ||
const c = await config({ | ||
isolate: false, | ||
poolOptions: { | ||
threads: { | ||
isolate: true, | ||
}, | ||
forks: { | ||
isolate: true, | ||
}, | ||
}, | ||
}, { | ||
poolOptions: { | ||
threads: { | ||
isolate: false, | ||
}, | ||
forks: { | ||
isolate: false, | ||
}, | ||
}, | ||
}) | ||
expect(c.poolOptions?.threads?.isolate).toBe(true) | ||
expect(c.poolOptions?.forks?.isolate).toBe(true) | ||
expect(c.isolate).toBe(false) | ||
}) | ||
|
||
it('override CLI isolate flag if poolOptions is not set via CLI', async () => { | ||
const c = await config({ | ||
isolate: true, | ||
}, { | ||
poolOptions: { | ||
threads: { | ||
isolate: false, | ||
}, | ||
forks: { | ||
isolate: false, | ||
}, | ||
}, | ||
}) | ||
expect(c.poolOptions?.threads?.isolate).toBe(true) | ||
expect(c.poolOptions?.forks?.isolate).toBe(true) | ||
expect(c.isolate).toBe(true) | ||
}) | ||
|
||
it('keeps user configured poolOptions if no CLI flag is provided', async () => { | ||
const c = await config({}, { | ||
poolOptions: { | ||
threads: { | ||
isolate: false, | ||
}, | ||
forks: { | ||
isolate: false, | ||
}, | ||
}, | ||
}) | ||
expect(c.poolOptions?.threads?.isolate).toBe(false) | ||
expect(c.poolOptions?.forks?.isolate).toBe(false) | ||
expect(c.isolate).toBe(true) | ||
}) | ||
|
||
it('isolate config value overrides poolOptions defaults', async () => { | ||
const c = await config({}, { | ||
isolate: false, | ||
}) | ||
expect(c.poolOptions?.threads?.isolate).toBe(false) | ||
expect(c.poolOptions?.forks?.isolate).toBe(false) | ||
expect(c.isolate).toBe(false) | ||
}) | ||
|
||
it('if no isolation is defined in the config, fallback ot undefined', async () => { | ||
const c = await config({}, {}) | ||
expect(c.poolOptions?.threads?.isolate).toBe(undefined) | ||
expect(c.poolOptions?.forks?.isolate).toBe(undefined) | ||
// set in configDefaults, so it's always defined | ||
expect(c.isolate).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters