@testing-library/dom window.getComputedStyle“未实现”错误即使在设置了计算样式支持PseudoElements:true

问题描述

我正在使用 jest 来测试实现 react embla carousel 的自定义模块。

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

Error: Not implemented: window.computedStyle(elt,pseudoElt)

经过一番搜索后,我发现 jsdom 不支持第二个参数,@testing-library 的优秀人员可以选择做...某事...关于它。

参考

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

jest-setup.js:

import { configure } from '@testing-library/dom';
configure({
  computedStyleSupportsPseudoElements: true
})
import '@testing-library/jest-dom';

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

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

解决方法

我在将 Jest 更新到最新版本 (26.6.3) 时遇到了同样的错误。 embla-carousel 代码中的 Calling getComputedStyle as a window property 给我造成了这个问题:

const pseudoString = window.getComputedStyle(node,':before').content

我也尝试使用 @testing-library/dom 解决此问题,但无济于事。但是当我删除窗口并直接调用 getComputedStyle 时,问题就消失了:

const pseudoString = getComputedStyle(node,':before').content

我已针对此问题发布了 embla-carousel v4.4.1,其中 includes a fix。请安装此版本,问题就会消失。