??????????Karma??????
?????SegmentFault ???????[ 2016/9/14 17:13:11 ] ??????????????? ???????
???????
?????????????У????????????????????????????о???????????????Karma??????????????????????????????а?????
????????Karma??
????Karma????Google????????????????????п????????????????????jasmine??mocha?????????????Щ??????????????????1?????
????1??Karma??????web???????????????js??????js??????????棻
????2?????????????????棬??????????????
????3??????????????????????????м??????????????
????Karma????????
????????????
????karma-example
?????????? src
?????????? index.js
?????????? test
?????????? package.json
????index.js??????????
????function isNum(num) {
????if (typeof num === 'number') {
????return true
????} else {
????return false
????}
????}
???????Karma????
???????????Karma?????????????????? karma-cli ?????????????????????
????npm i -g karma-cli
?????????????а??karma??
????npm i --save-dev karma
??????????????????????? karma init ?????в????????????????????????????ɡ?
?????????????????????????jasmine???????PhantomJS??????????е?????????????????????????????л?????????Chrome??IE??????????????????karma.conf.js?????
??????????????????Karma???л?????
????????Karma
??????test?????д????????????????????????jasmine????????????api????ο? jasmine api ??????????
????describe('index.js: '?? function() {
????it('isNum() should work fine.'?? function() {
????expect(isNum(1)).toBe(true)
????expect(isNum('1')).toBe(false)
????})
????})
?????????????????????? karma start ?????????????????е???????
????????????????е??????????????
????????????????????????????????????????????????????????????????Karma?????????????????У??????????????????
????Coverage
??????κ??????????????????????????ο??????????????coverage????
????????????????????????????????е??????????д????????????????????????????????????????????????岻???????????????????в????????
?????????????????????????????????д??????????????ο?????4?????????????????д????????????????????????????????????????Karma?????????Coverage??
????????????Karma?????????
????npm i --save-dev karma-coverage
?????????????????karma.conf.js??
module.exports = function(config) {
config.set({
basePath: ''??
frameworks: ['jasmine']??
files: [
'src/**/*.js'??
'test/**/*.js'
]??
exclude: []??
// modified
preprocessors: {
'src/**/*.js': ['coverage']
}??
//modified
reporters: ['progress'?? 'coverage']??
// add
coverageReporter: {
type : 'html'??
dir : 'coverage/'
}??
port: 9876??
colors: true??
logLevel: config.LOG_INFO??
autoWatch: true??
browsers: ['PhantomJS']??
singleRun: false??
concurrency: Infinity
})
}
?????????? karma start ??????????????coverage?????????б??β?????????檔???????????
???????Webpack+Babel
?????????????У????????????Webpack??ES6?????????????Webpack??Babel?????Karma?????С?
???????karma-webpack
????npm i --save-dev karma-webpack
???????babel
????npm i --save-dev babel-loader babel-core babel-preset-es2015
?????????????и??? src/index.js ???????
????function isNum(num) {
????if (typeof num === 'number') {
????return true
????} else {
????return false
????}
????}
????exports.isNum = isNum
????text/index.js ???????
????const Util = require('../src/index')
????describe('index.js: '?? () => {
????it('isNum() should work fine.'?? () => {
????expect(Util.isNum(1)).toBe(true)
????expect(Util.isNum('1')).toBe(false)
????})
????})
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11