Jsdom: scrollIntoView 不是函数

创建于 2016-12-28  ·  14评论  ·  资料来源: jsdom/jsdom

我正在使用在a标签上使用scrollIntoView的库( grommet )运行测试。 jsdom在点击该行代码时抛出错误。 通过在我的 testHelper 文件中添加window.HTMLElement.prototype.scrollIntoView = function() {};来设置测试,我能够让它工作,但我想我会报告它,看看它是否是你想要修复的东西。

css feature layout

最有用的评论

@QuantumInformation您可以将其用于 Jest:

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

所有14条评论

是的,这可能值得添加一个未实现的警告存根。 我们不能真正实现它,因为我们不做布局。

这有什么动静吗?

尝试在 Jest 测试中使用,我试图在元素上安装一个调用 scrollIntoView 的组件,但测试总是失败

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

@QuantumInformation您可以将其用于 Jest:

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

我到处都看到这个修复,但你是从哪里导入元素的? 我得到“元素未定义”我正在使用带有柴的酶。

您的 tsconfig 库中需要 Element:

IE
'dom'

@qmg-JamesT 是全局元素类,其他元素都继承自 scrollIntoView方法。

嗨,有没有不使用玩笑的更新?

你好,你可以使用window.HTMLElement.prototype.scrollIntoView = jest.fn()

我需要同样的 Element.prototype.scroll = jest.fn()

@enessoylu@msmetana您建议将Element.prototype.scrollIntoView = jest.fn()放在每个 .test 中的哪个位置。

使用 CRA 时有一个 setupTest.ts,似乎可以工作,但是 idk

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

Element.prototype.scroll = jest.fn()

对于ava或任何其他单元测试运行程序,您可以使用window.HTMLElement.prototype.scrollIntoView = () => {}并将其设置在test.beforeEach()或测试函数内

对我来说,设置就足够了:

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

一旦进入 setupJest.ts

scroll似乎也是这种情况

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

谢谢! 有效。

试试这个..
window.HTMLElement.prototype.scrollIntoView = function() {};

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

tolmasky picture tolmasky  ·  4评论

machineghost picture machineghost  ·  4评论

philipwalton picture philipwalton  ·  4评论

mitar picture mitar  ·  4评论

JacksonGariety picture JacksonGariety  ·  4评论