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]: toHaveBeenCalledWith matcher matches with any URL object #15032

Open
yatki opened this issue Apr 19, 2024 · 2 comments
Open

[Bug]: toHaveBeenCalledWith matcher matches with any URL object #15032

yatki opened this issue Apr 19, 2024 · 2 comments

Comments

@yatki
Copy link

yatki commented Apr 19, 2024

Version

29.7.0

Steps to reproduce

Please run the test below

it('toHaveBeenCalledWith not working with URL object', () => {
    const spy = jest.fn()

    spy(new URL('http://demo'))

    expect(spy).toHaveBeenCalledWith(new URL('http://demo'))
    expect(spy).toHaveBeenCalledWith(new URL('http://demo2'))
    expect(spy).toHaveBeenCalledWith(new URL('http://demo3'))
  })

Expected behavior

I either expect the last 2 assertions to fail or since the object references are different, I expect all assertions to fail.

Actual behavior

But all assertions pass.

Additional context

No response

Environment

MacOS 14.4.1
@r2omvavra
Copy link

We can see the same behavior when trying to compare URL objects, with toEqual (which is used by toHaveBeenCalledWith)

it("toEqual not working with URL object", () => {
    expect(new URL("http://demo")).toEqual(new URL("http://demo2"));
});

according to the documentation .toEqual should use Object.is, which can differentiate between two URLs though

Object.is(new URL("http://demo"), new URL("http://demo2"));
> false

@yatki
Copy link
Author

yatki commented May 13, 2024

Also based on this line, URL object would be compared based on href attribute but seems like it's not working like that neither.

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

No branches or pull requests

2 participants