Back to articles
Vitest Has a Free API — Here's How to Test JavaScript at Blazing Speed

Vitest Has a Free API — Here's How to Test JavaScript at Blazing Speed

via Dev.to JavaScriptAlex Spinov

Vitest is a Vite-native testing framework that is compatible with Jest's API but runs 10-20x faster. It provides instant HMR, TypeScript support, and ESM-first testing. Installation npm install -D vitest Basic Tests // sum.test.ts import { describe , it , expect } from " vitest " ; import { sum , multiply } from " ./math " ; describe ( " math " , () => { it ( " adds numbers correctly " , () => { expect ( sum ( 1 , 2 )). toBe ( 3 ); expect ( sum ( - 1 , 1 )). toBe ( 0 ); }); it ( " multiplies numbers " , () => { expect ( multiply ( 3 , 4 )). toBe ( 12 ); }); }); Async Testing import { describe , it , expect } from " vitest " ; describe ( " API " , () => { it ( " fetches users " , async () => { const users = await fetchUsers (); expect ( users ). toHaveLength ( 10 ); expect ( users [ 0 ]). toHaveProperty ( " name " ); }); it ( " handles errors " , async () => { await expect ( fetchUser ( " invalid " )). rejects . toThrow ( " Not found " ); }); }); Mocking import { vi , describe , it , ex

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
2 views

Related Articles