> For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt.

# prefer-spy-on

[Added in v0.6.4](https://github.com/web-infra-dev/rslint/releases/tag/v0.6.4)
## Configuration

| Preset                   | Configured Value |
| ------------------------ | ---------------- |
| ✅ jestPlugin.configs.all | `"error"`        |


```ts title=rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/prefer-spy-on': 'error',
    },
  },
]);
```

## Rule Details

When a function is mocked by assigning `jest.fn()` to an object property, the
original implementation is lost and must be restored manually during cleanup.
`jest.spyOn()` wraps the existing method as a mock while preserving its behavior,
and Jest can restore it with `jest.restoreAllMocks()`, `mockFn.mockRestore()`, or
the `restoreMocks` config option.

The mock returned by `jest.spyOn()` behaves like the original function until you
change it with `mockImplementation()`, `mockReturnValue()`, or another mock API.

Examples of **incorrect** code for this rule:

```js
Date.now = jest.fn();
Date.now = jest.fn(() => 10);
obj.a = jest.fn();
window.fetch = jest.fn(() => ({})).mockReturnValue('ok');
```

Examples of **correct** code for this rule:

```js
jest.spyOn(Date, 'now');
jest.spyOn(Date, 'now').mockImplementation(() => 10);
jest.spyOn(obj, 'a').mockImplementation();
jest.spyOn(window, 'fetch').mockReturnValue('ok');
```

## Original Documentation

- [eslint-plugin-jest: prefer-spy-on](https://github.com/jest-community/eslint-plugin-jest/blob/v29.16.0/docs/rules/prefer-spy-on.md)
- [Source code](https://github.com/jest-community/eslint-plugin-jest/blob/v29.16.0/src/rules/prefer-spy-on.ts)
