Per-file opt-in isolation
Clear and concise description of the problem
--no-isolate is a great way to increase performance, drastically speeding things up in our large monorepo. However, we also have leaky files that mutate global state, which break other tests. It's inconvenient to fix all of these tests for us, and it would be very convenient if we could say "isolate just these test files". By far the biggest time sink for us with Vitest is the collect step, because basically every single file in the monorepo has to be re-imported for each test file.
It's not even really that we want those test files to be isolated themselves, we really just want the worker to be cleaned up after those test files, so that changes in that one file don't leak into other test files.
I think this would be a great middle ground for performance, where most tests that don't leak run quickly, and the few that need isolation can opt-in to it.
Suggested solution
Perhaps a comment like // @vitest-environment - // @vitest-isolate?
Alternative
I guess the config could have some sort of file matching regex as well, like environmentMatchGlobs (though that's deprecated I know)
Additional context
I know adding even more features and configuration isn't the best, but in the name of performance I think it would be worth it.
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
I think this was attempted in https://github.com/vitest-dev/vitest/pull/5791 but rejected eventually.
You should be able to do this by using different pools though:
import { defineConfig } from "vitest/config";
const nonIsolatedTests = [
"test/non-isolated-1.test.ts",
"test/non-isolated-2.test.ts",
];
export default defineConfig({
test: {
poolOptions: {
forks: { isolate: true },
threads: { isolate: false },
},
workspace: [
{
test: {
name: "Isolated",
pool: "forks",
include: ["test/*.test.ts"],
exclude: nonIsolatedTests,
},
},
{
test: {
name: "Non-isolated",
pool: "threads",
include: nonIsolatedTests,
},
},
],
},
});
Thanks that's useful. Not quite as convenient as a comment in the file in question, though, especially in a monorepo with a shared config.
I have a POC here, do you think that's a non-starter @AriPerkkio? Looks to be a similar approach to the closed PR.
I have a POC here
That still would not change isolation option of the pool. It's only affecting the worker's isolation option. The pool-level isolation is where the speed impact would be achieved.
Edit: Or wait - do you mean to disable isolation on global level and make few tests isolated by the comment?
Edit: Or wait - do you mean to disable isolation on global level and make few tests isolated by the comment?
Yeah exactly
This will be possible to implement after https://github.com/vitest-dev/vitest/pull/8705
Would that fix my previous issue which was closed while not being resolved? => https://github.com/vitest-dev/vitest/issues/6258
@paulintrognon after #8705 it's now possible to configure isolation per project.
- https://vitest.dev/guide/recipes.html#disabling-isolation-for-specific-test-files-only
I would also recommend to define unique sequence.groupOrder for the projects in that case.