Skip to content

Conversation

@marchaos
Copy link

@marchaos marchaos commented Dec 16, 2025

Problem

When tests mock window.location with a partial mock (e.g., jest.spyOn(window, 'location', 'get').mockReturnValue({ reload: jest.fn() })), the BrowserFrameURL.getRelativeURL method fails because location.href is undefined from the mock.

This causes:

  1. URL resolution falls back to about:blank (which has origin: null)
  2. Origin comparison in History.pushState\ fails: null !== 'http://localhost'`
  3. SecurityError thrown, history library tries fallback to window.location.assign()
  4. Mock doesn't have assign, causing confusing errors

Solution

Use frame.window[PropertySymbol.location].href instead of frame.window.location.href. This accesses the internal Location object via Symbol property, which isn't affected by mocks on the public getter.

Testing

Tested against a large React test suite (~26,000 tests) that was failing with this issue. After the fix, tests pass successfully.

… mock interference

When tests mock window.location with a partial mock (e.g., only providing
`reload`), the getRelativeURL method would fail because `location.href`
was undefined from the mock. This caused the URL resolution to fall back to
'about:blank', which has `origin: null`.

The origin mismatch then triggered a SecurityError in History.pushState,
which caused the `history` library to fall back to window.location.assign(),
which wasn't available in the mock - resulting in confusing errors.

By using the internal PropertySymbol.location instead of the public getter,
BrowserFrameURL now accesses the real Location object regardless of any
mocks applied to window.location.
@marchaos marchaos force-pushed the fix/browser-frame-url-mock-resilience branch 2 times, most recently from c2d9fc2 to d24c667 Compare December 21, 2025 09:04
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

Successfully merging this pull request may close these issues.

1 participant