Jest Mock Generator
Generate Jest mock and spy code instantly. Choose module mocks, class mocks, function mocks, spies with custom behaviors, or fake timer setups — with optional TypeScript type assertions.
Frequently Asked Questions
What is the difference between jest.mock() and jest.spyOn()?▼
jest.mock() replaces an entire module with a mocked version — great for isolating tests from external dependencies. jest.spyOn() wraps an existing method on an object, letting you observe calls and optionally change behavior while preserving the original. Use jest.mock() for heavy isolation, jest.spyOn() for lighter observation with restore capability.
When should I use mockReturnValue vs mockImplementation?▼
Use mockReturnValue when the mock simply needs to return a fixed value — it's concise and readable. Use mockImplementation when you need conditional logic, access to arguments, or multiple return paths. Use mockReturnValueOnce for chained sequential values: mock.mockReturnValueOnce('first').mockReturnValueOnce('second').
How do I mock async functions?▼
Use mockResolvedValue (equivalent to mockReturnValue(Promise.resolve(value))) or mockRejectedValue (equivalent to mockReturnValue(Promise.reject(error))). For async spies, use jest.spyOn(object, 'method').mockResolvedValue(data). In tests, use await to resolve the promise and assert the result.
How do I mock a class constructor and its methods?▼
Call jest.mock('./MyClass') to auto-mock the class. Then use MyClass as jest.MockedClass<typeof MyClass> to access mock methods. Override methods via MyClass.prototype.method = jest.fn() or use mockImplementation on the constructor to return a custom object. Always call mockClear() in beforeEach to reset call counts.
What does mockRestore() do and when should I use it?▼
mockRestore() restores a jest.spyOn() spy back to its original implementation. It only works on spies created with jest.spyOn() — not manual jest.fn() mocks. Call it in afterEach to prevent test pollution. If you forget to restore, later tests may interact with stale spies. jest.restoreAllMocks() in afterEach restores all spies at once.
How do I reset or clear mocks between tests?▼
Use jest.clearAllMocks() to clear call history (calls, instances, results) without removing implementations. jest.resetAllMocks() also removes mock implementations, returning jest.fn(). jest.restoreAllMocks() restores spies created with jest.spyOn(). Configure these automatically in jest.config.js with clearMocks, resetMocks, or restoreMocks: true.
Related Products
on GumroadTypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →DevToolkit Starter Kit
A ready-to-deploy developer tools website with 12 built-in tools. Astro + Preact + Tailwind. Deploy to Cloudflare Pages for free. Built-in SEO, dark mode, and ad-ready layout.
Buy on Gumroad →DevPlaybook Interview Vault
210+ real technical interview questions with model answers, asked at Google, Meta, Amazon, Microsoft, Stripe, and Shopify. Covers Frontend (55), Backend (55), System Design (50), and DSA (50). Available in English and Traditional Chinese.
Buy on Gumroad →You Might Also Need
Jest Coverage Config Generator
Generate Jest configuration with coverage thresholds, reporters, and collectCoverageFrom patterns. Set line, function, branch, and statement thresholds.
Open free tool →
Jest Testing Framework Guide
Complete guide to Jest JavaScript testing framework. Learn describe/it/expect, mocking, snapshot testing, async tests, coverage, and best practices for unit and integration testing.
Open free tool →
Jest to Vitest Migrator
Convert Jest configuration and test syntax to Vitest automatically. Handles jest.config.js, moduleNameMapper, jest.fn(), jest.mock() and more.
Open free tool →
More Free Tools
Related Articles
Vitest: The Fast Unit Testing Framework for Vite Projects - 2026 Guide
Complete guide to Vitest in 2026: setup, configuration, mocking, coverage, watch mode, and a detailed comparison with Jest to help you decide which testing framework fits your project.
Vitest vs Jest vs Mocha: Complete Testing Framework Comparison 2026
A comprehensive comparison of Vitest, Jest, and Mocha for JavaScript and TypeScript projects in 2026. Covers speed benchmarks, configuration, TypeScript support, mocking, code coverage, and migration strategies.
Vitest vs Jest 2026: Modern Testing Comparison
Comprehensive comparison of Vitest vs Jest in 2026: speed benchmarks, ESM support, migration guide, snapshot testing, mocking, watch mode, and coverage reporting.
Jest vs Vitest vs Bun Test: JavaScript Testing Framework Comparison 2026
A technical comparison of Jest, Vitest, and Bun Test for JavaScript/TypeScript testing in 2026 — covering setup speed, watch mode, coverage, ESM support, and migration paths.
Modern JavaScript Testing 2026: Vitest vs Jest vs Playwright
Complete guide to JavaScript testing in 2026. Compare Vitest vs Jest for unit tests, Playwright vs Cypress for E2E, component testing, coverage tools, and CI/CD integration.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →