关于vue单元测试,js函数测试的问题,提示expect不是一个函数。

发布于 2022-09-06 03:17:43 字数 1750 浏览 24 评论 0

我在做vue单元测试,项目都运行起来了,下面的组件测试断言能通过。
Hello.vue的测试文件

/* Hello.spec.js */

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

// 挂载元素并返回已渲染的文本的工具函数
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

但是我现在要测试 src/assets/js/utils.js里的工具函数。

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

我创建了相对应的测试文件:

/* 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{}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深陷 2022-09-13 03:17:43
describe('utils.js', () => {
  it('currency应该能输出正确的值', done => { 
     expect(utils.currency(10)).to.be(10.00);
     expect(utils.currency(10, '¥')).to.be('¥10.00');
     done();//这里应该调用done,否则会出错
  })
})

单纯看这个函数除了done没有调用之外没有其他的问题。

友欢 2022-09-13 03:17:43

为什么要加nextTick才可以呢? 去掉nextTick就报错了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文