对包含带有槽的 Vuetify 数据表的 Vue 组件进行单元测试

sp0*_*0gg 6 javascript ecmascript-6 vue.js jestjs vuetify.js

我正在尝试使用嵌套组件对一个简单组件进行单元测试v-data-table。该页面在浏览器中正确呈现,但我似乎无法编写有效的 Jest 测试。

问题似乎出在我用于插槽的模板上——我直接从文档中提取了该模板。

当我用属性注释掉模板时v-slot,测试执行得很好。

People.vue:

<template>
  <v-data-table
    :headers="headers"
    :items="people"
    disable-pagination
    disable-sort
    disable-filtering
    hide-default-footer
    :loading="!people"
  >
    <template v-slot:item.id="{ item }">
      <v-icon>
        mdi-link-variant
      </v-icon>
      <router-link
        :to="{ name: 'assignments', query: { assignee_id: item.id } }"
        >Link</router-link
      >
    </template>
  </v-data-table>
</template>

<script>
export default {
  name: "People",
  data: () => ({
    headers: [
      {
        text: "Name",
        value: "first_name"
      },
      {
        text: "Assignment link",
        value: "id"
      }
    ]
  }),
  props: {
    people: {
      type: Array,
      required: true
    }
  }
};
</script>
Run Code Online (Sandbox Code Playgroud)

People.spec.js:

import { shallowMount } from "@vue/test-utils";
import People from "@/components/People.vue";

function getMountedComponent(Component, propsData) {
  return shallowMount(Component, { propsData });
}

const propsData = {
  people: [{ id: 1 }]
};
describe("headers", () => {
  it("contains name and assignment", () => {
    expect(getMountedComponent(People, propsData).vm.headers).toEqual([
      {
        text: "Name",
        value: "first_name"
      },
      {
        text: "Assignment link",
        value: "id"
      }
    ]);
  });
});
Run Code Online (Sandbox Code Playgroud)

错误信息:

  console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
    [Vue warn]: Error in render: "TypeError: Cannot read property 'item' of undefined"

    found in

    ---> <VDataTable>
           <People>
             <Root>

  console.error node_modules/vue/dist/vue.runtime.common.dev.js:1884
    TypeError: Cannot read property 'item' of undefined
Run Code Online (Sandbox Code Playgroud)

Rya*_*ing 6

我遇到了同样的问题,发现如果你将 包裹v-data-table在 a 中div,测试就会成功。由于某种原因,当是模板的根元素shallowMount时,Jest 会失败。v-data-table