Back to articles
Vitest Has a Free API — The Blazing Fast Unit Testing Framework for Vite

Vitest Has a Free API — The Blazing Fast Unit Testing Framework for Vite

via Dev.to TutorialAlex Spinov

Vitest is a unit testing framework powered by Vite. It's compatible with Jest's API but 10-20x faster, with native ESM, TypeScript, and JSX support out of the box. Why Vitest? Vite-powered — uses Vite's transform pipeline, instant HMR for tests Jest-compatible — same API, easy migration TypeScript native — no ts-jest or babel config In-source testing — write tests next to your code Quick Start npm install -D vitest // package.json { "scripts" : { "test" : "vitest" , "test:run" : "vitest run" } } Writing Tests // math.test.ts import { describe , it , expect , vi } from ' vitest ' ; describe ( ' math ' , () => { it ( ' adds numbers ' , () => { expect ( 1 + 2 ). toBe ( 3 ); }); it ( ' handles arrays ' , () => { expect ([ 1 , 2 , 3 ]). toHaveLength ( 3 ); expect ([ 1 , 2 , 3 ]). toContain ( 2 ); }); it ( ' handles objects ' , () => { expect ({ a : 1 , b : 2 }). toMatchObject ({ a : 1 }); expect ({ a : 1 }). toHaveProperty ( ' a ' , 1 ); }); }); Mocking import { vi } from ' vitest ' ; // Mo

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles