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

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

问题描述

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

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

在jest中,每个测试文件都在自己的进程中运行,您不必重置设置。

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

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

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