安装 Karma
npm install -g karma-cli |
karmainit |
npm install karma --save-dev |
基本内容是:
// Karma configuration // Generated on Wed Jul 12 2017 18:29:58 GMT+0800 (CST) 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: [ ], // 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: { }, // 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: ['Chrome'], // 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 }) } |
framework 则表示需要引用的框架,比如 jasmine 或者 mocha。
preprocessors 表示 在浏览器进行测试前你需要对文件的一些预处理。
我们后边会贴出完整的 karma.conf.js .
安装webpack 和 babel
npm install webpack babel-polyfill babel-preset-stage-2
babel-plugin-transform-runtime babel-loader babel-core babel-preset-env
babel-istanbul --save-dev |
{ "presets": [ ["env", { "modules": false }], "stage-2" ], "plugins": ["transform-runtime"], "comments": false, "env": { "test": { "presets": ["env", "stage-2"], "plugins": [ "istanbul" ] } } } |
npm install karma-webpack karma-sourcemap-loader --save-dev
安装 mocha 和 chai
这个时候我们需要安装 mocha ,mocha 是一款功能丰富的javascript单元测试框架,它既可以运行在nodejs环境中,也可以运行在浏览器环境中。
npm install mocha --save-dev |
npm install karma-mocha --save-dev |
expect(foo).to.not.equal('bar'); expect(goodFn).to.not.throw(Error); expect({ foo: 'baz' }).to.have.property('foo') .and.not.equal('bar'); |
npm install chai karma-chai --save-dev |
npm install karma-chrome-launcher --save-dev |
const webpack = require("webpack"); const path = require('path'); module.exports = function(config) { config.set({ files: [ '../node_modules/babel-polyfill/dist/polyfill.min.js', './test/**/*.test.js' ], exclude: ['**/node_modules/**/*.test.js'], frameworks: ['mocha', 'chai'], logLevel: config.LOG_INFO, //config.LOG_DISABLE, // config.LOG_INFO singleRun: true, preprocessors: { './test/**/*.test.js': ['webpack'] }, // reporters: ['mocha'], webpack: { module: { // devtool: 'inline-source-map', loaders: [ { test: /\.js$/, loader: 'babel-loader', exclude: /(node_modules|bower_components)/, }, ] } }, webpackServer: { noInfo: true }, plugins: [ "karma-webpack", "karma-mocha", "karma-chai", "karma-chrome-launcher", "karma-sourcemap-loader" ], browsers: ['Chrome'], // ['Chrome'] concurrency: Infinity }); }; |
"test": "karma start test/karma.conf.js", |
如果你需要引入 DOM 的操作,你需要安装 mocha-jsdom,这样你可以实现对 DOM 节点的操作和判断。