Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 13 / Topic 5 of 5

Verifying a lazy chunk actually loads on demand

"It's lazy" is a claim worth actually proving, not assuming from the syntax, at two different levels.

At the test level: spy on the import itself, not on the component - that is what proves "on demand" rather than just "it eventually renders":

// examples/05_lazy_and_suspense_settings_panel.test.jsx
const { importSpy } = vi.hoisted(() => ({ importSpy: vi.fn() }));
vi.mock("./04_lazy_settings_panel.jsx", async () => {
  importSpy();
  return vi.importActual("./04_lazy_settings_panel.jsx");
});

With the launcher rendered and still closed, importSpy really has zero calls - the module has not been touched. Clicking "Open settings" brings it to exactly one call, and crucially, closing the panel and opening it again does not bring it to two - React.lazy caches the resolved module forever once it succeeds, so every later open just reads that same cached result (examples/05_lazy_and_suspense_settings_panel.test.jsx).

At the build level: a real vite build of this exact component, run once while writing this part (the same verification path this course's TOOLING.md uses for every rendered claim), really produces two separate files, not one:

dist/assets/index-CgX1r-Ox.js           220.99 kB
dist/assets/SettingsPanel-_KKx9NEI.js     0.24 kB

SettingsPanel's own ~250-byte chunk ships completely separately from the ~221 KB bundle holding React, ReactDOM, and everything else - and the browser never requests that small file at all unless import() actually runs, which lazy() only triggers the first time SettingsPanel is asked to render. That split is what "on demand" means physically, not just inside a test.

Done here? The practice set for Part 13 — including the solutions — is on the part page.

Search the guide