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

[Bug]: Jest does not properly resolve href attributes with relative URLs #11861

Closed
elias6 opened this issue Sep 9, 2021 · 5 comments
Closed

Comments

@elias6
Copy link

elias6 commented Sep 9, 2021

Version

27.1.1

Steps to reproduce

  1. Clone my repo at https://github.com/elias6/jestAHrefTest
  2. npm i
  3. npm run test
  4. You should see the bug.

Expected behavior

I expect the expect(link.href).toBe("https://example.com/"); line to pass.

Actual behavior

The line doesn't pass. I get this:

    Expected: "https://example.com/"
    Received: "http://localhost/"

Additional context

It seems that jest (or maybe jsdom) is not properly resolving the URL in the link's href attribute.

I tried setting the current URL using the top 3 answers to this question and none of them worked.

I also experience this bug in Jest 26.4.2.

Environment

System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 12.14.0 - ~/.nodenv/versions/12.14.0/bin/node
    Yarn: 1.21.1 - ~/.nodenv/versions/12.14.0/bin/yarn
    npm: 6.14.15 - ~/.nodenv/versions/12.14.0/bin/npm
  npmPackages:
    jest: ^27.1.1 => 27.1.1
@SimenB
Copy link
Member

SimenB commented Sep 10, 2021

Essentially jsdom/jsdom#1388 (comment), you cannot override window.location. You can use https://jestjs.io/docs/configuration#testurl-string

@SimenB SimenB closed this as completed Sep 10, 2021
@elias6
Copy link
Author

elias6 commented Sep 10, 2021

Hmm... right now I'm working around the problem by using a custom testEnvironment that puts the jsdom object in global, but I've been looking for a less awkward way to deal with this.

Is there any easy and practical way to make some tests use one testURL and other tests (in the same file) use a different one?

@SimenB
Copy link
Member

SimenB commented Sep 12, 2021

https://jestjs.io/docs/configuration#projects-arraystring--projectconfig with separate configs should work

@elias6
Copy link
Author

elias6 commented Sep 16, 2021

I looked at that projects thing, and it seems to be able to make all the tests in a given file use the same URL. But how can I set the URL to something different for each test or it call?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants