【发布时间】:2021-01-16 00:30:59
【问题描述】:
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。
目前我必须在每个测试文件中都有这个beforeAll:
import matchMediaPolyfill from 'mq-polyfill';
beforeAll(() => {
matchMediaPolyfill(window)
window.resizeTo = function resizeTo(width, height) {
Object.assign(this, {
innerWidth: width,
innerHeight: height,
outerWidth: width,
outerHeight: height,
}).dispatchEvent(new this.Event('resize'))
}
})
我是这样使用的:
it('does something at small screen sizes', async () => {
window.resizeTo(320, 800);
// actual test here
我可以有一个全局的beforeAll 来应用于我项目中的每个测试文件吗?
文档提到globalsetup:
https://jestjs.io/docs/en/configuration#globalsetup-string
但是它会出错,因为它无法识别 beforeAll 函数。在我看来,该选项用于设置环境但不向测试文件添加内容?
【问题讨论】:
标签: jestjs