Run A Specific Test in Vitest

Learn how to run a specific test by providing its file and line number.

VitestTesting

This tutorial will show you how to run one test function at a time in your Vitest test suite. This is a lifesaver for fast iteration in long-running test suites.

One Test at a Time

Suppose I have 30 integration tests that each take an average of 1 second. I’m only interested in the result of one of them. If that function starts on line 232, I can run it alone with:

bash
npx vitest run path/to/test-suite.ts:232

In general, you can do this for any test on any line number with:

bash
npx vitest run path/to/test-suite.ts:<test-line-number>

Vitest’s Documentation

You can read about this method in the Vitest docs:

Vitest

Final Words

This saves me a lot of wasted time whilst testing, and keeps me in that fast development flow. Hopefully it does the same for you!