From quality-tools
Advanced mocking patterns for Jest and Vitest including module mocking, spies, and fake timers. PROACTIVELY activate for: (1) Module mocking, (2) Partial mocking with spies, (3) Mock lifecycle management, (4) Fake timers for time-dependent code, (5) Complex mock implementations. Triggers: "jest.mock", "vi.mock", "spyOn", "fakeTimers", "mockImplementation", "mockReturnValue", "mock lifecycle"
How this skill is triggered — by the user, by Claude, or both
Slash command
/quality-tools:advanced-js-mocking-patternsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
**Keywords**: jest.mock, vi.mock, spyOn, fakeTimers, mockImplementation
Keywords: jest.mock, vi.mock, spyOn, fakeTimers, mockImplementation
File Patterns: *.test.ts, *.spec.js
Modes: testing_frontend, testing_backend
// Mock entire module
jest.mock('axios');
import axios from 'axios';
test('fetches data', async () => {
(axios.get as jest.Mock).mockResolvedValue({ data: { id: 1 } });
const result = await fetchUser(1);
expect(result).toEqual({ id: 1 });
});
const obj = {
method1: () => 'original',
method2: () => 'original'
};
const spy = jest.spyOn(obj, 'method1');
spy.mockReturnValue('mocked');
obj.method1(); // 'mocked'
obj.method2(); // 'original' (not mocked)
expect(spy).toHaveBeenCalled();
beforeEach(() => {
jest.clearAllMocks(); // Reset call counts
});
afterEach(() => {
jest.restoreAllMocks(); // Restore original implementations
});
jest.useFakeTimers();
test('debounce function', () => {
const callback = jest.fn();
const debounced = debounce(callback, 1000);
debounced();
debounced();
debounced();
jest.advanceTimersByTime(1000);
expect(callback).toHaveBeenCalledTimes(1); // Only last call
});
const mock = jest.fn()
.mockImplementationOnce(() => 'first')
.mockImplementationOnce(() => 'second')
.mockImplementation(() => 'default');
mock(); // 'first'
mock(); // 'second'
mock(); // 'default'
npx claudepluginhub agentient/vibekit --plugin quality-toolsMocks modules, functions, and timers in Vitest and Jest to isolate units under test. Covers vi.fn(), module mocking, spying, and time manipulation.
Provides Jest testing patterns for unit tests, mocks, spies, snapshots, setup/teardown, and matchers including equality, truthiness, numbers, strings, and arrays.
Guides Bun test mocking using mock(), spyOn(), mock.module for functions, spies, modules, implementations, return values, and assertions.