我怎么能用jest模仿window.open呢?我尝试了几个选项,但每一个都失败了
我需要检查是否调用了window.open,以及是否应该使用某些参数来调用它
假设我有这样的东西
open(): void {
window.open('/link');
}发布于 2019-11-06 20:32:53
安装以下软件包:
jest-environment-jsdom
jest-environment-jsdom-global在您的jest配置中添加"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://stackoverflow.com/questions/58726275
复制相似问题