VueのCLIで生成されたwebpackテンプレートを使用しており、単体テストを追加しようとしています。そして例は、既に提供されていて、それが完璧に動作します:私が得たユニットテストでエラーが発生しましたvue.js karma(webpack):未定義がコンストラクタではありません
// Inspect the raw component options
it('has a created hook',() => {
console.log(typeof Hello.created)
expect(typeof Hello.created).toBe('function')
})
:
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)から直接コピーされたテストが、奇妙で起こった何かを追加しよう次のエラー:
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
だから、私にundefined
を与えるHello.created
のように思えるが、あなたが見ることができるように、私はまた、console.log
それは二重チェックするために、それはそれが望む結果を与えます:undefined
誰も私に何が起こったか、それを修正する方法について助けを与えることができますか?私はすでにhereのソリューションを試しても、それを動作させることができませんでした。あなたの参考のために
は、ここHello.vue
のように見える方法は次のとおりです。
<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>