Files
ai-course/node_modules/eslint-plugin-jest/docs/rules/prefer-spy-on.md
KQL ce6aa207e9 fix: 修复图片路径以适配GitHub Pages base path
- 将所有图片路径从绝对路径改为使用 process.env.PUBLIC_URL
- 修复 HomePage.tsx 中所有图片引用
- 修复 CoursePage.tsx 中所有图片引用
- 确保图片在 GitHub Pages 上正确加载

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 09:24:45 +08:00

1.3 KiB

Suggest using jest.spyOn() (prefer-spy-on)

When mocking a function by overwriting a property you have to manually restore the original implementation when cleaning up. When using jest.spyOn() Jest keeps track of changes, and they can be restored with jest.restoreAllMocks(), mockFn.mockRestore() or by setting restoreMocks to true in the Jest config.

Note: The mock created by jest.spyOn() still behaves the same as the original function. The original function can be overwritten with mockFn.mockImplementation() or by some of the other mock functions.

Date.now = jest.fn(); // Original behaviour lost, returns undefined

jest.spyOn(Date, 'now'); // Turned into a mock function but behaviour hasn't changed
jest.spyOn(Date, 'now').mockImplementation(() => 10); // Will always return 10
jest.spyOn(Date, 'now').mockReturnValue(10); // Will always return 10

Rule details

This rule triggers a warning if an object's property is overwritten with a jest mock.

Default configuration

The following patterns are considered warnings:

Date.now = jest.fn();
Date.now = jest.fn(() => 10);

These patterns would not be considered warnings:

jest.spyOn(Date, 'now');
jest.spyOn(Date, 'now').mockImplementation(() => 10);