Jsdom: scrollIntoView is not a function

Created on 28 Dec 2016  ·  14Comments  ·  Source: jsdom/jsdom

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.

css feature layout

Most helpful comment

@QuantumInformation You can use this for the Jest:

Element.prototype.scrollIntoView = jest.fn();

All 14 comments

Yeah this might be worth adding a not-implemented-warning stub for. We can't really implemented it since we don't do layout.

Any movement on this?

Trying to use in a Jest test, I'm trying to mount a component that calls scrollIntoView on an element, and the test always fails with

TypeError: node.scrollIntoView is not a function
        at Object.scrollInToView src/component/shared/foo.tsx:7
        at User.Object.<anonymous>.User.componentDidMount

@QuantumInformation You can use this for the Jest:

Element.prototype.scrollIntoView = jest.fn();

I see this fix everywhere, but were are you importing Element from? I get 'Element is not defined' I am using enzyme with chai.

You need Element in your tsconfig libs:

ie
'dom'

@qmg-JamesT It is the global element class where the other elements are inherited scrollIntoView method from.

Hi, is there any update on this without using jest?

hello, you can use window.HTMLElement.prototype.scrollIntoView = jest.fn()

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

import '@testing-library/jest-dom/extend-expect'

Element.prototype.scroll = jest.fn()

For ava or any other unit test runner you can use window.HTMLElement.prototype.scrollIntoView = () => {} and set it in test.beforeEach() or inside the test function

For me it was enough to set:

Element.prototype.scrollIntoView` = () => {};

once into setupJest.ts

This also seems to be the case with scroll

Element.prototype.scrollIntoView = jest.fn();

Thanks! it worked.

try this one..
window.HTMLElement.prototype.scrollIntoView = function() {};

Was this page helpful?
0 / 5 - 0 ratings