Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
milung opened this issue Nov 20, 2023 · 6 comments

Comments

@milung
Copy link

milung commented Nov 20, 2023

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
Copy link

ankoehn commented Nov 22, 2023

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

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Nov 22, 2023

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
Copy link
Author

milung commented Nov 23, 2023

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 is undefined. 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
Copy link

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 #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
Copy link

Hi,

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

@nioe
Copy link

nioe commented May 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants