Jest 中的存根窗口函数 [英] Stubbing window functions in Jest

查看:20
本文介绍了Jest 中的存根窗口函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我在 window.confirm 提示点击OK"时触发回调,我想测试回调是否被触发.

In my code, I trigger a callback upon "OK" click of a window.confirm prompt, and I want to test that the callback is triggered.

sinon中,我可以通过以下方式存根window.confirm函数:

In sinon, I can stub the window.confirm function via:

const confirmStub = sinon.stub(window, 'confirm');
confirmStub.returns(true);

有没有办法在 Jest 中实现这种存根?

Is there a way I can achieve this stubbing in Jest?

推荐答案

开玩笑的,你可以使用 global 覆盖它们.

In jest you can just overwrite them using global.

global.confirm = () => true

就像开玩笑一样,每个测试文件都在自己的进程中运行,您不必重置设置.

As in jest every test file run in its own process you don't have to reset the settings.

这篇关于Jest 中的存根窗口函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆