Vitest

September 3, 2024

What is Vitest?

Vistest is a fast native testing framework for JavaScript and Typescript.  Vitest is generally used for unit testing, meaning it tests parts of a codebase in isolation, rather than integration testing where the parts are combined and tested to see how they work together (though Vitest can be used for both). Vitest makes it easy to write and manage standardized Javascript tests for your codebase. Vitest works hand in hand with your CI (Continuous Integration) system to ensure tests are reliable and repeatable across team members and environments.

Enabling XML Output

Configure Vitest to produce JUnit XML output using the built in junit reporter command line option.

1vitest --reporter=junit --outputFile=test-output.xml

Typically you will do this in the package.json script that runs your tests, like this:

1"scripts": {
2 "vitest": "vitest run --dir test --reporter=junit --outputFile=./test-output.xml",
3}

For more details see the Vitest reporter docs.

Test Suite naming

The generated XML contains nested testsuites and testcase tags. You can use the environment variables VITEST_JUNIT_SUITE_NAME and VITEST_JUNIT_CLASSNAME to configure their name and classname attributes, respectively. These can also be customized via reporter options:

1export default defineConfig({
2 test: {
3 reporters: [
4 ['junit', { suiteName: 'custom suite name', classname: 'custom-classname' }]
5 ]
6 },
7})

For more details see the Vitest JUnit Reporter docs.

Handling Retries

The Vitest command line can automatically retry failed tests using the retry command line option, or the retry option in the Vitest config.

1vitest --retry <times>

For more details see the Vitest retry doc.

If you need to retry a lot of tests that is a strong indicator that you may have flaky tests. These are tests that sometimes pass and sometimes fail, slowing down your development process. You can get early access to Trunk’s new Flaky Tests tool to manage these types of tests better.

Try it yourself or
request a demo

Get started for free

Try it yourself or
Request a Demo

Free for first 5 users