一、必备插件

1.babel:es6的语法支持

2.karma:测试框架

3.jasmine:断言框架

4.webpack:打包工具

5.karma-webpack:karma调用webpack打包接口的插件

二、实现步骤

1.通过npm安装上述必备的插件包

2.创建webpack.test.config.js文件,此文件的配置用于单元测试

var path = require('path');
var webpack = require('webpack');
module.exports={
module:{
loaders:[{
test:/\.js$/,
loader:'babel',
query:{
presets:['es2015']
},
exclude:[
path.resolve( __dirname, '../test' ), path.resolve( __dirname, '../node_modules' )
]
}]
}
};

注意:

1.此配置参数中没有entry、output两个节点的配置,打包的输入和输出karma会指定

3. 通过karma init命令创建karma.conf.js配置文件

此文件创建好之后,手动添加对webpack.test.config.js文件的引用,且需要增加如下节点:

1.webpack:设置webpack相关配置参数,也就是导入的webpack.test.config.js的对象

2.webpackMiddleware:设置webpack-dev-middleware(实现webpack的打包,但可以控制输入和输出)插件的相关参数

3.preprocessors:增加对webpack引用。

var webpackConfig = require('./webpack.test.config');
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/index.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: {
'../test/index.js':['webpack']
}, // 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, webpack: webpackConfig,
webpackMiddleware:{
noInfo:false
}
})
}

注意:配置的files与preprocessors节点都是指向单元测试的入口文件(test/index.js)

4.创建需要测试的源码与单元测试文件

1.src/cache/index.js:cache模块导出接口,本次只导出的memoryCache.js,代码如下:

export { default as MemoryCache } from './memoryCache';

2.src/cache/memoryCache.js:实现缓存数据的操作,也是需要单元测试的类,代码如下:

export default class MemoryCache extends abCache{
constructor( limit ){
super( limit );
this._map = [];
}
}
var p = MemoryCache.prototype;
p.push = function(key, item){
var entry = {
key: key,
value: item
};
this._map.push(entry);
};
p.get = function(key,ruturnEntry){
for(let item of this._map){
if(item.key == key){
return ruturnEntry ? item.value : item;
}
}
return null;
};
p.remove = function(key){
for(let index in this._map){
if(this._map[index].key == key){
this._map.splice(index,1);
return;
}
}
}

3.test/cache/memoryCacheTest.js:单元测试用例类

var _memory = require('../../src/cache/index.js').MemoryCache;
describe('memoryCache test',function(){
var _memeoryCache;
_memeoryCache = new _memory();
beforeEach(function(){ //每运行一个it时,之前执行
});
it('push',function(){
var foo = {"name":"foo.Name"};
_memeoryCache.push("foo",foo);
var _destFoo = _memeoryCache.get('foo',true);
expect(_destFoo).toBe(foo);
});
it('get', function(){
expect(_memeoryCache.get('foo',true)).not.toBeNull();
});
it('remove',function(){
_memeoryCache.remove('foo');
expect(_memeoryCache.get('foo')).toBeNull();
});
});

4.test/index.js:单元测试的入口文件

require('./cache/memoryCahceTest.js');

5. karma start运行单元测试即可。

karma与webpack结合的更多相关文章

  1. Karma 4 - Karma 集成 Webpack 进行单元测试

    可以将 karma 与 webpack 结合起来,自动化整个单元测试过程. 配置环境 1. 首先根据 1 完成基本的 karma 测试环境. 2. 安装 webpack 和 webpack 使用的 l ...

  2. #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(一)

    目标: 为已有的vue项目搭建 karma+mocha+chai 测试框架 编写组件测试脚本 测试运行通过 抽出共通 一.初始化项目 新建项目文件夹并克隆要测试的已有项目 webAdmin-web 转 ...

  3. Rails 5 Test Prescriptions 第10章 Unit_Testing JavaScript(新工具,learn曲线太陡峭,pass)

    对Js的单元测试是一个大的题目.作者认为Ruby的相关测试工具比Js的测试工具更灵活 大多数Js代码最终是关于响应用户的行为和改变DOM中的元素 没有什么javascript的知识点.前两节用了几个新 ...

  4. [React Unit Testing] React unit testing demo

    import React from 'react' const Release = React.createClass({ render() { const { title, artist, outO ...

  5. [Webpack 2] Use Karma for Unit Testing with Webpack

    When writing tests run by Karma for an application that’s bundled with webpack, it’s easiest to inte ...

  6. 学习Karma+Jasmine+istanbul+webpack自动化单元测试

    学习Karma+Jasmine+istanbul+webpack自动化单元测试 1-1. 什么是karma?  Karma 是一个基于Node.js的Javascript测试执行过程管理工具.该工具可 ...

  7. #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(二)

    学习对vue组件进行单元测试,先参照官网编写组件和测试脚本. 1.简单的组件 组件无依赖,无props 对于无需导入任何依赖,也没有props的,直接编写测试案例即可. /src/testSrc/si ...

  8. Vue.js——60分钟webpack项目模板快速入门

    概述 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开发模板.webpack提供了和browserify ...

  9. [笔记]ng2的webpack配置

    欢迎吐槽 前言 angular.cn教程中用的是systemjs加载器,那用webpack应该怎么配置呢?本文 demo: https://github.com/LeventZheng/angular ...

随机推荐

  1. 浅析z-index(覆盖顺序)和定位

    多次在项目中遇到html页面元素的非期待重叠错误,多数还是position定位情况下z-index的问题.其实每次解决类似问题思路大致都是一样的,说到底还是对z-index的理解比较模糊,可以解决问题 ...

  2. py2exe使用中遇到的几个问题

    问题: 在使用py2exe对所写的python脚本打包成.exe可执行程序时,遇到两个问题: 问题1: RuntimeError: maximum recursion depth exceeded w ...

  3. bootstrap 学习

    <!DOCTYPE html> <html> <head> <title>Bootstrap</title> <meta name=& ...

  4. javascript面向对象系列第三篇——实现继承的3种形式

    × 目录 [1]原型继承 [2]伪类继承 [3]组合继承 前面的话 学习如何创建对象是理解面向对象编程的第一步,第二步是理解继承.本文是javascript面向对象系列第三篇——实现继承的3种形式 [ ...

  5. 使用VS Code开发ASP.NET 5 应用程序

    本文简要地翻译了 https://code.visualstudio.com/Docs/runtimes/ASPnet5 并结合我的实践做了一些说明. 准备工作 1.安装VS Code  https: ...

  6. 图的广度优先搜索(BFS)

    把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...

  7. 千呼万唤始出来:Apache Spark2.0正式发布

    我们很荣幸地宣布,自7月26日起Databricks开始提供Apache Spark 2.0的下载,这个版本是基于社区在过去两年的经验总结而成,不但加入了用户喜爱的功能,也修复了之前的痛点. 本文总结 ...

  8. 1Z0-053 争议题目解析46

    1Z0-053 争议题目解析46 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 46.What happens when you run the SQL Tuning Adviso ...

  9. jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏

    一.需求: 我需要做一个多媒体播放页面,左侧为播放列表,右侧为播放器.为了避免系统滚动条把列表和播放器隔断开,左侧列表的滚动条需要自定义,并且滚动停止和鼠标离开时要隐藏掉. 二.他山之石: 案例来自h ...

  10. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...