Overview
@stencil/vitest provides testing utilities for Stencil components, powered by Vitest: a render() function, matchers for custom elements, and spies for events, methods, props, and lifecycle hooks.
Paired with @stencil/unplugin, which compiles your components as each test imports them, there's no Stencil build to run before your tests. The same test code runs in:
- A real browser, through Vitest's browser mode. Components get real layout, styling, and events, so this is the recommended setup.
- Node, with a simulated DOM (Stencil's mock-doc, jsdom, or happy-dom). Tests start faster and need no browser installed.
Quick Start
The quickest way to set up is the wizard. Run it from your Stencil project:
stencil add @stencil/vitest
It asks whether your tests should run in a browser or in Node, and how components should be loaded, then installs the dependencies, writes vitest.config.ts, and adds test scripts to your package.json. Choose Plugin (source) when it asks how to load components, for the setup described on this page. The stencil init wizard offers the same steps when you create a project.
The rest of this section sets up browser tests by hand.