feat: allow custom pools#4417
Conversation
|
|
✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.
|
|
Note: this PR just opens up a possibility to do this, we still need to improve how to run tests in the new runtime - like exposing the logic of |
AriPerkkio
left a comment
There was a problem hiding this comment.
Tested a custom pool in locally linked project and seems to work nice.
docs/advanced/pool.md
Outdated
|
|
||
| ## API | ||
|
|
||
| The file specified in `pool` option should export a function (can be async) that accepts `Vitest` interface as its first option. This functions needs to return an object matching `ProcessPool` interface: |
There was a problem hiding this comment.
| The file specified in `pool` option should export a function (can be async) that accepts `Vitest` interface as its first option. This functions needs to return an object matching `ProcessPool` interface: | |
| 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/advanced/pool.md
Outdated
|
|
||
| Vitest will wait until `runTests` is executed before finishing a run (i.e., it will emit [`onFinished`](/guide/reporters) only after `runTests` is resolved). | ||
|
|
||
| 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 (mose important are `collectFiles` and `updateTasks`). Vitest uses `startTests` function from `@vitest/runner` package to do that. |
There was a problem hiding this comment.
| 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 (mose important are `collectFiles` and `updateTasks`). Vitest uses `startTests` function from `@vitest/runner` package to do that. | |
| 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. |
And the URL should eventually point at main instead of work branch.
| return sequencer.sort(specs) | ||
| } | ||
|
|
||
| await Promise.all(Object.entries(filesByPool).map(async (entry) => { |
There was a problem hiding this comment.
This issue seems to be in main branch too, but all pools are executed parallel here, right?
If user has threads pool and vmThreads pools set, and each are using default max-threads, the execution will spawn too many workers at the same time.
There was a problem hiding this comment.
Yes, this is how it works. That's why I wanted agnostic implementation of tinypool.
There was a problem hiding this comment.
Tinypool can run worker_threads and child_process parallel.
It's possible to push all threads tasks in the pool, then wait for queue to be empty (remaining tasks are running, nothing in queue), and then change the runtime and push more tasks in the queue to wait for previous ones to finish. Like here is done:
vitest/packages/vitest/src/node/pools/threads.ts
Lines 171 to 174 in ab55637
There was a problem hiding this comment.
Ideally, I would like an API that we can expose to reuse the same Tinypool instance in different pools:
function createThreadsPool(ctx, { pool }) {
function runTests(testFile) {
const context = { config: ctx.config, files: [testFile], executeFile: resolve('./dist/worker.js') }
await pool.run(context, { runtime: 'worker_threads', name: 'run' })
}
}Maybe instead of having separate worker.js/child.js/vmWorker.js files we have a single file where inside of a context we pass down information on how we want to run it? So, we don't need to change filename option in Tinypool. This should also make it easier for external pools to reuse it
There was a problem hiding this comment.
I like the idea. From Tinypool's side I think that kind of API should already be possible - some kind of wrappers around it might be needed though.
There's also some need for refactoring in threads and forks pools to reduce code duplication. While doing that it might be good to look into this kind of API. But all this can be left out of this PR as it's not relevant to these changes.
fe67eee to
fba232a
Compare
Description
This PR adds a possibility to specify a filepath or package name in
pooloption. Vitest expects that specified module will have a default export which is a function that accepts Vitest as its parameter. This function should return on object that satisfies this interface:runTestsis called when user runsvitestcommand and then each time watch mode is triggered. This API is primarily designed to be used by library authors because it requires deep knowledge of how Vitest works internally. More about this option can be found in documentation.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.