即使在设置computeStyleSupportsPseudoElements:true之后,@testing-library/dom window.getCompulatedStyle“未实现”错误

Ric*_*gen 4 javascript jsdom reactjs jestjs ts-jest

我正在使用 jest 来测试实现 React Embla 轮播的自定义模块。

我收到与 jsdom 缺少实现相关的错误window.getComputedStyle(elt, pseudoElt)

Error: Not implemented: window.computedStyle(elt, pseudoElt)
Run Code Online (Sandbox Code Playgroud)

经过一番搜索后,我发现 jsdom 不支持第二个参数,并且 jsdom 的优秀人员可以@testing-library选择对此做一些事情。

参考

https://testing-library.com/docs/dom-testing-library/api-configuration#compulatedstylesupportspseudoelements

笑话设置.js:

import { configure } from '@testing-library/dom';
configure({
  computedStyleSupportsPseudoElements: true
})
import '@testing-library/jest-dom';
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这似乎并不能解决我的错误。我知道正在应用配置,因为我设置了一些其他配置选项,这些选项破坏了我的所有测试。

我是否没有正确应用某些内容,或者是否有其他可能的解决方法?我在测试中不需要轮播的完整功能,我只想确保传递给视图的数据能够正确呈现。

Dav*_*vid 8

将 Jest 更新到最新版本 (26.6.3) 时,我遇到了同样的错误。在代码中调用 getCompulatedStyle作为窗口属性embla-carousel导致了我这个问题:

const pseudoString = window.getComputedStyle(node, ':before').content
Run Code Online (Sandbox Code Playgroud)

我也尝试使用 来解决这个问题,@testing-library/dom但没有成功。但是当我删除窗口并直接调用getCompulatedStyle时,问题就消失了:

const pseudoString = getComputedStyle(node, ':before').content
Run Code Online (Sandbox Code Playgroud)

我已经发布了embla-carousel v4.4.1,其中包含此问题的修复程序。请安装此版本,问题就会消失。