Vue-test-utils: shallowMount(Parent).contains(Child) returns false if Parent uses Child as a dynamic component (#853)

Created on 1 Aug 2018  路  3Comments  路  Source: vuejs/vue-test-utils

Version

1.0.0-beta.23

Reproduction link

https://gitlab/mygroup/myproject

Steps to reproduce

Parent.spec.js

import { shallowMount } from "@vue/test-utils";
import { expect } from "chai";
import { Parent, Child } from "@/components";

describe("Parent.vue", function() {

  it("contains Child", () => {
    expect(shallowMount(Parent).contains(Child)).to.be.true;
  });
});

Parent.vue:

<template>
  <Child />
</template>

<script>
export default {
  components: {
    Child: () => import("@/components/Child")
  }
}
</script>

What is expected?

The tests should pass (like they did in 1.0.0-beta.20).

What is actually happening?

  1) Parent.vue
       contains Child:

      AssertionError: expected false to be true
        expected - actual

      -false
       true

Most helpful comment

I believe this was working previously as a side effect of not being stubbed correctly. When we create the stubs, we don't have enough information to create a stub that can be identified with a component selector, like you have in your example.

You can acheive this functionality by using mount:

it('renders dynamic import', (done) => {
  const wrapper = mount(TestComponent)
  setTimeout(() => {
    expect(wrapper.find(Component).exists()).to.equal(true)
    done()
  })
})

Or by stubbing the component with the stubs option:

it('renders dynamic import', () => {
  const wrapper = mount(TestComponent, {
    AsyncComponent: Component
  })
  expect(wrapper.find(Component).exists()).to.equal(true)
})

Unfortunately, I don't think we'll be able to achieve the auto stubbing that you expected.

All 3 comments

This is a follow-up of #853 (fixed in #864).

I believe this was working previously as a side effect of not being stubbed correctly. When we create the stubs, we don't have enough information to create a stub that can be identified with a component selector, like you have in your example.

You can acheive this functionality by using mount:

it('renders dynamic import', (done) => {
  const wrapper = mount(TestComponent)
  setTimeout(() => {
    expect(wrapper.find(Component).exists()).to.equal(true)
    done()
  })
})

Or by stubbing the component with the stubs option:

it('renders dynamic import', () => {
  const wrapper = mount(TestComponent, {
    AsyncComponent: Component
  })
  expect(wrapper.find(Component).exists()).to.equal(true)
})

Unfortunately, I don't think we'll be able to achieve the auto stubbing that you expected.

Thank you for the explanation, which totally makes sense, and the solutions. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robcresswell picture robcresswell  路  3Comments

vwxyutarooo picture vwxyutarooo  路  3Comments

maerteijn picture maerteijn  路  3Comments

eddyerburgh picture eddyerburgh  路  4Comments

matt-sanders picture matt-sanders  路  3Comments