2017-05-10 4 views
2

Vue의 CLI에서 생성 된 webpack 템플릿을 사용 중이며 일부 단위 테스트를 추가하려고했습니다. 그리고 예를 이미 제공되고있어 완벽하게 작동합니다 :단위 테스트에서 오류가 발생했습니다. vue.js karma (webpack) : undefined가 생성자가 아닙니다.

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

describe('Hello.vue',() => { 
    it('should render correct contents',() => { 
    const Constructor = Vue.extend(Hello) 
    const vm = new Constructor().$mount() 
    expect(vm.$el.querySelector('.hello h1').textContent) 
     .to.equal('Welcome to Your Vue.js App') 
    }) 
}) 

그럼 내가 뷰의 문서에서 직접 복사 다른 테스트 (Documentation link)를 추가하려고하지만, 뭔가 이상한 일이 :

// Inspect the raw component options 
it('has a created hook',() => { 
    console.log(typeof Hello.created) 
    expect(typeof Hello.created).toBe('function') 
}) 

은 내가 가진 다음과 같은 오류 :

LOG LOG: 'function' 
    ✗ has a created hook 
    undefined is not a constructor (evaluating 'expect((0, _typeof3.default)(_Hello2.default.created)).toBe('function')') 
webpack:///test/unit/specs/Hello.spec.js:16:38 <- index.js:75919:64 

Hello.created이 나에게 undefined을 제공합니다 같은 것 같다,하지만 당신이 볼 수 있듯이, I console.log 두 번 확인하고 원하는 결과를 제공합니다 : undefined

아무도 나에게 무슨 일이 있었는지와 그 문제를 해결하는 방법에 대한 도움을 줄 수 있습니까? 나는 이미 솔루션 here을 시도했지만 여전히 작동하지 못했습니다.

<template> 
    <div class="hello"> 
    <h1>{{ msg }}</h1> 
    </div> 
</template> 

<script> 
export default { 
    name: 'hello', 
    data() { 
    return { 
     msg: 'Welcome to Your Vue.js App', 
     message: 'hello!' 
    } 
    }, 
    created() { 
    console.log('oh crap') 
    this.message = 'bye!' 
    } 
} 
</script> 

답변

0

템플릿이 실제로 단위 테스트를 할 자스민 대신 차이를 사용 밝혀 : 참고로

, 여기 Hello.vue가 보이는 방법입니다. 이 경우

,

expect(typeof Hello.created).to.equal('function')

또는

expect(Hello.created).to.be.a('function')

모두 작업

.

관련 문제