From 91577c8c098c88c4112ef7f7e9913712f81a1f8f Mon Sep 17 00:00:00 2001 From: Viljami Kuosmanen Date: Sat, 25 Jul 2026 16:06:52 +0200 Subject: [PATCH] Fix tests against axios 0.33 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit axios 0.31–0.33 hardened `utils.merge` to build results with `Object.create(null)`. `dispatchRequest` runs every config through that merge, so `config.headers` reaches the adapter with a null prototype and axios-mock-adapter's `config.headers.constructor.name === 'AxiosHeaders'` check throws (same in axios-mock-adapter 2.1.0, so upgrading it does not help). The same hardening makes `mergeConfig` produce null-prototype `defaults.auth` / `defaults.proxy`, which `toStrictEqual` rejects on prototype mismatch. The library itself works fine under axios 0.33 — this was purely a test tooling incompatibility, surfacing as 30 failures in the `0.*.*` CI matrix entry. - Add a jest setup that gives axios-mock-adapter a normal-prototype copy of the headers, only when they arrive with a null prototype, leaving AxiosHeaders detection untouched on axios 1.x and 0.25/0.30 - Relax the two prototype-sensitive `toStrictEqual` assertions to `toEqual` Co-Authored-By: Claude Opus 5 (1M context) Co-authored-by: Claude --- jest.config.ts | 1 + src/__tests__/setup.ts | 18 ++++++++++++++++++ src/client.test.ts | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/setup.ts 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: {