Jest 测试给出错误:“无法读取未定义的属性

时间:2021-04-21 13:27:34

标签: javascript vue.js unit-testing jestjs

我正在编写一个简单的笑话测试,但收到以下错误:

[Vue warn]: Error in render: "TypeError: Cannot read property 'fields' of undefined"

[Vue warn]: Error in created hook: "TypeError: Cannot read property 'relationNumber' of undefined"

我的笑话测试:

import { mount } from "@vue/test-utils";
import Step2 from "pathtoStep2.vue";

describe("Step2", () => {
  it("should enable button", async () => {
    const wrapper = mount(Step2);
    await wrapper
      .find("[data-testid='relationnumber-input']")
      .setValue("12341234123412");
    const button = wrapper.find("[data-testid='next-button']");
    expect(button.toBeEnabled());
  });
});

我在 step2.vue 中的字段数据对象:

  data () {
    return {
      fields: {
        relationNumber: '',
      }
    }
  }

根据https://lmiller1990.github.io/vue-testing-handbook/simulating-user-input.html#creating-the-component 它不需要初始化数据对象?那么我做错了什么?

0 个答案:

没有答案