【发布时间】:2020-02-16 18:11:07
【问题描述】:
我有一个使用 Vue Analytics 的 Vue 应用程序,它创建了一个 this.$ga.page 方法来进行页面跟踪。现在,我需要使用 Jest 测试我的组件,但它说 this.$ga.page 没有定义。我正在main.js 中初始化 Vue Analytics。
如何在测试文件中的组件前包含main.js?我知道我们需要在 beforeEach 方法中添加它,但我不知道具体该怎么做。
Vue Analytics init in main.js
import VueAnalytics from 'vue-analytics'
Vue.use(VueAnalytics, {
id: 'UA-150437966-1'
})
我的主页测试
import { mount } from '@vue/test-utils'
import Homepage from '../../src/Pages/Homepage'
describe('Homepage', () => {
const wrapper = mount(Homepage)
it('has a button', () => {
expect(wrapper.contains('button')).toBe(true)
})
})
Homepage.vue摘录
created: function() {
//analytics
this.$ga.page({
page: "/",
title: "Home page",
location: window.location.href
});
}
};
我遇到了错误
Homepage › encountered a declaration exception
TypeError: Cannot read property 'page' of undefined
67 | //analytics
68 |
> 69 | this.$ga.page({
【问题讨论】:
标签: vue.js google-analytics vuejs2 jestjs vue-test-utils