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

Help needed making fetch-mock compatible with jest #566

Closed
wheresrhys opened this issue Jan 15, 2019 · 6 comments
Closed

Help needed making fetch-mock compatible with jest #566

wheresrhys opened this issue Jan 15, 2019 · 6 comments
Labels

Comments

@wheresrhys
Copy link
Contributor

@wheresrhys wheresrhys commented Jan 15, 2019

Hello.

I have a compatibility bug between fetch-mock and jest which, in summary, is because fetch-mock requires node-fetch, and jest tries to mock node-fetch using fetch-mock globally, so there is a vicious cycle. wheresrhys/fetch-mock#407

It would probably solve the problem if node-fetch could provide a different path to require its helpers from. Are you open to a PR that puts Headers, Request, etc in a separate file to the main fetch implementation, and documents that they can be required from this path e.g. node-fetch/lib/classes?

@bitinn

This comment has been minimized.

Copy link
Collaborator

@bitinn bitinn commented Jan 16, 2019

Personally, without knowing all details, I would prefer to see if jest has a solution to this problem. It seems like an oversight on jest's part.

(To mock something means you need to know its interface, but JS doesn't have interface per se, so I hope jest already designed with that in mind: while your test code need mocking, your dependency for the test code, may not)

@wheresrhys

This comment has been minimized.

Copy link
Contributor Author

@wheresrhys wheresrhys commented Jan 16, 2019

They do kinda. There's a requestActual method which is supposed to cater for this kind of issue, but I've filed a bug as it's not working within node_modules facebook/jest#7620. No response yet, but I figure a solution will be slow coming, if at all; 476 open issues, and being able to requestActual from within node_modules might well be an edge case they don't care about. Whereas hopefully I can persuade you that making it possible to use one of node-fetch's main complementary test utilities with the fastest growing test framework is something worth fixing.

@bitinn

This comment has been minimized.

Copy link
Collaborator

@bitinn bitinn commented Jan 16, 2019

@wheresrhys can you quickly outline what the changes might look like? because to me it sounds we are working around jest's problem (what I hear: jest mock dependency globally so we avoid the mocked api and use another require path?)

To add:

  • why are fetch used globally in the first place? isomorphism?
  • how does jest handle global modules? just mock them?

how about this doc?

https://github.com/facebook/jest/blob/master/docs/BypassingModuleMocks.md

@bitinn

This comment has been minimized.

Copy link
Collaborator

@bitinn bitinn commented Jan 16, 2019

(I haven't used jest and I might not fully understand your users' problem)

(It also feel kinda crazy that the doc mention node-fetch explicitly but it doesn't work as expected?)

@wheresrhys

This comment has been minimized.

Copy link
Contributor Author

@wheresrhys wheresrhys commented Jan 16, 2019

Sure, I'll do a PR of the proposed changes and also attempt to explain the problem in a gist. i've not come across that doc you shared before. I will analyse what it says for ideas. Where jest breaks down is in requiring a module as a dependency of the thing that mocks it. they have requestActual to get around it, but it's not working in the fetch-mock case for some reason (I guess because some features may deliberately be scoped to the application folder excluding node_modules)

@wheresrhys

This comment has been minimized.

Copy link
Contributor Author

@wheresrhys wheresrhys commented Jan 19, 2019

Hi, I found a workaround in the end http://www.wheresrhys.co.uk/fetch-mock/#usageusage-with-jest. Thanks for being open to discussion. Still waiting on a reply from jest 🙄

@wheresrhys wheresrhys closed this Jan 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.