Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.72 KB

File metadata and controls

53 lines (38 loc) · 1.72 KB

no-mock-implementation

Prohibit persistent mock setup methods; use their Once variants to prevent test bleeds.

Rule Details

Property Value
Type suggestion
Fixable No
Recommended warn
Strict error

Rationale

mockImplementation, mockReturnValue, mockResolvedValue, and mockRejectedValue configure a mock persistently for the lifetime of the test suite (or until manually reset). This means a mock configured in one test silently bleeds into subsequent tests, leading to hard-to-diagnose ordering-dependent failures.

The Once variants (mockImplementationOnce, mockReturnValueOnce, mockResolvedValueOnce, mockRejectedValueOnce) only apply for a single call and then revert. This eliminates the source of test bleeds entirely.

Banned Replacement
mockImplementation mockImplementationOnce
mockReturnValue mockReturnValueOnce
mockResolvedValue mockResolvedValueOnce
mockRejectedValue mockRejectedValueOnce

Examples

✅ Correct

jest.fn().mockImplementationOnce(() => 'value');
jest.fn().mockReturnValueOnce(42);
jest.fn().mockResolvedValueOnce({ data: 'ok' });
jest.fn().mockRejectedValueOnce(new Error('failed'));

❌ Incorrect

jest.fn().mockImplementation(() => 'value');
jest.fn().mockReturnValue(42);
jest.fn().mockResolvedValue({ data: 'ok' });
jest.fn().mockRejectedValue(new Error('failed'));

Configuration

This rule has no options:

'zero-tolerance/no-mock-implementation': 'error'