Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add FetchMocker#mockObject() method to mock non-global fetch #59

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

nzakas
Copy link
Contributor

@nzakas nzakas commented Feb 20, 2025

This pull request introduces new functionality to mock the fetch function on specific objects, enhancing the flexibility of the FetchMocker class. The most important changes include updates to the documentation, the addition of new methods to the FetchMocker class, and corresponding tests.

Enhancements to FetchMocker functionality:

  • src/fetch-mocker.js: Added mockObject and unmockObject methods to allow mocking and unmocking the fetch function on specific objects. These methods store the original fetch functions in a WeakMap and replace them with the mocked fetch function.

Documentation updates:

Testing updates:

  • tests/fetch-mocker.test.js: Added tests for the new mockObject and unmockObject methods, verifying their functionality, including handling custom property names and restoring multiple mocked properties.

Codebase adjustments:

  • src/fetch-mocker.js: Updated the mockGlobal and unmockGlobal methods to use the new mockObject and unmockObject methods for consistency and code reuse.
  • src/fetch-mocker.js: Changed the documentation comment for the fetch property to reflect that it is the created fetch function, not the global fetch function.

@nzakas nzakas merged commit 43495c5 into main Feb 20, 2025
12 checks passed
@nzakas nzakas deleted the mock-object branch February 20, 2025 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant