【发布时间】:2020-11-22 15:50:04
【问题描述】:
我正在尝试使用 Jest 创建一个单元测试。 我有一个函数可以检查 Outlook 需求集的版本,如果支持“集合”,它会做一些事情 - 在这种情况下,它正在关闭加载项。
if (Office.context.requirements.isSetSupported('Mailbox', '1.15') {...}
在单元测试中,我尝试模拟外部 isSetSupported() 调用值,但是在运行 getCloseAddin() 时出现以下错误:
Office.context.requirements.isSetSupported 不是函数
它无法识别我刚刚创建的模拟函数。 我还尝试更改 global.Office
的值global.Office = {
context: {
requirements: {
isSetSupported: {'Mailbox', '1.5'}
}
}
}; 但没有成功。
文件.js
const getCloseAddin = () => {
try {
const Office = getOffice();
if (Office.context.requirements.isSetSupported('Mailbox', '1.15')) {
return Office.context.ui.closeContainer();
}
} catch (e) {
console.error('Cannot close the add-in properly.');
return null;
}
};
file.test.js
it('verify we gets Undefined when working with supported version', async () => {
const mockedIsSetSupported = jest.fn();
mockedIsSetSupported.mockReturnValueOnce('Mailbox', '1.5');
global.Office = {
context: {
requirements: {
mockedIsSetSupported
}
}
};
const closeAddin = await getCloseAddin();
expect(closeAddin).toBeUndefined();
});
【问题讨论】:
标签: javascript jestjs outlook-addin outlook-web-addins