Loading topic
npx vitest from the previous chapter. It also assumes you know ES module syntax and arrow functions. Here you will learn how Vitest finds test files and how to group tests so the output reads clearly.2 min readdescribe and it and run it with npx vitest. You should also know how JavaScript compares primitives and objects.1 min readtest or it, use expect with matchers such as toBe and toThrow, and read code that uses promises and async/await.2 min readdescribe, it, and expect, and that you know how promises and async/await work. Lifecycle hooks let you run code before and after your tests. You use them to build fresh test data and to clean up anything a test leaves behind.2 min readdescribe, it, and expect, and that you know how promises and async/await work. It covers mock functions created with vi.fn. Later chapters cover spying on existing methods and replacing whole modules.2 min readdescribe, it, and expect, that you know how vi.fn records calls, and that you have used afterEach hooks. A spy builds on all three.2 min readvi.fn works and how to test async code with await and .resolves. It covers vi.mock, which replaces a whole module for every file that imports it during a test run. You need this when the code under test imports its dependencies directly, so you canno2 min readtest, it, and describe, and that you know the common expect matchers such as toBe, toEqual, and toThrow.2 min readdescribe, test, and it, and run them with npx vitest (watch mode) or npx vitest run (single run). Once a suite grows, you often want to run only part of it, usually while debugging one failure. Vitest gives you two ways to do that. Modifiers in2 min readdescribe and it, create mock functions with vi.fn, and use beforeEach and afterEach hooks. Code that relies on setTimeout, setInterval, or the current date is hard to test with real time. A test that waits a real second is slow, and a test 2 min readtest and expect and run Vitest in watch mode and single-run mode. A snapshot assertion saves the serialized output of a value the first time a test runs. Each later run compares the new output with the saved copy. Snapshots are useful when output is 2 min readtest and expect, use beforeEach, and edit a vitest.config.js file as shown in the previous chapter.2 min readvitest.config.js file, as set up in the configuration chapter, and that you can run tests from the terminal. Code coverage tells you which parts of your source code ran while your tests ran. It shows you where you have no tests. It does2 min readdescribe/it, lifecycle hooks, vi.fn, vi.spyOn, vi.mock, and fake timers. Here those tools are put together into a few habits that keep a suite trustworthy as it grows.2 min read