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

fetch-mock does not forward requests correctly when jsdom-global is in effect #608

Open
fluggo opened this issue May 15, 2021 · 0 comments · May be fixed by #611
Open

fetch-mock does not forward requests correctly when jsdom-global is in effect #608

fluggo opened this issue May 15, 2021 · 0 comments · May be fixed by #611

Comments

@fluggo
Copy link

fluggo commented May 15, 2021

fetch-mock has a function patchNativeFetchForSafari that wraps the native fetch when it detects it's running in Safari. By a funny coincidence, what fetch-mock looks for is exactly how jsdom identifies itself.

The side effect is that, when jsdom-global is loaded, the patchNativeFetchForSafari function only passes the first argument on to the nativeFetch function. Like so:

require('jsdom-global/register');
const fetchMock = require('fetch-mock');

fetchMock.config.fetch = function() {
  console.log(arguments);
  return Promise.reject(new Error('nyeh'));
}

const sandbox = fetchMock.sandbox();
sandbox.spy(true);

sandbox('http://localhost:2021', { headers: { 'cookie': 'id=5' } }).catch(() => {});

Produces:

fluggo@NONEYABIZ:~/software/fetchmocktest$ node test.js 
[Arguments] { '0': 'http://localhost:2021/' }

...leaving out the specified header.

Either the detection needs to be more specific, nativeFetch should be called as nativeFetch.apply(arguments), or both. For my purposes, my workaround will probably be navigator = { vendor: 'Compy' };.

fluggo pushed a commit to fluggo/fetch-mock that referenced this issue May 15, 2021
This is not a great fix; it ignores that method is only inspected when it's
in the first fetch() parameter, and it glosses over the fact that the
navigator object, for historical reasons, is very constrained in the
responses it gives (which is why the code snagged jsdom in the first
place).

It does, however, solve my immediate need of getting fetch-mock to
interoperate with jsdom.

Fixes wheresrhys#608
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 a pull request may close this issue.

1 participant