【发布时间】:2020-06-03 16:02:56
【问题描述】:
我已经使用 screen.height & screen.width 进行 js 操作。我无法通过测试用例。 (代码覆盖率)。我正在使用 vue utils - Jest 框架。 下面我的js代码
const clientWidth = this.clientWidth ? this.clientWidth : screen.width;
const clientHeight = this.clientHeight ? this.clientHeight : screen.height;
低于我在测试用例中尝试过的内容
it('should set the height of window screen', () =>{
Object.defineProperty(HTMLElement.prototype, 'screen', {
height: {
value: 100,
}
configurable: true,
});
如何为这个场景编写测试用例。
【问题讨论】:
-
你测试什么代码?如果是 window.screen,那么在 window 上模拟它会更有意义。
-
@EstusFlask 它不是 window.screen
-
@EstusFlask 它不是全局屏幕。我已经更新了我的 js 代码。请仔细查看
-
@EstusFlask 其实我是 js 新手。我在控制台中尝试了 screen.height 。它显示了一些价值。当我尝试 window.screen.height 时,它也返回相同的值。
-
我明白了。你试图在 HTMLElement 上模拟它让我很困惑。我会发布一个答案。
标签: unit-testing vue.js vuejs2 jestjs vue-test-utils