Loading
Current section: Performance 6 exercises
Problem

Isolation

Disable test isolation in Vitest to avoid spawning a new worker per file and reduce overhead when running hundreds or thousands of test files.

Loading exercise

Transcript

00:00 Let's talk about other ways to improve performance of our tests, switching our focus from test cases to test files. VTEST runs every test file in a separate worker. And by doing so, it affords to run them in parallel, which already speeds up your tests by default. But there's also another side effect of that. Every worker has its own environment.

00:18 So this way, VTEST provisions isolation on a test file level. So basically, if one test file mutates, let's say some global, then the other environment will not get this mutation out of the box, because, well, it's not going to be there. So that will be a regular fetch in this example. This is pretty great, but there's one interesting side effect to this.

00:38 If you happen to have a lot of test files, we're talking hundreds, maybe thousands, then the cost of spawning workers will accumulate and may result in performance degradation. Let me show you an example. So in this project, I've got a lot of test files, up to 100. And all of them feature simple test cases. They might even be concurrent.

00:55 But simply because there's 100 test files, running the test will be quite slow because we're paying the price of spawning the worker for every test file. So in this instance, it ends up up to 6.8 seconds, almost 7 second test run, which is extremely slow. This is where I need your help. Follow the instructions to disable test isolation

01:15 and see what kind of difference it makes in projects with a lot of test files.