vue.js test:unit w test-utils & Jest : [vue-test-utils]: wrapper.destroy() can only be called on a Vue instance

时间:2018-09-05 16:24:13

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

In order to test a beforeDestroy() hook in my component, I wrote the following spec :

 it("should test lifecycle when audio tag is destroyed", () => {
    // jsdom doesn't support any loading or playback media operations. 
    // As a workaround you can add a few stubs in your test setup:
    window.HTMLMediaElement.prototype.removeEventListener = () => { /* do nothing */ };
    // given
    const wrapper = mount(AudioPlayer, {
     // attachToDocument: true,
      propsData: {
        autoPlay: false,
        file: file,
        ended,
        canPlay
      }
    });
    wrapper.vm.loaded = true; // enable buttons
    const player = wrapper.find("#player");
    expect(wrapper.contains('#playPauseBtn')).toBe(true);
    // when
    player.destroy()
    // then
    expect(wrapper.contains('#playPauseBtn')).toBe(false);
  });

but I am getting an error, even if the destroy() is used as in the doc ...

[vue-test-utils]: wrapper.destroy() can only be called on a Vue instance

  177 |     expect(wrapper.contains('#playPauseBtn')).toBe(true); // OK
  178 |     // when
> 179 |     player.destroy()

where am I wrong ?

thanks for feedback

1 个答案:

答案 0 :(得分:0)

ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 3, fill = "gray63") + annotate("rect", xmin = 21, xmax = 22, ymin = 0, ymax = Inf, fill = "gray18", alpha = 0.5) + ylim(0, 12) 返回DOM元素的包装,因此基本上是HTML。

destroy()销毁Vue组件实例。

您不能在“ HTML元素”上调用destroy函数。我相信您想写const player = wrapper.find("#player");