diff --git a/jest.config.ts b/jest.config.ts index 4c713a0..e4bdc06 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -3,6 +3,7 @@ import type { JestConfigWithTsJest } from 'ts-jest' const jestConfig: JestConfigWithTsJest = { testEnvironment: 'node', testMatch: ['**/?(*.)+(spec|test).ts?(x)'], + setupFilesAfterEnv: ['/src/__tests__/setup.ts'], transform: { '^.+\\.[tj]s?$': [ 'ts-jest', diff --git a/src/__tests__/setup.ts b/src/__tests__/setup.ts new file mode 100644 index 0000000..7410b3f --- /dev/null +++ b/src/__tests__/setup.ts @@ -0,0 +1,18 @@ +import MockAdapter from 'axios-mock-adapter'; +import type { AxiosRequestConfig } from 'axios'; + +/** + * axios >= 0.33 hardened its internal config/header merge to build null-prototype objects. + * axios-mock-adapter reads `config.headers.constructor.name` to detect AxiosHeaders, which + * throws on such objects. Hand the mock adapter a normal-prototype copy of the headers. + * + * @see https://github.com/ctimmerm/axios-mock-adapter/blob/master/src/handle_request.js + */ +const originalAdapter = MockAdapter.prototype.adapter; +MockAdapter.prototype.adapter = function patchedAdapter(this: MockAdapter) { + const handleRequest = originalAdapter.call(this) as (config: AxiosRequestConfig) => Promise; + return ((config: AxiosRequestConfig) => + config.headers && Object.getPrototypeOf(config.headers) === null + ? handleRequest({ ...config, headers: { ...config.headers } }) + : handleRequest(config)) as ReturnType; +}; diff --git a/src/client.test.ts b/src/client.test.ts index ef6e32f..1de5be7 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -281,7 +281,7 @@ describe('OpenAPIClientAxios', () => { expect(d.timeout).toBe(1234); expect(d.withCredentials).toBe(true); expect(d.adapter).toBe(userAdapter); - expect(d.auth).toStrictEqual({ + expect(d.auth).toEqual({ username: 'fake', password: 'fakepassword' }), @@ -296,7 +296,7 @@ describe('OpenAPIClientAxios', () => { expect(d.validateStatus).toBe(userValidateStatus); expect(d.maxRedirects).toBe(99); expect(d.socketPath).toBe('/fake/path/example'); - expect(d.proxy).toStrictEqual({ + expect(d.proxy).toEqual({ host: '1.2.3.4', port: 9876, auth: {