Skip to content

Instantly share code, notes, and snippets.

@cdriehuys
Last active September 14, 2022 17:07
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdriehuys/b22c833716826f936cae309428624367 to your computer and use it in GitHub Desktop.
Save cdriehuys/b22c833716826f936cae309428624367 to your computer and use it in GitHub Desktop.
Typescript definition file to make axios work with nock

Axios, Nock, and Typescript

There is a documented issue describing how axios and nock do not work well together. The workaround described in this comment works.

import axios from 'axios';
import httpAdapter from 'axios/lib/adapters/http';
import nock from 'nock';

axios.defaults.adapter = httpAdapter;

// Some tests using nock ...

The problem with this solution is that Typescript complains httpAdapter from axios/lib/adapters/http implicitly has the any type. To combat this, add a definitions file to your project with the contents of axios.d.ts.

declare module 'axios/lib/adapters/http' {
import { AxiosAdapter } from 'axios';
const httpAdapter: AxiosAdapter;
namespace httpAdapter { }
export = httpAdapter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment