Javascript 如何根据函数调用参数从jest.spyOn返回不同的对象?
目前我有这段代码用于单元测试Javascript 如何根据函数调用参数从jest.spyOn返回不同的对象?,javascript,jestjs,Javascript,Jestjs,目前我有这段代码用于单元测试 mockWindow = { document: { createElement: jest.fn().mockReturnValue(div), head: { appendChild: jest.fn(), }, body: { appendChild: jest.fn(), }, removeEventListener: jest.fn(), }, addEventListe
mockWindow = {
document: {
createElement: jest.fn().mockReturnValue(div),
head: {
appendChild: jest.fn(),
},
body: {
appendChild: jest.fn(),
},
removeEventListener: jest.fn(),
},
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
};
jest.spyOn(window, 'open').mockReturnValue(mockWindow);
现在,我正在向窗口添加另一个调用。使用不同的参数打开到我的代码库:
window.open('', 'new_target', 'width=1000,height=500')
我希望在调用第二个窗口.open
时能够返回不同的值。我是否可以要求spyOn根据参数返回不同的值
我尝试了jest.spyOn(窗口,'open','new_target')
但是我得到了这个错误错误:(47,5)TS2554:预期2-3个参数,但是得到了4个。
。第三个参数应该指定访问类型(即get vs set)如果多次使用.mockReturnValueOnce('x')
返回不同的值会发生什么?