一. 要注意npm包全局安装与本地安装的区别。
如果安装在全局,成功后直接输入karma start;如果安装在本地,输入./node_modules/karma/bin/karma start
二 输入karma init在本地生成karma.conf.js文件,进行karma的配置。
具体配置内容,参考官网。我们这里设置如下
// Karma configuration
// Generated on Wed Mar 28 2018 00:26:22 GMT+0800 (CST)
require('phantomjs-prebuilt').path = './node_modules/.bin/phantomjs';
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: ['jasmine'],
// list of files / patterns to load in the browser
files: [
"./test/api/**/*.js",
"./test/api/**/*.spec.js"
],
// 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: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Phantom'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
其中,browsers是浏览器设置,按上下键有选项。Phantom是无头浏览器
三 安装jasmine
npm install karma-jasmine直接安装即可
四 安装phantomjs
npm install phantomjs 以及karma的依赖:npm install karma-phantomjs-launcher
五 修改phantom安装过程中的bug
我这里踩坑比较重。npm intall phantomjs在下载过程中,各种卡壳,折腾了我整整一天,各种崩溃。痛恨npm。mac下,改用brew下载,具体代码brew update && brew install phantomjs




跑karma测试一下,提示下面错误


