我想在我的测试中模拟navigator.userAgentData.getHighEntropyValues的解析值,作为一个示例对象。
我该怎么做?
我试过:
beforeEach(() => {
jest.mock(global.navigator.userAgentData);
});
it('uaFullVersion is defined', async () => {
global.navigator.userAgentData.getHighEntropyValues.mockResolvedValueOnce({uaFullVersion: '1.2.3'});
const hev = await myFunc();
expect(hev.uaFullVersion).toBeDefined();
});myFunc:
async function myFunc() {
const hev = await navigator.userAgentData.getHighEntropyValues(["uaFullVersion"]);
return hev;
}但是我得到了错误TypeError: Cannot read properties of undefined (reading 'getHighEntropyValues')
发布于 2022-10-01 05:22:20
您在beforeEach上尝试使用jest.mock而不是jest.mock吗?
像这样:
jest.spyOn(global.navigator.userAgentData,
'getHighEntropyValues').mockResolvedValueOnce({})https://stackoverflow.com/questions/73915347
复制相似问题