用于Jest测试的可重用const

时间:2019-10-11 15:01:29

标签: vue.js jestjs

我正在尝试声明const以便在许多测试中重新使用。

例如:

describe('Component.vue', () => {

    const householdData = [ "here", "is", "some", "data" ]


    it('does stuff', () => {
        const wrapper = mount(HouseholdsComponent, {
            propsData: {
                original_household: householdData,
            }
        });

        expect(original_household).toContain("here");
    })

    it('does stuff', () => {
        const wrapper = mount(HouseholdsComponent, {
            propsData: {
                original_household: householdData,
            }
        });

        expect(original_household).toContain("is");
    })
});

问题在于householdData似乎没有被设置。

当我console.log householdData时,我得到了:

{ clients: [Getter/Setter], networth: [Getter/Setter] }

我也尝试过在组件内设置数据,如下所示:

wrapper.vm.someVariable = householdData

这也给了我这个:

{ clients: [Getter/Setter], networth: [Getter/Setter] }

但是,当我这样做时它确实起作用。

wrapper.vm.someVariable = [ "here", "is", "some", "data" ]

我讨厌在每次测试中都必须设置此数据。

我在做什么错?

1 个答案:

答案 0 :(得分:0)

我知道了。与将数据设置为const相反,我必须从函数中将其返回。

function householdData() {
    return [ "here", "is", "some", "data" ]
}

然后我将其传递给组件道具,如下所示:

    const wrapper = mount(HouseholdsComponent, {
        propsData: {
            original_household: householdData(),
        }
    });

Voila!