/harness-claude:test-mock-patternsfrom harness-claude
Mocks modules, functions, timers, and dates in Vitest and Jest to isolate units under test. Covers vi.fn(), vi.mock(), vi.spyOn(), fake timers, and partial module mocking.
Claude never picks this skill on its own
Its frontmatter has no description. Without a description, Claude Code leaves it out of the list Claude chooses from. Typing /harness-claude:test-mock-patterns still runs it.
What the skill costs in Claude's context, stage by stage
No description Claude Code can read, so Claude's list leaves it out: only you can start it.
SKILL.md joins the conversation and stays.
Mock modules, functions, and timers in Vitest and Jest to isolate units under test
vi.fn():import { vi, describe, it, expect } from 'vitest';
const sendEmail = vi.fn();
it('calls sendEmail with correct args', () => {
sendEmail('[email protected]', 'Welcome');
expect(sendEmail).toHaveBeenCalledWith('[email protected]', 'Welcome');
expect(sendEmail).toHaveBeenCalledTimes(1);
});
const getUser = vi.fn();
getUser.mockReturnValue({ id: '1', name: 'Alice' });
getUser.mockReturnValueOnce({ id: '2', name: 'Bob' }); // First call only
getUser.mockResolvedValue({ id: '1', name: 'Alice' }); // Async
getUser.mockRejectedValue(new Error('Not found')); // Async error
import { vi, describe, it, expect } from 'vitest';
vi.mock('./email-service', () => ({
sendEmail: vi.fn().mockResolvedValue(true),
}));
import { sendEmail } from './email-service';
import { createUser } from './user-service';
it('sends welcome email on user creation', async () => {
await createUser({ name: 'Alice', email: '[email protected]' });
expect(sendEmail).toHaveBeenCalledWith('[email protected]', expect.stringContaining('Welcome'));
});
const spy = vi.spyOn(console, 'log');
doSomething();
expect(spy).toHaveBeenCalledWith('Processing...');
spy.mockRestore(); // Restore original implementation
it('debounces rapid calls', () => {
vi.useFakeTimers();
const callback = vi.fn();
const debounced = debounce(callback, 300);
debounced();
debounced();
debounced();
expect(callback).not.toHaveBeenCalled();
vi.advanceTimersByTime(300);
expect(callback).toHaveBeenCalledTimes(1);
vi.useRealTimers();
});
it('uses current timestamp', () => {
vi.setSystemTime(new Date('2024-01-15T12:00:00Z'));
const record = createRecord();
expect(record.createdAt).toEqual(new Date('2024-01-15T12:00:00Z'));
vi.useRealTimers();
});
vi.mock('./utils', async () => {
const actual = await vi.importActual('./utils');
return {
...actual,
generateId: vi.fn().mockReturnValue('test-id-123'),
};
});
afterEach(() => {
vi.restoreAllMocks(); // Restores original implementations
// vi.clearAllMocks(); // Clears call history but keeps mock implementation
// vi.resetAllMocks(); // Resets implementation to vi.fn()
});
import type { UserService } from './user-service';
const mockUserService: Pick<UserService, 'findById' | 'create'> = {
findById: vi.fn(),
create: vi.fn(),
};
Mocking replaces real dependencies with controlled substitutes. This isolates the unit under test and lets you control inputs, verify outputs, and simulate error conditions.
Mock vs Spy vs Stub:
vi.fn()) — a fake function that records calls and can return configured valuesvi.spyOn()) — wraps a real function, recording calls while preserving the original behavior (unless overridden)mockReturnValue)Module mocking mechanics: vi.mock() is hoisted to the top of the file by Vitest's transformer. This means it executes before imports, replacing the module before any code uses it. The factory function is lazy — it runs when the module is first imported.
Common matchers for mock assertions:
toHaveBeenCalled() — called at least oncetoHaveBeenCalledTimes(n) — called exactly n timestoHaveBeenCalledWith(arg1, arg2) — called with specific argumentstoHaveBeenLastCalledWith(args) — last call had these argumentsexpect.any(Constructor) — matches any instance of the constructorexpect.stringContaining(str) — matches strings containing the substringTrade-offs:
https://vitest.dev/guide/mocking.html
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeRead when SKILL.md points to them.
793 skills in this plugin
/a11y-aria-patternsARIA Patterns> Apply ARIA roles, states, and properties correctly to enhance assistive technology support for custom widgets/a11y-color-contrastColor Contrast and Color Independence> Ensure sufficient color contrast ratios and never use color as the sole means of conveying information/a11y-form-patternsAccessible Form Patterns> Build accessible forms with proper labeling, grouped controls, inline validation, and clear error communicationOther plugins' skills for the same job