我需要能夠測試我的組件(方法,計算屬性,數據...)。然而,當導入我VUE部件在我的單元測試:單元測試打字稿vue組件
import Pagination from 'src/components/shared/pagination.vue'
import { newComponent } from '../component-factory'
describe('pagination.vue',() => {
const propsData = {
metadata: {
page: 2,
records: 155,
total: 11,
},
perPage: 15,
}
it('should have correct number of records',() => {
const ctor = Vue.extend(Pagination)
const vm = new ctor({propsData}).$mount()
expect(vm.firstRecord).toBe(16)
expect(vm.lastRecord).toBe(30)
})
...
vm
是Vue
類型的,並且因此不具有firstRecord
/lastRecord
性質。運行與因果報應的測試表明取得了成功,但打字稿編譯器吐出的錯誤:
ERROR in ./tests/shared/pagination.spec.ts
(16,19): error TS2339: Property 'firstRecord' does not exist on type 'Vue'.
ERROR in ./tests/shared/pagination.spec.ts
(17,19): error TS2339: Property 'lastRecord' does not exist on type 'Vue'.
我試過鑄造:
...
const vm = new ctor({propsData}).$mount() as Pagination
...
但是,這導致在VSCode警告:
[ts] Cannot find name 'Pagination'.
並且具有將vm
作爲any
類型的效果,這是完全適得其反的。
我覺得這一切都源自一個事實,即使用.vue
文件時必須添加的聲明莖:
declare module '*.vue' {
import Vue from 'vue'
export default typeof Vue
}
它清楚地規定所有.vue
文件到Vue
的類型,這是不準確一個謊言,但也沒有幫助...任何建議?我究竟做錯了什麼?
爲了將來的參考,我試圖使用vuetype爲每個.vue
文件生成.d.ts
文件,但遇到了this issue。另外,there is a request使.vue
成爲打字稿生態系統中的頭等公民,從而消除此問題。而且,我只是增加了vue language service extension