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.

detect jest and use http adapter instead of XMLhttpRequest

See original GitHub issue

So I had a peculiar evening. We have an unconventional test suite in Jest. We run it in the regular mode, not with "testEnvironment": "node" so it has window. In one test I was trying to fetch something with axios, but it always broke down and showed me a Network error. I was banging my head against the table and had a nice debug session and eventually found out this thread: https://stackoverflow.com/questions/42677387/jest-returns-network-error-when-doing-an-authenticated-request-with-axios Which was awesome. Using this spectacular hack:

if (process.env.LOOOP_ENVIRONMENT === 'test') {
    const path = require('path') 
    const lib = path.join(
      path.dirname(require.resolve('axios')),
      'lib/adapters/http'
    )
    const http = require(lib)
    config.adapter = http
  }

I was able resolve the problem. What I want to suggest is-since jest is very popular test runner and a lot of people are running jest in regular mode, not in node they will often bang their heads.

Maybe it would be good to detect when we’re running in jest and refrain from using XMLHttpRequest? Is that too much to ask?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:33
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

42reactions
kmarkowcommented, Mar 21, 2018

I’ve had a fun afternoon too. I solved it by adding testEnvironment: 'node', to jest.config.js file.

By default jest runs tests in jsdom mode which adds XmlHttpRequest to global scope which in turns causes axios to think it’s running in a browser and making requests via XmlHttpRequest instead of node’s native library.

12reactions
mpacarycommented, Mar 29, 2019

If you need to keep jsdom as testEnvironment (Vue components testing…), here is another nice workaround:

Add global.XMLHttpRequest = undefined in your Jest setup file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest returns "Network Error" when doing an authenticated ...
Solution change axios adapter to http. import axios from 'axios'; //This WORKS test('testing with headers ...
Read more >
Using XMLHttpRequest - Web APIs | MDN
In this guide, we'll take a look at how to use XMLHttpRequest to issue HTTP requests in order to exchange data between the...
Read more >
Making HTTP requests - Vue Test Utils
We do so by using the flushPromises() function from @vue/test-utils . ... Jest to mock any call to `axios.get` // and to return...
Read more >
xhr-mock - npm
Utility for mocking XMLHttpRequest.. Latest version: 2.5.1, last published: 3 years ago. Start using xhr-mock in your project by running ...
Read more >
How to Implement Request Retry Using Axios
In the browser, through XMLHttpRequest and the Fetch API, we can make HTTP requests to get data from the server. However, in practice,...
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