1

I am new to testing and GraphQL. I am trying to test my VueJS app which uses GraphQL using vue-test-utils and jest. I have a component which gets the categories from a graphql server in mounted hook.

mounted() {
  this.$apollo
    .query({
      query: getCategories
    })
    .then(res => {
      this.categoriesData[0].options = res.data.categories;
    });
}

In my StepTwo.spec.js in I am adding Vue-apollo to vue instance.

const Vue = createLocalVue();
Vue.use(VeeValidate);
Vue.use(BootstrapVue);
Vue.use(VueApollo);

test("Step Two ", async () => {
  const wrapper = shallowMount(StepTwo, { localVue: Vue });
});

When I'm trying to mount my component I'm getting TypeError: Cannot read property 'query' of undefined. Any help would be appreciated.

1 Answer 1

6

I managed to work it out but I don't know if this is the correct way or not. I mocked $apollo when I was mounting the component.

mocks: {
  $apollo: {
    query: jest.fn(() => Promise.resolve({
      data: {
        categories,
        subCategories: categories
      }
    }))
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is a pretty common pattern with Vue tests in my experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.