karma与webpack结合
一、必备插件
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结合的更多相关文章
- Karma 4 - Karma 集成 Webpack 进行单元测试
可以将 karma 与 webpack 结合起来,自动化整个单元测试过程. 配置环境 1. 首先根据 1 完成基本的 karma 测试环境. 2. 安装 webpack 和 webpack 使用的 l ...
- #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(一)
目标: 为已有的vue项目搭建 karma+mocha+chai 测试框架 编写组件测试脚本 测试运行通过 抽出共通 一.初始化项目 新建项目文件夹并克隆要测试的已有项目 webAdmin-web 转 ...
- Rails 5 Test Prescriptions 第10章 Unit_Testing JavaScript(新工具,learn曲线太陡峭,pass)
对Js的单元测试是一个大的题目.作者认为Ruby的相关测试工具比Js的测试工具更灵活 大多数Js代码最终是关于响应用户的行为和改变DOM中的元素 没有什么javascript的知识点.前两节用了几个新 ...
- [React Unit Testing] React unit testing demo
import React from 'react' const Release = React.createClass({ render() { const { title, artist, outO ...
- [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 ...
- 学习Karma+Jasmine+istanbul+webpack自动化单元测试
学习Karma+Jasmine+istanbul+webpack自动化单元测试 1-1. 什么是karma? Karma 是一个基于Node.js的Javascript测试执行过程管理工具.该工具可 ...
- #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(二)
学习对vue组件进行单元测试,先参照官网编写组件和测试脚本. 1.简单的组件 组件无依赖,无props 对于无需导入任何依赖,也没有props的,直接编写测试案例即可. /src/testSrc/si ...
- Vue.js——60分钟webpack项目模板快速入门
概述 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开发模板.webpack提供了和browserify ...
- [笔记]ng2的webpack配置
欢迎吐槽 前言 angular.cn教程中用的是systemjs加载器,那用webpack应该怎么配置呢?本文 demo: https://github.com/LeventZheng/angular ...
随机推荐
- RequireJS学习笔记
前言 进入移动前端是很不错的选择,这块也是我希望的道路,但是不熟悉啊... 现在项目用的是require+backbone,整个框架被封装了一次,今天看了代码搞不清楚,觉得应该先从源头抓起,所以再看看 ...
- salesforce 零基础学习(四十八)自定义列表分页之Pagination基类封装 ※※※
我们知道,salesforce中系统标准列表页面提供了相应的分页功能,如果要使用其分页功能,可以访问http://www.cnblogs.com/zero-zyq/p/5343287.html查看相关 ...
- Dynatree使用
最近用到了Dynatree的树形结构,记录一下它的用法. 需求: 1.jquery.js 2.jquery-ui.custom.js 3.jquery.cookie.js 下载dynatree,放到资 ...
- 用扩展开发一个PHP类
原文:http://my.oschina.net/mickelfeng/blog/122519?p=1 假设我们要用PHP扩展实 现一个类Person,它有一个private的成员变量$_name和两 ...
- SQL Server 存储过程生成insert语句
你肯定有过这样的烦恼,同样的表,不同的数据库,加入你不能执行select insert 那么你肯定需要一条这样的存储过程,之需要传入表明,就会给你生成数据的插入语句. 当然数据表数量太大,你将最好用 ...
- Web API接口之FileReader
Web API接口之FileReader *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- ASP.NET MVC之表单集合数据自动绑定到对象属性(集合)中
前言 之前没遇到过这个问题,在项目中遇到这个问题时想法挺好,按照流程走下去,结果事与愿违,于是开始探索着解决方案,接下来我们来看看这个问题,早已经明了的童鞋请绕道,此文仅供未遇到的童鞋提供一种解决方案 ...
- 【重磅开源】Hawk-数据抓取工具:简明教程
Hawk-数据抓取工具:简明教程 标签(空格分隔): Hawk Hawk: Advanced Crawler& ETL tool written in C#/WPF 1.软件介绍 HAWK是一 ...
- spring源码分析之cache demo
spring提供了对echache.guava.jcache的支持,先看一个echache的示例: import org.springframework.cache.CacheManager; imp ...
- 5分钟学会使用Less预编译器
5分钟学会使用Less预编译器 Less是什么? LESS CSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法为CSS赋予了动态语言的特性,如变量.继承.运算.函数等,更方便 ...