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

Allow setting the base URL #868

Closed
akaltar opened this issue Apr 17, 2023 · 9 comments · Fixed by #1019
Closed

Allow setting the base URL #868

akaltar opened this issue Apr 17, 2023 · 9 comments · Fixed by #1019
Labels
enhancement New feature or request

Comments

@akaltar
Copy link

akaltar commented Apr 17, 2023

Is your feature request related to a problem? Please describe.
When trying to migrate from jest-environment-jsdom I'm experiencing lots of issues around about:blank being the base URL because I'm trying to construct relative URLs

Describe the solution you'd like
Allow configuring the base URL in the base package + maybe change default for jest environment to match jsdom

Describe alternatives you've considered
I'm just trying happy-dom out, but I'm not sure how could I override this without a feature in my test setup. Adding the base URL everywhere in this big project feels a bit too difficult

Additional context
Very closely related to discussion in #678

@akaltar akaltar added the enhancement New feature or request label Apr 17, 2023
@kalvenschraut
Copy link
Contributor

kalvenschraut commented Apr 19, 2023

@capricorn86 I was able to set the base URL via location.assign('https://localhost') which I do in a "setup" file that is ran by vitest before each test suite file. Not sure if that is good enough for you @akaltar? Otherwise are you looking for happy-dom to read from some kind of configuration?

https://vitest.dev/config/#environment, I only know about vitest as my runner but looks like vitest could maybe have the environmentOptions, I assume whatever runner would have similar abilities.

@akaltar
Copy link
Author

akaltar commented Apr 21, 2023

This does seem to work for me, thank you @kalvenschraut!
I'll keep this discussion open in case it might help with easier migration from jsdom, if a default value will be considered in the future

@X-Y
Copy link

X-Y commented Apr 24, 2023

I encountered the same problem when trying to setup happy-dom with MSW. I'd vote for using http://localhost as default, to be consistent with js-dom.

@capricorn86
Copy link
Owner

capricorn86 commented Apr 29, 2023

@X-Y I believe that JSDom also defaults to "about:blank", but the test framework probably sets it to "http://localhost" in that case.

Defaulting to "http://localhost" can potentially cause security issues as it will affect functionality such as CORS and cookies.

JSDom source code:
https://github.com/jsdom/jsdom/blob/master/lib/api.js#L204

@silvenon
Copy link

The reason why MSW throws is because it's using location.origin, if it finds one, for mocking endpoints, and the default location.origin in happy-dom is null, which is an invalid second parameter for the new URL() constructor, which is what MSW uses.

If the default location.origin was undefined that would have been fine. So I'm not sure what should be done about this, whether it's a MSW issue or a happy-dom issue.

@capricorn86
Copy link
Owner

capricorn86 commented Aug 7, 2023

@silvenon location.origin is set to "null" (as a string) in Chrome and FF in a page with "about:blank" as URL.

The simplest solution would probably be if Vitest sets the URL to "https://localhost" or similar. In Vitest we at least know that we are simulating a local environment. It doesn't feel right to me if Happy DOM sets a default URL.

@capricorn86
Copy link
Owner

capricorn86 commented Aug 17, 2023

This has now been fixed in v0.34.2 version of Vitest.

Vitest pull request:
vitest-dev/vitest#3972

@capricorn86
Copy link
Owner

capricorn86 commented Aug 17, 2023

Sorry, I closed this a bit too quick. The issue is about Jest and not Vitest.

I believe that I misunderstood what the original issue was about. I will make a fix now that sets the base URL for the Jest environment.

capricorn86 added a commit that referenced this issue Aug 17, 2023
…base-url

#868@patch: Sets default URL for @happy-dom/jest-environment to "http…
@capricorn86
Copy link
Owner

capricorn86 commented Aug 17, 2023

@akaltar sorry for misunderstanding the original issue. There is now a fix for @happy-dom/jest-environment which sets the default URL to "http://localhost".

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v10.10.2

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

Successfully merging a pull request may close this issue.

5 participants