前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Karma单元测试reload异常处理

Karma单元测试reload异常处理

原创
作者头像
DamonLiu
发布2023-02-10 19:47:14
3990
发布2023-02-10 19:47:14
举报
文章被收录于专栏:知识技能知识技能

业务逻辑中存在

代码语言:javascript
复制
window.location.reload();

运行测试用例,会出现

的错误,导致测试用例无法完整进行。

可以使用sinon来解决这个问题

代码语言:javascript
复制
window.onbeforeunload = sinon.spy();

但是如果,你需要在一个模块的测试用例跑完之后,刷新页面进行下一个测试用例,就不用使用这种方法,这时需要屏蔽代码中的刷新页面逻辑。参考官方文档

4. For all other cases, the Swiss army knife /* istanbul ignore next */ may be used which skips the "next thing" in the source code 5. The "next" thing may be, among other things: A JS statement (including assignments, ifs, loops, switches, functions) in which case all of the statement is ignored for all forms of coverage. A switch case statement, in which case the particular case is ignored for branch coverage and its contents ignored for all forms A conditional inside a ternary expression in which case the branch is ignored A part of a logical expression in which case that part of the expression is ignored for branch coverage

那么直接用

代码语言:javascript
复制
/* istanbul ignore next */
window.location.reload();

进行屏蔽,发现并不能解决问题。仔细读文档,(including assignments, ifs, loops, switches, functions) ,只能屏蔽赋值、ifs、循环、开关、函数。所以封装一个函数

代码语言:javascript
复制
/* istanbul ignore next */
export const locationReload = () => {
    /* istanbul ignore next */
    window.location.reload();
};

这样子就可以屏蔽刷新页面

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档