vue单元测试学习笔记

import { expect } from 'chai'
import { mount } from '@vue/test-utils'
import Vue from 'vue'
import HelloWorld from '../../components/HelloWorld.vue'
import Count from '../../components/Count.vue'

describe('比较测试', () => {
	it('大小关系', () => {
		expect(5).to.greaterThan(3);
	})
	it('相等关系', () => {
		expect([1, 2, 3]).to.be.lengthOf(3);
	})
	it('包含关系', () => {
		expect('aeipyuan').to.be.contain('aeip');
	})
})
describe('Hello World.vue', () => {
	it('正常显示msg', () => {
		let wrapper = mount(HelloWorld, {
			propsData: { msg: 'hello' }
		});// 挂载
		expect(wrapper.find('h1').text()).to.be.contain('hello')
	})

})
describe('Count.vue', () => {
	it('测试点击计数', () => {
		let wrapper = mount(Count);
		expect(wrapper.find('#count').text()).to.be.equal('10');
		wrapper.find('#btn').trigger('click');/* 触发点击事件 */
		expect(wrapper.find('#count').text()).to.be.equal('11');
	})
})
//package.json
{
  "name": "util2",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit"
  },
  "dependencies": {
    "core-js": "^3.6.4",
    "vue": "^2.6.11",
    "vuex": "^3.1.3"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.3.0",
    "@vue/cli-plugin-unit-mocha": "~4.3.0",
    "@vue/cli-plugin-vuex": "~4.3.0",
    "@vue/cli-service": "~4.3.0",
    "@vue/test-utils": "1.0.0-beta.31",
    "chai": "^4.1.2",
    "vue-template-compiler": "^2.6.11"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}

posted @ 2020-04-08 16:59  aeipyuan  阅读(301)  评论(0编辑  收藏  举报