You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is advanced API. If you are just running tests, you probably don't need this. It is primarily used by library authors.
5
+
:::
6
+
7
+
Vitest runs tests in pools. By default, there are several pools:
8
+
9
+
-`threads` to run tests using `node:worker_threads` (isolation is provided with a new worker context)
10
+
-`forks` to run tests using `node:child_process` (isolation is provided with a new `child_process.fork` process)
11
+
-`vmThreads` to run tests using `node:worker_threads` (but isolation is provided with `vm` module instead of a new worker context)
12
+
-`browser` to run tests using browser providers
13
+
-`typescript` to run typechecking on tests
14
+
15
+
You can provide your own pool by specifying a file path:
16
+
17
+
```ts
18
+
exportdefaultdefineConfig({
19
+
test: {
20
+
// will run every file with a custom pool by default
21
+
pool: './my-custom-pool.ts',
22
+
// you can provide options using `poolOptions` object
23
+
poolOptions: {
24
+
myCustomPool: {
25
+
customProperty: true,
26
+
},
27
+
},
28
+
// you can also specify pool for a subset of files
29
+
poolMatchGlobs: [
30
+
['**/*.custom.test.ts', './my-custom-pool.ts'],
31
+
],
32
+
},
33
+
})
34
+
```
35
+
36
+
## API
37
+
38
+
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:
The function is called only once (unless the server config was updated), and it's generally a good idea to initialize everything you need for tests inside that function and reuse it when `runTests` is called.
51
+
52
+
Vitest calls `runTest` when new tests are scheduled to run. It will not call it if `files` is empty. The first argument is an array of tuples: the first element is a reference to a workspace project and the second one is an absolute path to a test file. Files are sorted using [`sequencer`](/config/#sequence.sequencer) before `runTests` is called. It's possible (but unlikely) to have the same file twice, but it will always have a different project - this is implemented via [`vitest.workspace.ts`](/guide/workspace) configuration.
53
+
54
+
Vitest will wait until `runTests` is executed before finishing a run (i.e., it will emit [`onFinished`](/guide/reporters) only after `runTests` is resolved).
55
+
56
+
If you are using a custom pool, you will have to provide test files and their results yourself - you can reference [`vitest.state`](https://github.com/vitest-dev/vitest/blob/feat/custom-pool/packages/vitest/src/node/state.ts) for that (most important are `collectFiles` and `updateTasks`). Vitest uses `startTests` function from `@vitest/runner` package to do that.
57
+
58
+
To communicate between different processes, you can create methods object using `createMethodsRPC` from `vitest/node`, and use any form of communication that you prefer. For example, to use websockets with `birpc` you can write something like this:
// ... running tests, put into "files" and "tasks"
83
+
const methods =createMethodsRPC(project)
84
+
awaitmethods.onCollected(files)
85
+
// most reporters rely on results being updated in "onTaskUpdate"
86
+
awaitmethods.onTaskUpdate(tasks)
87
+
}
88
+
```
89
+
90
+
You can see a simple example in [pool/custom-pool.ts](https://github.com/vitest-dev/vitest/blob/feat/custom-pool/test/run/pool-custom-fixtures/pool/custom-pool.ts).
thrownewError('Since Vitest 0.31.0 "browser" pool is not supported in "poolMatchGlobs". You can create a workspace to run some of your tests in browser in parallel. Read more: https://vitest.dev/guide/workspace')
0 commit comments