鍍金池/ 問答/網絡安全  HTML/ 關于vue單元測試,js函數(shù)測試的問題,提示expect不是一個函數(shù)。

關于vue單元測試,js函數(shù)測試的問題,提示expect不是一個函數(shù)。

我在做vue單元測試,項目都運行起來了,下面的組件測試斷言能通過。
Hello.vue的測試文件

/* Hello.spec.js */

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

// 掛載元素并返回已渲染的文本的工具函數(shù)
function getRenderedText(Component, propsData) {
  const Ctor = Vue.extend(Component)
  const vm = new Ctor({ propsData: propsData }).$mount()
  return vm.$el.textContent
}

describe('Hello.vue', () => {
  it('should render correct contents', () => {
    const Constructor = Vue.extend(loading)
    const vm = new Constructor().$mount()
    Vue.nextTick(() => {
      expect(vm.$el.querySelector('.loading-container h1').textContent).to.be('hello vue!')
    });
  })
})

測試結果

Hello.vue
    ? should render correct contents

但是我現(xiàn)在要測試 src/assets/js/utils.js里的工具函數(shù)。

/* utils.js */
export const currency = (value, currency, decimals) => {
   ...
}

我創(chuàng)建了相對應的測試文件:

/* utils.spec.js */
import Vue from 'vue'
import * as utils from '@/assets/js/utils.js'

// 這樣寫不對
describe('utils.js', () => {
  it('currency應該能輸出正確的值', done => {
    Vue.nextTick(() => {
      expect(utils.currency(10)).to.be(10.00);
      expect(utils.currency(10, '¥')).to.be('¥10.00');
    });
  })
})

//或者這樣寫也不對
describe('utils.js', () => {
  it('currency應該能輸出正確的值', done => { 
     expect(utils.currency(10)).to.be(10.00);
     expect(utils.currency(10, '¥')).to.be('¥10.00');
  })
})

上面的兩種寫法都有提示了一下console錯誤,是不是需要別的上面方法呢,還是說vue的單元測試只能測試vue組件或者vue的內部方法?

ERROR LOG: '[Vue warn]: Error in nextTick: "TypeError: expect(...).to.be is not a function"'
ERROR LOG: TypeError{}
回答
編輯回答
陌上花

為什么要加nextTick才可以呢? 去掉nextTick就報錯了

2017年3月25日 02:10
編輯回答
陪我終
describe('utils.js', () => {
  it('currency應該能輸出正確的值', done => { 
     expect(utils.currency(10)).to.be(10.00);
     expect(utils.currency(10, '¥')).to.be('¥10.00');
     done();//這里應該調用done,否則會出錯
  })
})

單純看這個函數(shù)除了done沒有調用之外沒有其他的問題。

2017年10月22日 09:32