No Login Data Private Local Save

Jest/Vitest Unit Test Generator - Online Function Stub

6
0
0
0

Jest/Vitest Mock Generator

Generate production-ready function stubs, spies & module mocks instantly

Use camelCase for best results
Comma-separated, leave empty if none
Use quotes for strings: 'hello'
🔧 Basic fn() 📤 Return Value ⚙️ Custom Impl ✅ Async Resolve ❌ Async Reject 🔄 Mock Once
Quick Fill:
Generated Test Code ~15 lines

      
    
Download .test.js
Frequently Asked Questions

Jest uses jest.fn(), jest.spyOn(), and jest.mock() as its core mocking APIs. Vitest provides nearly identical APIs through vi.fn(), vi.spyOn(), and vi.mock(). The main difference is that Vitest is ESM-first and integrates natively with Vite, making it faster for projects already using Vite. Both support the same mock chaining methods like .mockReturnValue(), .mockImplementation(), and .mockResolvedValue().

Stub: Provides predefined responses to calls. Use when you need to control a dependency's output (e.g., .mockReturnValue()).
Mock: Records calls and verifies behavior. Use when you need to assert how a function was called (e.g., expect(mockFn).toHaveBeenCalledWith(...)).
Spy: Wraps an existing function to observe calls while preserving original behavior (unless stubbed). Use jest.spyOn() when you need to watch real implementations. In practice, Jest/Vitest combine all three concepts into fn().

Always use beforeEach to reset mock state: call mockFn.mockClear() to reset call history, mockFn.mockReset() to also reset implementations, or mockFn.mockRestore() for spies. You can also configure clearMocks: true in your Jest/Vitest config to auto-clear all mocks before each test. For module mocks, use jest.unmock() or vi.unmock() in afterEach if needed.

Use .mockResolvedValue(value) for successful promises and .mockRejectedValue(error) for failed ones. For example: const mockFetch = jest.fn().mockResolvedValue({ data: [] }). Then in your test, use await or .resolves/.rejects matchers. For more complex async behavior, use .mockImplementation(() => Promise.resolve(data)) or combine with mockImplementationOnce for sequential async calls.

Yes! Use jest.mock('module-name') or vi.mock('module-name') at the top of your test file (it gets hoisted automatically). You can provide a factory function: jest.mock('axios', () => ({ get: jest.fn().mockResolvedValue({ data: {} }) })). For Vitest, the syntax is identical: vi.mock('axios', () => ({ ... })). For Node.js built-ins like fs, you may need jest.mock('fs') or use vi.mock('fs') with Vitest.

Keep mocks close to the tests that use them. For manual mocks, place them in a __mocks__ directory adjacent to the module being mocked. Name mock files after the module they replace (e.g., __mocks__/axios.js). Use descriptive variable names for inline mocks: const mockGetUser = jest.fn(). Group related mocks in describe blocks and clean them up in beforeEach/afterEach hooks. Avoid sharing mock state across unrelated test suites.

Use jest.requireActual() or vi.importActual() to get the real module, then spread it and override specific exports: jest.mock('../utils', () => ({ ...jest.requireActual('../utils'), helperFn: jest.fn() })). In Vitest: vi.mock('../utils', async () => ({ ...(await vi.importActual('../utils')), helperFn: vi.fn() })). This technique, called "partial mocking," lets you isolate the function under test while keeping its dependencies intact.