jsdom: scrollIntoView is not a function
I’m running a test using a library (grommet
) that uses scrollIntoView
on an a
tag. jsdom
is throwing errors when hitting that line of code. I was able to get it to work by adding window.HTMLElement.prototype.scrollIntoView = function() {};
in my testHelper file to setup the tests, but I thought I’d report it to see if it’s something you want to fix.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 62
- Comments: 19 (1 by maintainers)
Commits related to this issue
- Fix JSDOM Not Working With `scrollIntoView` Fix JSDOM is not working with `scrollIntoView` as a bug in JSDOM used for testing creates a DOM environment in memory so that the test can run against it. ... — committed to SpiritBreaker226/portfolio by SpiritBreaker226 2 years ago
- Fix JSDOM Not Working With `scrollIntoView` Fix JSDOM is not working with `scrollIntoView` as a bug in JSDOM used for testing creates a DOM environment in memory so that the test can run against it. ... — committed to SpiritBreaker226/portfolio by SpiritBreaker226 2 years ago
@QuantumInformation You can use this for the Jest:
Element.prototype.scrollIntoView = jest.fn();
Why not add the not-implemented-warning stub as suggested in the second comment?
Is there a reason why it has been done for
scrollTo
but notscrollIntoView
? https://github.com/jsdom/jsdom/blob/65ef06f3771a33dcbf7bef442fdc7e07816d008d/lib/jsdom/browser/Window.js#L931hello, you can use window.HTMLElement.prototype.scrollIntoView = jest.fn()
try this one… window.HTMLElement.prototype.scrollIntoView = function() {};
I need the same for Element.prototype.scroll = jest.fn()
@enessoylu and @msmetana where do you recommend putting
Element.prototype.scrollIntoView = jest.fn()
, in each .test.When using CRA there is a setupTest.ts, seems to work but idk
Anyone have an idea why it happens?
For
ava
or any other unit test runner you can usewindow.HTMLElement.prototype.scrollIntoView = () => {}
and set it intest.beforeEach()
or inside the test functionThanks! it worked.
For me it was enough to set:
Element.prototype.scrollIntoView` = () => {};
once into setupJest.ts
Yeah this might be worth adding a not-implemented-warning stub for. We can’t really implemented it since we don’t do layout.