Claude-skill-registry advanced-js-mocking-patterns

install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/advanced-js-mocking-patterns" ~/.claude/skills/majiayu000-claude-skill-registry-advanced-js-mocking-patterns && rm -rf "$T"
manifest: skills/data/advanced-js-mocking-patterns/SKILL.md
source content

Advanced JS Mocking Patterns Skill

Metadata (Tier 1)

Keywords: jest.mock, vi.mock, spyOn, fakeTimers, mockImplementation

File Patterns: *.test.ts, *.spec.js

Modes: testing_frontend, testing_backend


Instructions (Tier 2)

Module Mocking

// 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 });
});

Spies (Partial Mocking)

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();

Mock Lifecycle

beforeEach(() => {
  jest.clearAllMocks();  // Reset call counts
});

afterEach(() => {
  jest.restoreAllMocks();  // Restore original implementations
});

Fake Timers

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
});

Mock Implementations

const mock = jest.fn()
  .mockImplementationOnce(() => 'first')
  .mockImplementationOnce(() => 'second')
  .mockImplementation(() => 'default');

mock(); // 'first'
mock(); // 'second'
mock(); // 'default'

Anti-Patterns

  • Not clearing mocks between tests
  • Over-mocking (testing implementation)
  • Mocking internal modules
  • Forgetting to restore timers