4

I'm using jest to run vue unit tests to check the output of individual components. The component uses vuetify.

I create an instance of vue to mount the component:

import myComponent from './MyComponent.vue';
import VueRouter from 'vue-router';
import Vuetify from 'vuetify';

describe('Component', () => {
    let wrapper;

    const router = new VueRouter({
        base: '/ui/',
        routes: [
            {
                name: 'myRoute',
                path: '/route-to-my-component',
                component: myComponent
            },
        ]
    });

    beforeEach(() => {
        const localVue = createLocalVue();
        localVue.use(VueRouter);
        localVue.use(Vuetify);

        wrapper = mount(myComponent, {
            localVue: localVue,
            router
        });
    });



    it('contains a container', () => {
      expect(wrapper.contains('v-container')).toBe(true);
    })
});

I expect this test to pass, but instead I'm getting TypeError: Cannot read property 't' of undefined.

For reference, I was following https://fernandobasso.github.io/javascript/unit-testing-vue-vuetify-with-jest-and-vue-test-utils.html.

2
  • 1
    where is createLocalVue imported from tests utils ? Commented Aug 26, 2019 at 22:22
  • I can't post a comment yet. Please edit your question and provide a code of the component. Since the error is related to t, this is most likely issue with translation. You will need to mock the translation module in your test. Commented Aug 30, 2019 at 19:44

1 Answer 1

7

A couple of (somewhat random) things are needed in order to run vue unit test on top of vuetify:

  1. Avoid using createLocalVue(), per this comment.

  2. Some components (like Autocomplete) need $vuetify.lang and $vuetify.theme to be mocked

Your spec should look like:

import Vuetify from 'vuetify';
import Vue from 'vue';

Vue.use(Vuetify);

it('contains a container', () => {
  const wrapper = mount(FreeAutocomplete, {
    created() {
      this.$vuetify.lang = {
        t: () => {},
      };
      this.$vuetify.theme = { dark: false };
    }
  });

  expect(wrapper.contains('v-container')).toBe(true);
})
Sign up to request clarification or add additional context in comments.

1 Comment

It can't find the 'this' scope when I do this.

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.