IanVS / vitest-fetch-mock

Vitest mock for fetch, forked from jest-fetch-mock
MIT License
68 stars 9 forks source link

Allow Passing Custom `fetch` Values to Override `cross-fetch` Usage #22

Closed joealden closed 1 month ago

joealden commented 2 months ago

I'm in the process of moving a project to ESM, and as a part of that I'm now able to move to node-fetch V3 (as it's ESM only).

V3 also brings behavioural changes to make it more spec compliant - a notable one being that body parse failures now return SyntaxError instead of FetchError. As the latest cross-fetch version still depends on node-fetch V2 (presumably for CJS compatibility - which I understand), this means that the behaviour of our tests won't match what will happen in production.

As a flexible (yet hopefully simple to implement) solution, could we add an optional 2nd argument to createFetchMocker called something like overrides, that would be an object optionally containing fetch, Response, Headers and Request (all the values that createFetchMocker uses from cross-fetch)?

This would allow someone to pass whatever fetch implementation they want, which could be node-fetch V3, or thinking to the future, the built-in fetch values that are now a part of Node and are stable in Node 22.

dirkluijk commented 1 month ago

Should be fixed by https://github.com/IanVS/vitest-fetch-mock/pull/24.

IanVS commented 1 month ago

Closed by https://github.com/IanVS/vitest-fetch-mock/pull/24, will be released in the next version.