Chai.js
让断言更加方便 https://www.chaijs.com/
chai.should();foo.should.be.a('string');foo.should.equal('bar');foo.should.have.lengthOf(3);tea.should.have.property('flavors').with.lengthOf(3);
var assert = chai.assert;assert.typeOf(foo, 'string');assert.equal(foo, 'bar');assert.lengthOf(foo, 3)assert.property(tea, 'flavors');assert.lengthOf(tea.flavors, 3);
var expect = chai.expect;expect(foo).to.be.a('string');expect(foo).to.equal('bar');expect(foo).to.have.lengthOf(3);expect(tea).to.have.property('flavors').with.lengthOf(3);
安装
$ npm install chai -D
目录结构
在项目的根目录创建 test 文件夹
创建文件 button.test.js
const expect = chai.expect;import Vue from 'vue'import Button from '../src/button'Vue.config.productionTip = falseVue.config.devtools = false
最简单的例子
Constructor是一个函数,通过Constructor构造一个button实例

describe('Button', () => {it('存在.', () => {expect(Button).to.exist})})
it('可以设置icon.', () => {const Constructor = Vue.extend(Button)const vm = new Constructor({propsData: {icon: 'settings'}}).$mount()const useElement = vm.$el.querySelector('use')expect(useElement.getAttribute('xlink:href')).to.equal('#isettings')vm.$destroy()})
模拟事件触发
安装 chai-spies
yarn add -D chai-spies
it('点击 button 触发 click 事件', () => {const Constructor = Vue.extend(Button)const vm = new Constructor({propsData: {icon: 'settings',}}).$mount()const callback = sinon.fake();vm.$on('click', callback)vm.$el.click()expect(callback).to.have.been.called})
