我需要帮助测试第三方对象。以下是我的代码:
//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
部分?另外,为什么测试会通过?看起来window.adobe = adobe
在测试用例中起作用。
at.js
库被正确加载了吗? - Dacre Dennywindow.adobe
在其他地方已经定义,所以我可以直接使用adobe
而不会出现任何错误。 - peace and love