testdouble / quibble

Makes it easy to replace require'd dependencies.
94 stars 25 forks source link

Any way to mock Node globals? #79

Closed markcellus closed 1 year ago

markcellus commented 1 year ago

I love this package! I use it with mocha. Node.js has introduced fetch() as a Node global and I'd like to mock this in my tests. Is there a way to mock Node globals using quibble?

searls commented 1 year ago

I wouldn't recommend it, but you're certainly welcome to try. For globals that aren't imported as modules, it probably makes sense to use the parent testdouble.js package and do something like td.replace(globalThis, 'fetch') and see if that works.

markcellus commented 1 year ago

Thanks @searls. I'll give that a try and update here if it works.

I agree that it isnt recommended. I've already abstracted any fetch functions in my project to a separate file and just mock those functions in other tests. That works great. But I actually now need to test the functions in the abstracted fetch file 😅. So not sure how to get around it.