Skip to content

Instantly share code, notes, and snippets.

@garbles
Last active July 21, 2021 15:49
Show Gist options
  • Save garbles/1a795c41a5c1bfc9cf7ae609c5bec35a to your computer and use it in GitHub Desktop.
Save garbles/1a795c41a5c1bfc9cf7ae609c5bec35a to your computer and use it in GitHub Desktop.
Setting up axios so that it doesn't use Jest's mocked XMLHttpRequest

You can set the default adapter for axios in your test setup file.

In package.json

{
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/test-setup.js",
  }
}

And then in test-setup.js

const path = require('path');
const axios = require('axios');
const adapterPath = path.join(
  path.dirname(require.resolve('axios')), 
  'lib/adapters/http'
);
const adapter = require(adapterPath);

axios.defaults.adapter = adapter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment