-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
25.1.0 issue with window.location.reload giving '...Cannot assign to read only property...' #9471
Comments
Note that |
This matches browser behavior. You can try opening up an issue with JSDOM, but I doubt they'll diverge from browser behavior. I recommend instead of using the browser global you create some sort of abstraction. export function reload() {
window.location.reload()
} Then you can mock that module and replace Closing as this is working as intended and I don't think we can do anything about it in Jest. Feel free to continue discussing, though! |
Courtesy of my colleague @jchiam, this allows me to mock
It might work for the other properties in |
What worked for me is: Object.defineProperty(window, 'location', {
writable: true,
value: { assign: jest.fn() }
})
Object.defineProperty(window.location, 'reload', {
writable: true,
value: { assign: jest.fn() }
}) |
I don’t understand why restoring the old function. I always do : // GIVEN a mocked location
delete window.location
window.location = {
reload: jest.fn()
}
// WHEN the method is called
method()
// THEN the page should have been reload
expect(window.location.reload).toHaveBeenCalledTimes(1) In test environment, you don’t need to restore original window methods behaviour. |
When trying this:
I received the error:
|
What worked for me is:
|
The error was "TypeError: Cannot assign to read only property 'reload' of object '[object Location]'" This is related to this issue jestjs/jest#9471 It is a workaround to fix the yarn test failure by mocking out window.location This was caused when upgrading jest to version 26
The error was "TypeError: Cannot assign to read only property 'reload' of object '[object Location]'" This is related to this issue jestjs/jest#9471 It is a workaround to fix the yarn test failure by mocking out window.location This was caused when upgrading jest to version 26
The error was "TypeError: Cannot assign to read only property 'reload' of object '[object Location]'" This is related to this issue jestjs/jest#9471 It is a workaround to fix the yarn test failure by mocking out window.location This was caused when upgrading jest to version 26
Maybe helpful to some. The above examples will work if you execute them in a {
....
"setupFilesAfterEnv": [
"<rootDir>/config/jest/setupAfterEnv.js"
],
....
} |
You have to restore it if a possible next test uses the variable, otherwise, the mocked value could bias on the result. |
Just fyi for anyone landing here after google, using object assign stopped working, but this does (and it's a much simpler way of expressing it)
|
208: Update jest to remove node-notifier vulnerability r=bidoubiwa a=bidoubiwa After update tests were failing until this hero solved it: jestjs/jest#9471 fixes #203 Co-authored-by: Charlotte Vermandel <charlottevermandel@gmail.com>
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. |
🐛 Bug Report
TypeError: Cannot assign to read only property 'reload' of object '[object Location]'
Reloading a page using Jest mocking is causing an issue with jest version 25.1.0
To Reproduce
The text was updated successfully, but these errors were encountered: