【发布时间】:2021-03-15 02:41:55
【问题描述】:
尝试测试我使用通知 API 的代码。
我认为我没有正确地嘲笑它。
global.Notification = ({
requestPermission: jest.fn().mockImplementation(()=> {
console.log('reached here') //never logged
return 'denied';
}),
permission: 'denied',
} as unknown) as jest.Mocked<typeof Notification>;
test('should ask for permission from Notifications API ', async () => {
expect(global.Notification.requestPermission).toBeCalledTimes(1); // WORKS although console.log above never worked
});
这行得通。
但以下不是..
test('should create a new notification ', async () => {
// some code that eventually runs this
new Notification("Test");
expect(global.Notification).toBeCalledTimes(1); // TypeError: Notification is not a constructor
});
});
【问题讨论】:
标签: javascript typescript jestjs web-notifications