-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
RFC: Built-in support for HTTP mocking #6081
Comments
I'm interested in this. Right now I'm facing an issue where I want to assert that an HTTP request was made with a certain shape (host, path, search, body, etc), without caring about the response or even the return value from the containing function/method. Do you think that use calls fall under this RFC? |
Yes, I'd say so 🙂 The functions would be jest spies essentially, so you would be able to do @palmerj3 IIRC you said you had something at Spotify along these lines, any news on that? |
@SimenB yeah we have this (something similar) at Spotify and our intention is to open source it this quarter. Happy to hop on video chat sometime and show you how it works. |
@palmerj3 awesome! Let's talk about it in Stockholm later this month :) |
Yeah, @palmerj3 showed it to me, and it looked pretty good! 🙂 I don't know how the effort to open source it is going, though. |
+1 Love to see what you're working on @SimenB |
any news on this? |
Not exactly what this issue is about but we did release this https://github.com/spotify/polly-jest-presets |
Doesn't https://github.com/ScaleLeap/jest-polly/ provide this? |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
There's often a need to mock network calls when writing unit tests. While one can use
jest.mock
to implement the http API of your choice - or use modules likejest-fetch-mock
that only works forfetch
- this is often technical debt the moment you swap out your HTTP abstraction. If one migrates torequest
oraxios
, they often have to also switch out their mock implementation. This pain is potentially even worse if moving from e.g. traditionalxhr
requests tofetch
, or vice versa. Or even starting to use GraphQL instead of the more traditional REST based approaches.One could set up a mock server with some canned responses (e.g.
create-test-server
is nice), but that is often slower than you want it to be, especially for unit tests which needs to run against a clean instance every single test.A third alternative, and what I want to see in Jest, is to mock out the http layer of the runtime.
nock
is a module which does this for node (so works fine also for JSDom based tests), but I find its API clunky to use and work with. Other alternatives aresinon
orfaux-jax
.If we could provide a simpler and/or more intuitive API out of the box with Jest, I think that might solve a lot of common issues where people today find solutions to plug into Jest.
I don't have any particular API suggestions here, but I think something akin to
jest.useFakeTimers()
(sayjest.useFakeNetwork()
) with related APIs such asjest.fakeGet('url', {headers}, body)
might be a good start. Looking at existing solutions for inspirations sounds like a great way forward! Having something for GraphQL out of the box would be awesome as well.One source of inspiration beyond
sinon
andfaux-jax
could beava-nock
. Especially theNOCK_MODE
feature sounds like an awesome watch plugin for jest.Done! 10 days isn't too bad :D
The text was updated successfully, but these errors were encountered: