{"id":1245,"date":"2026-07-08T06:11:27","date_gmt":"2026-07-07T23:11:27","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/08\/how-to-use-vitest-for-unit-testing-in-javascript-projects\/"},"modified":"2026-07-08T06:11:27","modified_gmt":"2026-07-07T23:11:27","slug":"how-to-use-vitest-for-unit-testing-in-javascript-projects","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/08\/how-to-use-vitest-for-unit-testing-in-javascript-projects\/","title":{"rendered":"How to Use Vitest for Unit Testing in JavaScript Projects"},"content":{"rendered":"<h1>How to Use Vitest for Unit Testing in JavaScript Projects<\/h1>\n<p>Vitest is a blazing-fast unit testing framework built on top of Vite. It offers native support for TypeScript, JSX, and modern JavaScript, making it an ideal choice for testing applications that already use Vite. This tutorial will walk you through setting up and writing your first tests with Vitest.<\/p>\n<p>To get started, install Vitest as a development dependency using your package manager. For example, with npm: <code>npm install -D vitest<\/code>. If you&#8217;re using Vite, Vitest will automatically read your existing Vite configuration. For non-Vite projects, you can still use Vitest \u2013 it works out of the box with minimal setup.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20use%20Vitest%20for%20unit%20testing\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Writing Your First Test<\/h2>\n<p>Create a file named <code>sum.test.js<\/code> (or <code>.ts<\/code> for TypeScript). Vitest uses a Jest-compatible API, so you can use <code>describe<\/code>, <code>it<\/code>, <code>expect<\/code> immediately. Here&#8217;s a simple example:<\/p>\n<pre><code>import { describe, it, expect } from 'vitest';\n\nfunction sum(a, b) {\n  return a + b;\n}\n\ndescribe('sum function', () => {\n  it('adds two numbers correctly', () => {\n    expect(sum(1, 2)).toBe(3);\n    expect(sum(-1, 1)).toBe(0);\n  });\n});\n<\/code><\/pre>\n<h2>Running Tests<\/h2>\n<p>Add a test script to your <code>package.json<\/code>:<\/p>\n<pre><code>\"scripts\": {\n  \"test\": \"vitest\"\n}\n<\/code><\/pre>\n<p>Then run <code>npm test<\/code> or <code>npx vitest<\/code>. Vitest will watch for file changes by default. To run once without watch mode, use <code>vitest run<\/code>.<\/p>\n<h3>Advanced Features<\/h3>\n<ul>\n<li><strong>Mocking<\/strong>: Use <code>vi.mock()<\/code> to mock modules or <code>vi.fn()<\/code> for functions.<\/li>\n<li><strong>Coverage<\/strong>: Enable code coverage with <code>npm install -D @vitest\/coverage-v8<\/code> and run <code>vitest --coverage<\/code>.<\/li>\n<li><strong>Configuration<\/strong>: Create a <code>vitest.config.ts<\/code> to customize test patterns, environment, or global setup.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Vitest provides a modern, fast, and developer-friendly experience for unit testing. Its seamless integration with Vite, familiar Jest API, and built-in TypeScript support make it a powerful choice for any JavaScript project. Start testing today and enjoy instant feedback with minimal configuration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Use Vitest for Unit Testing in JavaScript Projects Vitest is a blazing-fast unit testing framework built on top of Vite. It offers native support for TypeScript, JSX, and modern JavaScript, making it an ideal choice for testing applications that already use Vite. This tutorial will walk you through setting up and writing your &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-1245","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1245"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}