Prohibit persistent mock setup methods; use their Once variants to prevent test bleeds.
| Property | Value |
|---|---|
| Type | suggestion |
| Fixable | No |
| Recommended | warn |
| Strict | error |
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 |
jest.fn().mockImplementationOnce(() => 'value');
jest.fn().mockReturnValueOnce(42);
jest.fn().mockResolvedValueOnce({ data: 'ok' });
jest.fn().mockRejectedValueOnce(new Error('failed'));jest.fn().mockImplementation(() => 'value');
jest.fn().mockReturnValue(42);
jest.fn().mockResolvedValue({ data: 'ok' });
jest.fn().mockRejectedValue(new Error('failed'));This rule has no options:
'zero-tolerance/no-mock-implementation': 'error'