axios / axios

Promise based HTTP client for the browser and node.js
https://axios-http.com
MIT License
104.66k stars 10.78k forks source link

Breaking change in 1.6.1 release does not allow to create axios instance in tests #6093

Open milung opened 7 months ago

milung commented 7 months ago

Describe the bug

When testing the application and trying to mock the axios (e.g. axios-mock-adapter, but generally any axios.create() call) I get error "TypeError: Invalid URL", which is caused by the fact that the change of the 1.6.1 of the function hasStandardBrowserEnv is missing the original conditions typeof window !== 'undefined' && typeof document !== 'undefined'; in the function isStandardBrowserEnv. That leads to the attempt to create and read url path from the document location, which is eventually not initialized yet.

To Reproduce

In my case I find it with stencil:

npm init stencil
npm i axios-mock-adapter
npm i

in file .../src/components/my-component/my-component.spec.ts add

import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const mockAdapter = new MockAdapter(axios);

Code snippet

import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const mockAdapter = new MockAdapter(axios);

Expected behavior

Either to have possibility to enforce "nonstandardBrowser" or to preserve behaviour of version 1.6.0

Axios Version

1.6.1

Adapter Version

No response

Browser

puppeteer

Browser Version

20,2,0

Node.js Version

21.1.1

OS

Windows 11

Additional Library Versions

stencil 4.7.2

Additional context/Screenshots

No response

ankoehn commented 7 months ago

I have the same issue with the version 1.6.2. Is there any work around?

DigitalBrainJS commented 7 months ago

the function hasStandardBrowserEnv is missing the original conditions typeof window !== 'undefined' && typeof document !== 'undefined';

This code is still there but refactored as an external constant

milung commented 7 months ago

This code is still there but refactored as an external constant

@DigitalBrainJS - yes you right. After further investigation I found the possible suspect - in 1.6.0 the platform.isStandardBrowser isundefined. The reason is that the constants were defined and exported from lib/platform/browser/index.js but this module was not imported into helpers/isSameURLOrigin.js. Its imports states:

import utils from './../utils.js';
import platform from '../platform/index.js';

and `platform/index.js imports are

import platform from './node/index.js';

export {platform as default}

Not sure if that was originally by intention, but by moving the code into the lib/platform/common/utils.js in 1.6.1 change, the constants get now evaluated in the test environment, therefore the breaking change.

ashnewport commented 7 months ago

I'm experiencing the same issue. Attempting to upgrade from v0.x mostly to fix an open vulnerability with axios in that major version as noted here https://github.com/axios/axios/pull/6091 however this issue making tests unable to use axios has become blocking. It's not possible to get a version of axios with the vulnerability fix AND tests still executing.

satyapendem commented 3 months ago

Hi,

Any resolution for this? I was facing with axios-mock-adapter after upgrading my axios version. image

nioe commented 1 month ago

Any news on this issue? It currently blocks us from updating, since all our tests which include Axios are failing...