gpt4 book ai didi

unit-testing - 如何使用 JEST 框架模拟 chrome API?

转载 作者:行者123 更新时间:2023-12-03 17:32:32 24 4
gpt4 key购买 nike

我想使用 jest 框架来模拟 chrome api。
例如我使用 chrome.browserAction.setIcon向后台脚本发送消息。我如何模拟 chrome.browserAction.setIcon或任何其他 API,例如 chrome.runtime.sendMessage测试该方法是否已被调用?

我曾尝试使用 jest.spyOn() 来测试该方法是否已被调用。

这是测试方法

 test("mock testing chrome",()=>{
spyOn(chrome,
chrome.browserAction.setIcon);
content.chromemocktest();
expect(spy).toHaveBeenCalledWith({path:"/images.png"});
});

我正在测试的方法是:
 chromemocktest: function(){
chrome.browserAction.setIcon({path:"/image.png"});

}

当我运行 npm run test对于运行测试用例,它会抛出如下所示的错误。
 <spyOn> : stub() method does not exist
Usage: spyOn(<object>, <methodName>)

最佳答案

看看模块@bumble/jest-chrome .我刚刚在我的 TS 项目中使用了它,就像一个魅力,而且 super 好用。

关于unit-testing - 如何使用 JEST 框架模拟 chrome API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51286009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com