TypeError: undefined is not an object (evaluating 'i18n._t')

背景

今天写vue+element单元测试报了错

ERROR LOG: '[Vue warn]: Error in render: "TypeError: undefined is not an object (evaluating 'i18n._t')"

解决

Unit tests not working since introducing vue-i18n

大佬是这样回复的

You need to inject an i18n object when instantiating your component.

Using your example :

import Vue from 'vue'
import Home from '@/components/Home'

import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({});

describe('Home.vue', () => {
  it('should render correct contents', () => {
    const Constructor = Vue.extend(Home)
    const vm = new Constructor({i18n}).$mount()
    expect(vm.$el.querySelector('h1').textContent)
      .to.equal('Welcome to Server Client Project (STP)!')
  })
})

加上去就好了

最后最好加个封装 util类 https://stackoverflow.com/questions/48238906/vue-project-tests-are-failing-when-i-added-vue-i18n-karma-mocha-phantomjs/48239256#48239256

猜你喜欢

转载自blog.csdn.net/qq_32340877/article/details/80455362