开玩笑嘲笑window.open

我如何用玩笑来嘲笑window.open? 我尝试了几种选择,但每一种都失败了

我需要检查是否调用了window.open,并且应使用某些参数来调用

假设我有这样的东西

open(): void {
  window.open('/link');
}
ydp1016 回答:开玩笑嘲笑window.open

安装这些软件包:

jest-environment-jsdom
jest-environment-jsdom-global

在玩笑的配置中添加"testEnvironment": "jest-environment-jsdom-global"

假设您具有这样的功能:

open() {
  window.open("abc");
}

然后在测试文件中:

it("should open the url in window",() => {
  const openSpy =  jest.spyOn(window,"open");

  open();

  expect(openSpy).toHaveBeenCalledTimes(1);
  expect(openSpy).toHaveBeenCalledWith("abc");
});
本文链接:https://www.f2er.com/3153780.html

大家都在问