【问题标题】:TypeError: Cannot read property 'innerHTML' of null | Vue Utils - Jest Framework | Can't able to run test case for thisTypeError:无法读取 null 的属性“innerHTML”| Vue Utils - Jest 框架 |无法为此运行测试用例
【发布时间】:2020-06-02 06:42:53
【问题描述】:

我正在使用 Vue utils jest 框架来覆盖我的 vue.js。像这样运行其显示的错误。请参考屏幕截图。

我的vue代码如下

  <div id="tile" class="tile" data-scale="2.4" @mouseover="cursorEnters" @mouseout="cursorLeaves" @mousemove="cursorMove($event)" :data-image="imageUrl"
  >

我的js代码如下

mounted() {
   this.initializationImageZoom();
},    
initializationImageZoom() {
  const tile = document.querySelector('#tile');
  tile.innerHTML += '<div id="photo" class="photo"></div>';
  tile.children[0].style.backgroundImage = 'url(' + this.imageUrl + ')';
},

在我的测试用例下面

import { shallowMount } from '@vue/test-utils';

const imageUrl = "https://i.ytimg.com/vi/ETsekJKsr3M/maxresdefault.jpg";

let wrapper;
const ImageZoom = require('./ImageZoom.vue').default;

beforeEach(() => {
  wrapper = shallowMount(ImageZoom, {
    propsData: {
      imageUrl,
    }
  });
});

afterEach(() => {
  wrapper.destroy();
});

describe('NoRecords.vue', () => {
  it('should have props (imageUrl) type as Object', () => {
    expect(typeof wrapper.vm.imageUrl).toBe('string');
 });
});

在通过测试用例渲染 vue 组件时。它显示的错误是这样的(参考屏幕截图)

【问题讨论】:

  • @tony19 我已经更新了问题和我的 vue 代码。请参考并帮助我
  • 您可以添加您正在为此功能编写的测试用例吗?
  • @NipunJain 我已经更新了我的测试用例。如果我开始渲染会发生此错误。(请参阅屏幕截图)

标签: unit-testing vue.js vuejs2 jestjs testcase


【解决方案1】:

你可以试试这个:

import { shallowMount } from '@vue/test-utils';

const imageUrl = "https://i.ytimg.com/vi/ETsekJKsr3M/maxresdefault.jpg";

// Creating #tile div

let tile = document.createElement('div')
tile.setAttribute("id", "tile")
let children = document.createElement('div')
tile.appendChild(children)
document.body.appendChild(tile)

let wrapper;
const ImageZoom = require('./ImageZoom.vue').default;

beforeEach(() => {
  wrapper = shallowMount(ImageZoom, {
    propsData: {
      imageUrl,
    }
  });
});

afterEach(() => {
  wrapper.destroy();
});

describe('NoRecords.vue', () => {
  it('should have props (imageUrl) type as Object', () => {
    expect(typeof wrapper.vm.imageUrl).toBe('string');
 });
});

我只是在 body 中创建一个 div 元素,这样玩笑就不会抛出错误。

【讨论】:

  • 它对我有用。但这不是正确的方法。需要解决确切的场景。一些我从你身边得到的想法。无论如何谢谢你
猜你喜欢
  • 2019-08-09
  • 2021-08-17
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2020-08-08
  • 2022-12-20
  • 2020-01-24
  • 2020-12-08
相关资源
最近更新 更多