Mock a dependency

Mock a dependency in JavaScript — with the four heights of help laid out: do it now, make it easier for the next person to accept, work out the right move when you are stuck, and learn the pattern so it stops coming back.

4prompt heights
Open it in the interactive atlas →

The four heights

The same task, four distances: today's deadline, the next reviewer, the stuck moment, the pattern.

Execute — do the immediate task

+
Replace the real API client in this module with a mock so unit tests run offline. In the test, stub…
Replace the real API client in this module with a mock so unit tests run offline. In the test, stub the fetchUser function to return a resolved promise with sample data, verify the component renders the sample user's name, and ensure the mock is restored after the test.

Improve — make it easier to accept

+
Before I submit the test changes, make the mock easier to review: keep the mocked shape identical…
Before I submit the test changes, make the mock easier to review: keep the mocked shape identical to the real client, name the mock functions to match production names, surface failure modes (reject case) so reviewers see error handling, and avoid stubbing internal helper functions that make the mock brittle.

Decide — diagnose the stuck moment

+
After adding a mock for the API client in one test file, other tests started receiving the stubbed…

Mock leaks into other tests

After adding a mock for the API client in one test file, other tests started receiving the stubbed responses and failing. I used a global module mock and I suspect teardown didn't restore it. I can't see where the leak originates. What is the likely cause and the safest change to isolate the mock to this test file only?

Become — change the pattern

+
Over time we have many tests that mock modules differently; some use global module mocks, some…

tests rely on lots of ad hoc module mocks

Over time we have many tests that mock modules differently; some use global module mocks, some replace require cache, and others monkey-patch. This inconsistency causes leaks and slow, confusing failures. Recommend one team-wide approach to mocking dependencies that minimizes leaks and makes tests easier to understand, and describe the single initial refactor to apply across the codebase.

Next to this one

Other programming language work people do in JavaScript.

Every task here came from the work, not from a feature list — which is why the prompts name what you want done and never the button that does it. The tool changes; the work does not.
Copyright © LLOS.ai · 2026 — original pedagogy, voice, and design — all rights reserved.

The rest of the map

Same library, five ways in.