iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💭

Does Switching to Vitest Automatically Speed Up Your Tests?

に公開
2025/05/11

I will provide a rough explanation of how testing libraries work and then consider whether simply using Vitest makes everything faster.

Rough Overview of Testing Library Mechanics

The flow of test execution is divided as follows:

  1. glob: Retrieving test files
  2. setup: (From the end of glob to before the run)
    • Dependency resolution
    • Test file transpilation
    • Parsing of describe, it, chains, and hooks[1]
    • Process or thread forking (in the case of parallel runs)
  3. assertion: Executing assertions

Since it might be a bit difficult to understand through text alone, I have prepared a very simplified testing library. Please try cloning it and playing around with it. Also, Christoph Nakazawa's Building a JavaScript Testing Framework is also very helpful.

Actually, let's look at some code. As a simple test case, suppose we test the following:

// __test__/index.test.js
it("sample test", () => {
  expect(true).toBe(true);
});

The functions to be provided by the testing library are:

  • it
  • expect

toBe simply returns true if received and expected match, and throws an Error otherwise.
Also, it is a function that takes a title as its first argument and a function as its second argument. It pushes these into a suitable array.

// index.js
const tests = []

// Defining matchers
const expect = (received) => ({
  toBe: (expected) => {
    if (received !== expected) {
      throw new Error(`Expected ${expected} but received ${received}.`);
    }
    return true;
  },
});

// Defining the test function (tests is a suitable array)
const it = (title, fn) => tests.push([title, fn]);

Next is retrieving the files. Normally, we would get an array of test file paths using a library capable of proper glob matching.

// 1. glob (Normally, use a glob library)
const testFilePath = ["__test__/index.test.js"];

We iterate through this retrieved testFilePath using for and evaluate them.

for (file of testFilePath) {
  const code = fs.readFileSync(`${root}/${file}`, "utf-8");
  eval(code);

What's happening is equivalent to the following code. Note that it and expect were declared earlier.

// index.js
for (file of testFilePath) {
  const code = fs.readFileSync(`${root}/${file}`, "utf-8");

  // ↓ The eval part. The content of the argument `code` is executed as is.
  it("sample test", () => {
    expect(true).toBe(true);
  });

.....

Then the function it is executed, and the tests are pushed into the tests array.

Finally, we perform the assertions. We iterate through tests using for, extracting title and fn.
fn corresponds to the assertion expect(true).toBe(true) in the previous test file.

We execute this inside a try block. If an error occurs within the expect function, it goes to the catch block.

try {
      // 3. Execute assertion (in this case, `expect(true).toBe(true)`)
      fn();
      result.success = true;
    } catch (e) {
      // If an error occurs in fn(), go to the catch block
      result.error = e;
    }

This is the general flow of a testing library's execution.
By understanding how testing libraries work, I think you can see that:

  • Most of it involves evaluating and executing the JS code itself.
    • Making this part faster is almost synonymous with increasing the execution speed of Node.js itself, so it's not very realistic.
    • For acceleration, the only way is to perform parallel execution efficiently.
  • Even if you use common tools like swc or esbuild, you can only speed up the transpilation part of the setup phase.

There are few areas where you can manually intervene from outside the library for performance improvements.

Can Vitest Make Everything Faster?

Personally, I think it depends on the case. Let's start with the theoretical side.
The idea behind vitest is simple and clear: use vite as a resolver.
The core is a package called vite-node. As the README says, it is "Vite as a Node runtime," and instead of a browser, the Runner acts as the client.

In vitest, the execution of JS/TS files follows this flow:

  • Transform various things with Vite -> Execute the test file within a vm context.

Let's review the test execution flow again:

  1. glob: Retrieving test files
  2. setup: (From the end of glob to before the run)
    • Dependency resolution
    • Test file transpilation
    • Parsing of describe, it, chains, and hooks
    • Process or thread forking
  3. assertion: Executing assertions

The parts that could potentially be sped up by using vite are:

  • Dependency resolution
  • Test file transpilation

Using vite doesn't mean the execution speed of JS itself becomes phenomenally faster. Since it can't speed up the assertion or the parsing of test files, it's these two parts.

Regarding transpilation, the same could be achieved in jest by changing the transformer.
Therefore, we can expect a reduction in execution time due to the acceleration of dependency resolution.

Conversely, I believe it's quite possible that for tests where dependency resolution isn't much of a bottleneck, it won't be much different from running with jest!

Currently, I don't think there's as big of a shock as "build time became dozens of times faster" like switching from Babel to esbuild/swc. Personally, I feel like it's about being able to create the fastest configuration possible in jest with no config + not having to worry about typescript or esm.
Also, it goes without saying that incremental execution in watch mode is explosively fast compared to others.

Current performance challenges Vitest is facing

Moving on to a completely different topic, I would also like to mention some issues that are currently being identified.

Worker behavior

It seems there are cases where it is not very stable (though of course, there are cases where it doesn't have much of an impact).
vitest has isolate set to true by default. This isolate prevents environment pollution by re-creating a worker every time it runs in parallel. (The implementation is not in Vitest but in a library called tinypool).

However, cases have been confirmed where this causes execution to become extremely slow, and it gets faster if you turn off --isolate. (jest vs jasmine) There is discussion that it might be creating and destroying workers too frequently.
To work around this, add:

--isolate=false

at runtime.

On a separate note, analysis with a profiler has shown that there are periods where some workers are idly in an idle state for no reason, and there are cases where execution is not very efficient.

v0.6.0 ?

The cause for this is unknown, but a phenomenon has been occurring since v0.6.0 where it mysteriously becomes slightly slower. While articles trying out vitest appear from time to time, if they are using versions before 0.6.0, the results might change if run again with the latest version.

Final words

Although it hasn't even reached a major version yet, this was a discussion about how there might be cases where using Vitest doesn't necessarily make things faster.
I'm not yet very familiar with the internal implementation (especially Vite) myself, so I intend to keep a close eye on it.

Also, Vitest is truly stress-free regarding ESM and TypeScript, so please give it a try.

脚注
  1. The hooks here refer to beforeEach and afterAll. Chains refer to it.skip and it.only. ↩︎

Discussion