JavaScript Testing

Tests let you change your code without fear. These tutorials introduce the tools and techniques for testing JavaScript, from writing your first unit test to organizing a full test suite. You’ll write assertions with test runners like Vitest, Jest, and the built-in node:test module, test asynchronous code, and use mocks to isolate the code you’re testing.
Free Bonus: JavaScript Cheat Sheet
Variables, arrays, objects, functions, and async basics in one printable reference.
Get the Free Cheat Sheet »As projects grow, testing covers more than single functions. Learn to test UI components, run end-to-end browser tests with tools like Playwright, measure code coverage, and run your tests automatically on every change with continuous integration. Good tests also document how your code is supposed to behave.
Tests catch bugs before your users do and prevent old bugs from coming back when you change code. They make refactoring safer and serve as living documentation of how each part of your code should behave.
Vitest and Jest are the most popular choices and share a similar API. Vitest is fast and fits projects built with Vite, while Jest is widely used in existing codebases. Node.js also ships a built-in test runner in node:test for projects that want no extra dependencies.
Install a test runner such as Vitest, create a file ending in .test.js, and write a test() that calls your function and checks the result with expect(). Run your tests from the terminal, and keep each test small and focused on one behavior.
Unit tests check a single function or module in isolation. Integration tests check that several parts work together, such as an API route and its database. End-to-end tests drive a real browser through your app the way a user would.
