Evan Lalo
Evan Lalo

Reputation: 1273

Reusable const for Jest tests

I'm trying to declare a const to be re-used for a number of tests.

For example:

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");
    })
});

The problem is that householdData does not seem to be getting set.

When I console.log householdData, I get this:

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

I've tried setting the data within the component like this as well:

wrapper.vm.someVariable = householdData

and that also gives me this:

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

However, it does work when I do this.

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

I would hate to have to keep setting this data in each test.

What am I doing wrong?

Upvotes: 1

Views: 491

Answers (1)

Evan Lalo
Evan Lalo

Reputation: 1273

I figured it out. As opposed to setting the data as a const, I had to return it from a function.

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

Then I pass it to component props like this:

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

Voila!

Upvotes: 1

Related Questions