【问题标题】:How to write test case for undefined window Object | Vue test utils | Jest Framework如何为未定义的窗口对象编写测试用例 | Vue 测试工具 |笑话框架
【发布时间】:2020-06-04 21:14:37
【问题描述】:

我正在使用 vue test utils - Jest 框架对我的 vue 文件进行单元测试。我无法测试以下行。

const inBrowser = typeof window !== 'undefined';

我的问题是如何将 JavaScript Window 设置为未定义。有没有办法将窗口设置为未定义的浏览器对象模型(BOM)?

【问题讨论】:

标签: javascript unit-testing vue.js jestjs vue-test-utils


【解决方案1】:

基于thisthis,您可以使用.spyOn 函数并将窗口设置为未定义:

let windowSpy;

beforeEach(() => {
  windowSpy = jest.spyOn(global, 'window', 'get');
  windowSpy.mockImplementation(() => undefined);
});

it('window should be undefined.', () => {
  expect(window).toBeUndefined();
});

【讨论】:

  • 我收到了这样的错误。你能帮我解决这个`不能窥探窗口属性;它不是一个函数 `
猜你喜欢
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 2020-12-15
  • 1970-01-01
  • 2012-01-26
  • 2014-11-17
  • 1970-01-01
相关资源
最近更新 更多