Javascript 笑话模拟第三方对象

Javascript 笑话模拟第三方对象,javascript,unit-testing,mocking,jestjs,Javascript,Unit Testing,Mocking,Jestjs,我需要一些关于测试第三方对象的帮助。下面是我的代码 //app.js export const specialFunction = (offer) => { adobe.target.applyOffer({ mbox: 'container', offer }) } adobe.target.getOffer({ mbox: 'container', success: (offer) => { specialFuncti

我需要一些关于测试第三方对象的帮助。下面是我的代码

//app.js
export const specialFunction = (offer) => {
   adobe.target.applyOffer({
       mbox: 'container',
       offer
   })
}


adobe.target.getOffer({
  mbox: 'container',
  success: (offer) => {
     specialFunction(offer);
  }
})
在我的测试文件中

//app.test.js
import { specialFunction } from './app';

beforeAll(() => {
  const adobe = {
     target: {
       getOffer: jest.fn(),
       applyOffer: jest.fn()
     }
  }
  window.adobe = adobe;
});
it('test function', () => {
    specialFunction({foo: 'bar'});
    expect(adobe.target.applyOffer).toHaveBeenCalledWith({
        mbox: 'container',
        offer: {foo: 'bar'}
    });
})
但是当我开始运行它时,
app.js
总是报告
ReferenceError:未定义adobe
但是如果我将
app.js
更改为

typeof adobe !== 'undefined' && adobe.target.getOffer({
      mbox: 'container',
      success: (offer) => {
         specialFunction(offer);
      }
    })
然后测试通过,上面的
adobe.target.getOffer
未测试 所以我的问题是,如何测试
adobe.target.getOffer
part?还有为什么考试会通过?似乎
window.adobe=adobe
正在处理测试用例

,以便将(模拟)方法添加到全局范围,您可以在测试运行之前将它们附加到该范围。比如:

beforeAll(() => {
  const adobe = {
    target: {
       getOffer: jest.fn()
    }
  }
  global.adobe = adobe;
})

您好,您确定加载.js库时没有错误吗?@DacreDenny
窗口。adobe
是在其他地方定义的,因此我可以直接使用
adobe
,而不会出现任何错误这可能对您有用,-在您的情况下,而不是使用此技术模拟
本地存储
,你可以用它来模仿adobe,不幸的是它不起作用。我将更新建议的代码后,不幸的是,它不工作。我已经用更多的细节更新了我的代码。谢谢