VueJS - Unit testing with vue-test-utils gives error - TypeError: _vm.$t is not a function VueJS - Unit testing with vue-test-utils gives error - TypeError: _vm.$t is not a function vue.js vue.js

VueJS - Unit testing with vue-test-utils gives error - TypeError: _vm.$t is not a function


const $t = () => {}shallow(Component, { mocks:{ $t }})

This way you dont have to load the whole i18n library. You can even spy on the function with Sinon or jest.fn() if using Jest.


The error isn't in the <style> tag because Jest will compile your Vue component and extract the JS code. So you can ignore the line error for now (I'm not sure how to fix it).

But based on your error message, the problem seems to be related to the use of vue i18n and you're missing it when declaring your Vue component in the test file. Try adding these lines to your test file:

import i18n from 'path-to-your-i18n-file'// ...cmp = shallow(SignupLayout, {  store,  localVue,  propsData: {      title: ['Login']  },  data: {      email: 'abc@dsf.com'  },  i18n // <- add the i18n object here})


Another way to mock the i18n library is mock it in a separate js file

import VueTestUtils from '@vue/test-utils';VueTestUtils.config.mocks.$t = key => key;

and add it in Jest configuration

 "setupFiles": ["<rootDir>/setup.js"]

so if you have more components with resource imports you don't need to mock it separately.