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

RFC: Built-in support for HTTP mocking #6081

Closed
SimenB opened this issue Apr 27, 2018 · 13 comments
Closed

RFC: Built-in support for HTTP mocking #6081

SimenB opened this issue Apr 27, 2018 · 13 comments

Comments

@SimenB
Copy link
Member

SimenB commented Apr 27, 2018

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 like jest-fetch-mock that only works for fetch - this is often technical debt the moment you swap out your HTTP abstraction. If one migrates to request or axios, they often have to also switch out their mock implementation. This pain is potentially even worse if moving from e.g. traditional xhr requests to fetch, 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 are sinon or faux-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() (say jest.useFakeNetwork()) with related APIs such as jest.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 and faux-jax could be ava-nock. Especially the NOCK_MODE feature sounds like an awesome watch plugin for jest.

image
Done! 10 days isn't too bad :D

@ewolfe
Copy link

ewolfe commented Sep 1, 2018

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?

@SimenB
Copy link
Member Author

SimenB commented Sep 1, 2018

Yes, I'd say so 🙂 The functions would be jest spies essentially, so you would be able to do toHaveBeenCalledWith() and other goodies.


@palmerj3 IIRC you said you had something at Spotify along these lines, any news on that?

@palmerj3
Copy link
Contributor

palmerj3 commented Sep 1, 2018

@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.

@SimenB
Copy link
Member Author

SimenB commented Sep 2, 2018

@palmerj3 awesome! Let's talk about it in Stockholm later this month :)

@ThisIsMissEm
Copy link

@palmerj3 @SimenB was there any follow-up to this?

@SimenB
Copy link
Member Author

SimenB commented Oct 27, 2018

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.

@dgreene1
Copy link

dgreene1 commented Dec 6, 2018

+1 Love to see what you're working on @SimenB

@codepunkt
Copy link

any news on this?

@palmerj3
Copy link
Contributor

Not exactly what this issue is about but we did release this https://github.com/spotify/polly-jest-presets

@jeking3
Copy link

jeking3 commented Jun 24, 2021

Doesn't https://github.com/ScaleLeap/jest-polly/ provide this?

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Jun 24, 2022
@github-actions github-actions bot removed the Stale label Jul 15, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Jul 15, 2023
@github-actions
Copy link

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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants