一、使用Karma做自動化測試
1坎炼、Karma([?kɑrm?] 卡瑪)是一個測試運行器愧膀,它可以呼起瀏覽器(打開瀏覽器),加載測試腳本谣光,然后運行測試用例檩淋;
Mocha([?mo?k?] 摩卡)是一個單元測試框架/庫,它可以用來寫測試用例;
Sinon(西農(nóng))是一個 spy / stub / mock 庫蟀悦,用以輔助測試(使用后才能理解)媚朦;
以上這些都是工具,只要會使用即可日戈;
2询张、步驟
①安裝各種工具,npm i -D karma karma-chrome-launcher karma-mocha karma-sinon-chai mocha sinon sinon-chai karma-chai karma-chai-spies
②創(chuàng)建 karma 配置浙炼,新建 karma.conf.js
// karma.conf.js
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon-chai'],
client: {
chai: {
includeStack: true
}
},
// list of files / patterns to load in the browser
files: [
'dist/**/*.test.js',
'dist/**/*.test.css'
],
// list of files / patterns to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
③創(chuàng)建 test/button.test.js 文件
const expect = chai.expect;
import Vue from 'vue'
import Button from '../src/button'
Vue.config.productionTip = false
Vue.config.devtools = false
describe('Button', () => {
it('存在.', () => {
expect(Button).to.be.ok
})
it('可以設(shè)置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('#i-settings')
vm.$destroy()
})
it('可以設(shè)置loading.', () => {
const Constructor = Vue.extend(Button)
const vm = new Constructor({
propsData: {
icon: 'settings',
loading: true
}
}).$mount()
const useElements = vm.$el.querySelectorAll('use')
expect(useElements.length).to.equal(1)
expect(useElements[0].getAttribute('xlink:href')).to.equal('#i-loading')
vm.$destroy()
})
it('icon 默認(rèn)的 order 是 1', () => {
const div = document.createElement('div')
document.body.appendChild(div)
const Constructor = Vue.extend(Button)
const vm = new Constructor({
propsData: {
icon: 'settings',
}
}).$mount(div)
const icon = vm.$el.querySelector('svg')
expect(getComputedStyle(icon).order).to.eq('1')
vm.$el.remove()
vm.$destroy()
})
it('設(shè)置 iconPosition 可以改變 order', () => {
const div = document.createElement('div')
document.body.appendChild(div)
const Constructor = Vue.extend(Button)
const vm = new Constructor({
propsData: {
icon: 'settings',
iconPosition: 'right'
}
}).$mount(div)
const icon = vm.$el.querySelector('svg')
expect(getComputedStyle(icon).order).to.eq('2')
vm.$el.remove()
vm.$destroy()
})
it('點擊 button 觸發(fā) 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
})
})
④創(chuàng)建測試腳本份氧,在 package.json 里面找到 scripts 并改寫 scripts;
"scripts": {
"dev-test": "parcel watch test/* --no-cache & karma start",
"test": "parcel build test/* --no-minify && karma start --single-run"
},
⑤運行npm run test
弯屈,報錯了蜗帜,那是因為你每次運行時沒有清除緩存,需要改一下package.json的scripts资厉,加上--no-cache厅缺,"test": "parcel build test/* --no-cache --no-minify && karma start --single-run"
( --no-minify是不要壓縮),還有一個原因是需要在button.vue中引入一下icon組件才可以宴偿,之后再運行npm run test就成功了
npm run test
整個步驟就是先用parcel打包button.vue店归,然后再用Karma打開瀏覽器進行測試,測試完畢后關(guān)閉瀏覽器酪我;⑥karma.conf.js中
3消痛、測試用例怎么寫?
describe
和it
都哭,它是BDD(行為驅(qū)動開發(fā))秩伞;describe
和it
是屬于Mocha,這個不用引入欺矫,它會自動引入纱新;具體的斷言expect
是由chai.js引入的,chai.js是需要安裝引入的穆趴;
describe('要測得組件',()=>{
it('此次測試的名字', ()=>{
expect(xx).to.equal(yyy) // expect(xx).to.eq(yyy) equal可寫成eq
})
})
二脸爱、持續(xù)集成(travis ci )
1、根目錄下創(chuàng)建“.travis.yml”文件
language: node_js
node_js:
- "8"
addons:
chrome: stable
sudo: required
before_script:
- "sudo chown root /opt/google/chrome/chrome-sandbox"
- "sudo chmod 4755 /opt/google/chrome/chrome-sandbox"
2未妹、打開https://www.travis-ci.org/簿废,使用GitHub登錄;添加項目后返回首頁络它,每次push后travis會自動去檢測族檬,每當(dāng)檢測結(jié)果狀態(tài)發(fā)生變化時,它會發(fā)郵件通知你化戳;
可以參考阮一峰這篇文章单料;
三、用npm發(fā)布自己的包
0、首先確保你的代碼通過了測試扫尖;
1白对、更改package.json里的版本號為“0.0.1”(一般還未正式發(fā)布的包版本號都為0.0.x);
2换怖、創(chuàng)建 index.js甩恼,在 index.js 里將你想要導(dǎo)出的內(nèi)容全部導(dǎo)出;
3狰域、去https://www.npmjs.com/注冊一個賬戶媳拴;
4、注冊完一定要去郵箱里確認(rèn)一下U桌馈G取!抬探!
5子巾、在 gulu 項目根目錄運行 npm adduser;
①你需要注意的是npm adduser必須要更換為 npm 官方源小压;
6线梗、運行 npm publish;
7怠益、別忘了把淘寶源改回來R巧Α!蜻牢!
四烤咧、下載安裝使用自己的包來檢查還有哪些錯誤
1、首先你要自己預(yù)測用戶會怎樣使用你的包
①使用vue-cli;
②使用webpack抢呆;
③使用parcel煮嫌;
2、這里我們以vue-cli為例來運行走一遍(實際中應(yīng)該每種方式都運行一遍)
①根據(jù)文檔使用vue-cli創(chuàng)建一個文件(需要注意的是如果一開始用npm安裝抱虐,就自始至終都用npm昌阿,不要一會npm一會yarn,會出錯的)
②創(chuàng)建好恳邀,我們就開始安裝我們的輪子npm i darkti-gulu
(darkti-gulu懦冰,就是package.json里的name)
③在main.js中引用我們的輪子,發(fā)現(xiàn)有報錯Failed to resolve loader: sass-loader
轩娶,原因就是我們在寫輪子的時候直接用的import和export儿奶,node目前不支持import,不應(yīng)該直接把import暴露給用戶鳄抒,我們需要把它轉(zhuǎn)換成用戶那邊能看懂的語法,用babel把import變成可執(zhí)行的js,那么有兩種選擇:
a. 一是你讓用戶自己使用babel去轉(zhuǎn)譯
b. 二是你轉(zhuǎn)譯好了许溅,讓用戶直接去用
-
npx parcel build index.js --no-minify
(本來不應(yīng)該加 --no-minify 的瓤鼻,奈何不加會有 bug,HTML 壓縮把 slot 標(biāo)簽全刪了) - 將 package.json 的 main 改為 dist/index.js
- 還要把版本號改一下贤重,npm不允許發(fā)布相同的版本號茬祷,然后push一下,push完了就
npm publish
發(fā)布一下(注意啦2⒒取<婪浮!發(fā)布的時候要把npm源改一下滚停,不能是淘寶的源沃粗,發(fā)布完成后再把源改回來) -
發(fā)布完成后,再npm裝一下键畴,如果一直裝不上最新的最盅,直接指定版本號來裝
image.png
image.png
image.png
3、引用進去你發(fā)現(xiàn)按鈕沒有樣式F鹛琛N屑!這里就只能讓用戶先把css自己引進去惹想,后面再進行優(yōu)化
import 'darkti-gulu/dist/index.css'
<style>
:root {
--button-height: 32px;
--font-size: 14px;
--button-bg: white;
--button-active-bg: #eee;
--border-radius: 4px;
--color: #333;
--border-color: #999;
--border-color-hover: #666;
}
</style>
五问词、使用npm link
來快速運行自己的輪子
1、為了不要上傳發(fā)布再下載這一系列繁瑣的步驟嘀粱,所以使用npm link
來快速在自己的機子上模擬用戶操作自己的包激挪;
2、你改動了輪子后草穆,先npx parcel build index.js --no-cache --no-minify
打一下包灌灾,只需要第一次npm link
一下,成功后悲柱,在用戶測試的目錄下npm link darkti-gulu
锋喜,你就得到了最新版的包了;
3豌鸡、之后每次更新過輪子后嘿般,只需打一下包,在用戶包那邊npm link darkti-gulu
就OK啦~~~