Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readTesting

Run one Vitest test by file line

Target a single Vitest case from its editor location by passing the full test file path and declaration line.

You are looking at one failing test in a file with dozens of cases. Its name appears in other suites, but your editor gives you the file and line. Pass that location to Vitest to run the test under your cursor.

Use the full test filename

Vitest's line filter is available in Vitest 3 and later. Pass the full filename, including the extension, followed by a colon and a line number. Use a path relative to the project root or an absolute path. The short substring filter that works for ordinary filename searches does not work with a line suffix.

In a project with Vitest installed, save this as label.test.ts. Keep the blank line so the second test starts on line 7.

import { expect, test } from 'vitest'

test('formats a trail label', () => {
  expect('trail'.toUpperCase()).toBe('TRAIL')
})

test('keeps an empty label empty', () => {
  expect(''.toUpperCase()).toBe('')
})

The file gives you two tests with different declaration lines, so you can see which one Vitest picks.

pnpm exec vitest run ./label.test.ts:7

The run reports one passing test and one skipped test.

Copy the declaration line

Use the line with test( when you copy from the editor. If edits moved the declaration after you copied a line from a failure, take the new number from the source. For multiple individual tests, give the CLI separate location arguments. A range such as ./label.test.ts:3-7 is unsupported.

run exits after the selected case runs. If you keep moving the declaration while editing, use -t with the test name and pass the filename without a line suffix.

Remove the line suffix after the fix to run the full file.