Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据函数调用参数从jest.spyOn返回不同的对象?_Javascript_Jestjs - Fatal编程技术网

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')
返回不同的值会发生什么?