
步骤二:安装Karma
运行Node.js的命令行程序:Node.js command prompt:
图2(处于“开始->所有程序->Node.js”中):

图3(我们将安装到E:Karma路径下):

输入命令安装Karma:
复制代码 代码如下:
npm install karma --save-dev
图4(Karma安装完毕后):

步骤三:安装karma-jasmine/karma-chrome-launcher插件
继续输入npm命令安装karma-jasmine、karma-chrome-launcher插件:
复制代码 代码如下:
npm install karma-jasmine karma-chrome-launcher --save-dev
图5(karma-jasmine、karma-chrome-launcher安装完毕之后):

步骤四:安装karma-cli
karma-cli用来简化karma的调用,安装命令如下,其中-g表示全局参数,这样今后可以非常方便的使用karma了:
复制代码 代码如下:
npm install -g karma-cli
图6(karma-cli安装完毕之后):

Karma-Jasmine安装完毕:
图7(安装完毕后,在E:Karma文件夹下会有一个node_modules目录,里面包含刚才安装的karma、karma-jasmine、karma-chrome-launcher目录,当然还包含了jasmine-core目录):
开启Karma:
输入命令:
复制代码 代码如下:
karma start
图8(运行后如图所示出现了一行INFO信息,并没有其他提示和动作,因为此时我们没有配置karma的启动参数。后面会加入karma.conf.js,这样karma就会自动启动浏览器并执行测试用例了):

图9(手动打开Chrome,输入localhost:9876,如果看到这个页面,证明已经安装成功):

Karma+Jasmine配置:
执行命令init命令进行配置:
karma init
图10(所有默认配置问题):

说明:
1. 测试框架:我们当然选jasmine
2. 是否添加Require.js插件
3. 选择浏览器: 我们选Chrome
4. 测试文件路径设置,文件可以使用通配符匹配,比如*.js匹配指定目录下所有的js文件(实际操作中发现该路径是karma.conf.js文件的相对路径,详见下面我给出的实际测试配置及说明)
5. 在测试文件路径下,需要排除的文件
6. 是否允许Karma监测文件,yes表示当测试路径下的文件变化时,Karma会自动测试
我在虚拟机上测试的例子:
图11(TestFiles和NodeJS处于E盘根目录下,karma.conf.js处于文件夹NodeJS的根目录下):

以下是karma.conf.js的完整内容:
// Karma configuration // Generated on Fri May :: GMT+ (中国标准时间) module.exports = function(config) { config.set({// base path that will be used to resolve all patterns (eg. files, exclude)basePath: "../TestFiles",// frameworks to use// available frameworks: https://npmjs.org/browse/keyword/karma-adapterframeworks: ["jasmine"],// list of files / patterns to load in the browserfiles: ["*.js"],// list of files to excludeexclude: [],// preprocess matching files before serving them to the browser// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessorpreprocessors: {},// test results reporter to use// possible values: "dots", "progress"// available reporters: https://npmjs.org/browse/keyword/karma-reporterreporters: ["progress"],// web server portport: ,// 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_DEBUGlogLevel: config.LOG_INFO,// enable / disable watching file and executing tests whenever any file changesautoWatch: true,// start these browsers// available browser launchers: https://npmjs.org/browse/keyword/karma-launcherbrowsers: ["Chrome"],// Continuous Integration mode// if true, Karma captures browsers, runs the tests and exitssingleRun: false }); };说明:files: ["../TestFiles/jasmineTest.js","../TestFiles/test.js" ]test.js内容: function TT() { return "abc";}jasmineTest.js内容: describe("A suite of basic functions", function () { it("test", function () {expect("abc").toEqual(TT()); });}); 启动Karma: 
图13(如果我们点击图12中的debug按钮,进入debug.html并按F12打开开发者工具,选择Console窗口,我们将能看到jasmine的执行日志):

若此时,我们将jasmineTest.js中对于调用TT方法的期望值改为"abcd"(实际为"abc"):
describe("A suite of basic functions", function () { it("test", function () {expect("abcd").toEqual(TT()); });}); 
代码覆盖率:
如果你还想查看测试的代码覆盖率,我们可以安装karma-coverage插件,安装命令为:
复制代码 代码如下:
npm install karma-coverage
图15(安装karma-coverage的过程):

修改karma.conf.js,增加覆盖率的配置:
图16(主要是变动了以下三个配置节点,其他的配置内容不变):
// preprocess matching files before serving them to the browser// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessorpreprocessors: { "../TestFiles/test.js":"coverage"},// test results reporter to use// possible values: "dots", "progress"// available reporters: https://npmjs.org/browse/keyword/karma-reporterreporters: ["progress","coverage"],coverageReporter:{ type:"html", dir:"../TestFiles/coverage/"}, 变动如下:// Karma configuration// Generated on Fri May 29 2015 19:30:26 GMT+0800 (中国标准时间)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: ["../TestFiles/jasmineTest.js","../TestFiles/test.js" ], // list of files to exclude exclude: [ ], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: {"../TestFiles/test.js":"coverage" }, // test results reporter to use // possible values: "dots", "progress" // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ["progress","coverage"], coverageReporter:{type:"html",dir:"../TestFiles/coverage/" }, // 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: ["Chrome"], // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: false });};执行命令: