Mocha Chai Vue测试Vue组件:this。$ notify不是函数

时间:2019-11-30 12:30:25

标签: unit-testing vuejs2 mocha

我正在使用此组件:https://github.com/euvl/vue-notification

从那时起,我所有的Mocha chai测试单元都失败了。

this.$notify is not a function

这是我的登录规范:

// Importing The testing library 
import { expect } from "chai";
import { mount } from '@vue/test-utils'

// Importing  The component I need to test
import Login from "@/components/Login.vue";


// Mounting the component as in real life 
const wrapper = mount(Login);


describe("Login test", () => {

  it("getAuth() to be a function", () => {
    expect(wrapper.vm.getAuth).to.be.a("function");
  });

});

我已经尝试了mount,shallowMount,没有运气的渲染。

任何解决方法?

我在main.js中这样调用vue-notification:

import Notifications from "vue-notification";
Vue.use(Notifications);

谢谢!

编辑: 香港专业教育学院试图添加

const $notify = require('vue-notification')

没有运气到我的Login.vue组件

编辑2:试图没有运气地调用这样的函数:

 this.$root.$notify({
        group: 'foo',
        title: 'Hello ',
        text: 'Cool'
        });

[Vue warn]: Error in mounted hook: "TypeError: this.$root.$notify is not a function"

1 个答案:

答案 0 :(得分:0)

* EDIT:*****由我解决****** *  我严重导入了vue。请在此处查看我的工作 login.spec.js 测试文件:

// THE ASSERTION LIBRARY
import { expect } from "chai";

// THE TESTING LIBRARY
import { mount } from "@vue/test-utils";

// THE COMPONENT THAT I WANT TO TEST
import Login from "@/components/Login.vue";

// THE EXTERNAL COMPONENTS LINKED TO MY LOGIN COMPONENT  THAT I NEED TO JOIN
import Vue from 'vue';
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)
 import {
    required,
    minLength,
    between
} from "vuelidate/lib/validators";
import Notifications from "vue-notification";
import velocity      from 'velocity-animate'
Vue.use(Notifications, { velocity });


// THE WRAPPER CONTAIN MY LOGIN MOUNTED COMPONENT, JUST LIKE IN THE REAL LIFE
const wrapper = mount(Login)

describe("Login test", () => {

  it("getAuth() to be a function", () => {
    expect(wrapper.vm.getAuth).to.be.a("function");
  });

});

success result