vitest-dev / vscode

VS Code extension for Vitest
https://vitest.dev/vscode
MIT License
764 stars 86 forks source link
testing vitest vscode

Vitest extension for Visual Studio Code logo

Vitest extension for Visual Studio Code. Available on Visual Studio Marketplace.

Features

Requirements

Usage

You can manage tests both from the Testing view and directly within your test files.

Vitest uses vscode's TestController API to provide a unified testing experience. You can read the official guide on how to run tests in the VSCode Documentation.

In the Testing View

Testing view

You can access the extension from the Testing view in the Visual Studio Code sidebar.

The toolbar at the top provides various commands to manage test execution:

💡 Hovering, or right clicking a folder, test file, test suite, or a test will reveal more actions.

The filter bar allows you to narrow down the tests displayed, focusing on specific tests by name, exclusion patterns, or tags.

Icons next to each test indicate their status—passed (checkmark), failed (cross), skipped (arrow), queued (yellow icon), or not executed (dot).

In the Test File

Vitest test file

When viewing a test file, you'll notice test icons in the gutter next to each test case:

Configuration

You can identify if your config is loaded by the extension with process.env.VITEST_VSCODE and change the configuration accordingly.

Workspace Configurations

These options are resolved relative to the workspace file if there is one. If you have a single folder open in Visual Studio Code, paths will be resolved relative to that folder. If there are multiple folders, but there is no workspace file, then paths are resolved as is (so they should be absolute) - this can happen if you change your user config to have multiple folders.

💡 The vitest.nodeExecutable and vitest.nodeExecArgs settings are used as execPath and execArgv when spawning a new child_process, and as runtimeExecutable and runtimeArgs when debugging a test. The vitest.terminalShellPath and vitest.terminalShellArgs settings are used as shellPath and shellArgs when creating a new terminal

Other Options

Commands

You can reveal the current test file in the test explorer view by selecting the "Reveal in Test Explorer" option (the last option on the screenshot) in the file context:

Reveal test in explorer

You can also type the same command in the quick picker while the file is open.

Reveal test in explorer

Experimental

If the extension hangs, consider enabling vitest.experimentalStaticAstCollect option to use static analysis instead of actually running the test file every time you make a change which can cause visible hangs if it takes a long time to setup the test.

FAQs (Frequently Asked Questions)

How can I use it in monorepo?

See https://vitest.dev/guide/workspace.html for monorepo support.

How to hide Test Results view when running tests

You can change the behaviour of testing view by modifying testing.openTesting option:

This is a vscode's built-in option and will control every plugin.

I am using vitest.shellType: terminal, but I don't see the terminal

The extension uses a modified Vitest script that removes the reporter output. For this reason, the terminal is hidden by default. However, it might be useful to debug issues with the extension or Vitest itself - to open the terminal in the "Terminals" view you can use the "Vitest: Show Shell Terminal" command.