web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

  1. # Suggest using `jest.spyOn()` (`prefer-spy-on`)
  2. When mocking a function by overwriting a property you have to manually restore
  3. the original implementation when cleaning up. When using `jest.spyOn()` Jest
  4. keeps track of changes, and they can be restored with `jest.restoreAllMocks()`,
  5. `mockFn.mockRestore()` or by setting `restoreMocks` to `true` in the Jest
  6. config.
  7. Note: The mock created by `jest.spyOn()` still behaves the same as the original
  8. function. The original function can be overwritten with
  9. `mockFn.mockImplementation()` or by some of the
  10. [other mock functions](https://jestjs.io/docs/en/mock-function-api).
  11. ```js
  12. Date.now = jest.fn(); // Original behaviour lost, returns undefined
  13. jest.spyOn(Date, 'now'); // Turned into a mock function but behaviour hasn't changed
  14. jest.spyOn(Date, 'now').mockImplementation(() => 10); // Will always return 10
  15. jest.spyOn(Date, 'now').mockReturnValue(10); // Will always return 10
  16. ```
  17. ## Rule details
  18. This rule triggers a warning if an object's property is overwritten with a jest
  19. mock.
  20. ### Default configuration
  21. The following patterns are considered warnings:
  22. ```js
  23. Date.now = jest.fn();
  24. Date.now = jest.fn(() => 10);
  25. ```
  26. These patterns would not be considered warnings:
  27. ```js
  28. jest.spyOn(Date, 'now');
  29. jest.spyOn(Date, 'now').mockImplementation(() => 10);
  30. ```