单元测试中的错误vue.js karma:undefined不是构造函数() [英] error in unit test vue.js karma : undefined is not a constructor ()

查看:164
本文介绍了单元测试中的错误vue.js karma:undefined不是构造函数()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一次单元测试,我收到的错误消息无法找到为什么我到目前为止在论坛中找到它。

This is my first unit test and I'm getting an error message that couldn't find why I get it in the forums so far.

这是我的单元测试:

import LoginPage from 'src/pages/Login'

describe('Login.vue', () => {
it('mounted is a fuction', () => {
    expect(typeof LoginPage.mounted).toBe('function')
})
})

这是登录页面:

<template>
<div class="">
    <p v-if="$route.query.redirect">
       You need to login first.
    </p>
    <form class="column is-one-third is-offset-one-third" @submit.prevent="login">
    <div class="control">
        <input type="email" placeholder="email" v-model="email" class="input">
    </div>
    <div class="control">
        <input type="password" autocomplete="off" placeholder="password" v-model="pass" class="input">
    </div>
    <div class="control">
        <button class="button is-primary" type="submit">Login</button>
        <a class="button" href="/signup">Sign up</button>
    </div>
    <p v-if="error" class="help is-danger">{{ error }}</p>
</form>
</div>
</template>
<script>
export default {
props: ['state'],
data () {
return {
    email: '',
    pass: '',
    error: ''
  }
},
mounted () {
if (this.state.auth.currentUser) {
    this.$router.replace(this.$route.query.redirect || '/')
}
},
methods: 
{
....//
}
}


这是我收到的错误消息:

mounted is a fuction
Login.vue
undefined is not a constructor (evaluating 'expect((0, _typeof3.default)(_Login2.default.mounted)).toBe('function')')
webpack:///test/unit/specs/Component.spec.js:5:42 <- index.js:161:65

感谢您的帮助

推荐答案

这里有两点你不知道。

首先,你不会在vue组件上获得方法就像那样,vue在内部代理方法,数据等,以便可以通过引用它们这可能会导致你的困惑。

Firstly, you won't get methods on a vue component just like that, vue internally proxies the methods, data etc such that they can be referenced via this maybe this led to your confusion.

解决方案: componentName.methods.methodName 在您的情况下 LoginPage.methods.mounted

Solution: componentName.methods.methodName in your case LoginPage.methods.mounted

将您的代码更改为:

import LoginPage from 'src/pages/Login'

describe('Login.vue', () => {
  it('mounted is a fuction', () => {
    expect(typeof LoginPage.methods.mounted).toBe('function')
  })
})

这篇关于单元测试中的错误vue.js karma:undefined不是构造函数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆