question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Built-in support for HTTP mocking

See original GitHub issue

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 😄

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:46
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
palmerj3commented, 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.

4reactions
dgreene1commented, Dec 6, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

mocking outbound http requests in go: you're (probably) doing ...
In short, it works by mocking the built-in http.Client by defining an interface with a Do method, which is implemented by both http....
Read more >
How do you know what's gone wrong when your API request ...
RFC 7807 from the IETF is a proposed standard aiming to do exactly this, by defining a standard format for HTTP API error...
Read more >
Mock API Response Templating - WireMock
A number of HTTP elements (query parameters, form fields, headers) can be single or multiple valued. The template request model and built-in helpers...
Read more >
API Mocking Service Release Notes - MuleSoft Documentation
HTTP 400 - Bad Request. The mocking service returned an empty response if the response content type was application/vnd.api+json .
Read more >
[PATCH 0/8] [RFC] Enhance credential helper protocol to include ...
The Smart HTTP protocol in Git supports a few different types of HTTP authentication ... Leverage existing authentication systems built-in to many operating ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found