Karma

官方介绍

A simple tool that allows you to execute JavaScript code in multiple real browsers.

即一个允许你在多个真实浏览器中执行js代码的简单工具。

使用了karma之后,我们之前为了Enzyme的mount而要求的环境就不需要用jsdom去模拟了,因为karma就是将测试js在真实浏览器中执行的。

安装:

npm i --save-dev karma karma-chai karma-webpack karma-mocha karma-chrome-launcher
npm i karma -g
npm install -g karma-cli //window命令行运行,如果显示不能发现webpack那么需要安装这个

然后配置karma:

karma init

以下为我的配置:

// Karma configuration
// Generated on Fri Feb 02 2018 10:07:20 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: ['mocha','chai'], // list of files / patterns to load in the browser
files: [
'src/**/*.js',
'src/**/*.jsx',//不知道为什么,karma无法识别jsx
'test/*.test.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: {
'src/**/*.js': ['webpack'],
//'src/**/*.jsx':['webpack'],//不知道为什么,karma无法识别jsx
'test/*.test.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: {
module: {
rules: [{
test: /\.jsx?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env', 'react']
}
}
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images/'
}
}]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
}
]
}
}
})
}

一点小疑惑,在用kamar的时候正则表达式匹配不到jsx文件,于是将项目内部的jsx文件都改为js就好了。

因为更喜欢jest的方式,所以对karma这种使用到浏览器的没有做更深入的研究,只是有所了解就够了。

【前端单元测试入门04】Karma的更多相关文章

  1. 【前端单元测试入门02】react的单元测试之Enzyme

    React项目的单元测试 React的组件结构和JSX语法,对上一章的内容来讲进行测试显得很勉强. React官方已经提供了一个测试工具库:react-dom/test-utils 只是用起来不够方便 ...

  2. 【前端单元测试入门03】Sinon

    前端测试存在的问题 在讲Sinon之前,我们得先讲一下在学习了Mocha.chai以及enzyme之后,我们的前端测试还存在的一些问题. 比如前台测试需要与后台交互,获取后台数据后再根据相应数据进行测 ...

  3. 前端单元测试环境搭建 Karma Jasmine

    Karma 官网On the AngularJS team, we rely on testing and we always seek better tools to make our life e ...

  4. 【前端单元测试入门01】Mocha与chai

    Mocha 的简介 Mocha是流行的JavaScript测试框架之一,通过它添加和运行测试,从而保证代码质量 Mocha 的安装与配置 全局安装Mocha npm install -g mocha ...

  5. 【前端单元测试入门05】react的单元测试之jest

    jest jest是facebook推出的一款测试框架,集成了前面所讲的Mocha和chai,jsdom,sinon等功能. 安装 npm install --save-dev jest npm in ...

  6. 在WebStorm中集成Karma+jasmine进行前端单元测试

    在WebStorm中集成Karma+jasmine进行前端单元测试 前言 好久没有写博了,主要还是太懒=.=,有点时间都去带娃.看书了,今天给大家分享一个原创的小东西,如果大家对TDD或者BDD有兴趣 ...

  7. 前端自动化测试工具--使用karma进行javascript单元测试(转)

    Karma+Jasmine+PhantomJS组合的前端javascript单元测试工具. 1.介绍 Karma是由Google团队开发的一套前端测试运行框架,karma会启动一个web服务器,将js ...

  8. Jest 前端单元测试工具

    Jest和enzyme 前端单元测试工具 什么是Jest? Jest是一个令人愉悦的JavaScript测试框架,其重点是简单性. 它适用于使用以下项目的项目:Babel,TypeScript,Nod ...

  9. openresty 前端开发入门五之Mysql篇

    openresty 前端开发入门五之Mysql篇 这章主要演示怎么通过lua连接mysql,并根据用户输入的name从mysql获取数据,并返回给用户 操作mysql主要用到了lua-resty-my ...

随机推荐

  1. qdoc.exe

    qdoc.exe Usage: qdoc [options] file1.qdocconf ... # Qt文档生成器 Qt documentation generator # 设置 Options: ...

  2. GB 标准

    std::map<int, std::string> GB2261 = { { 0,"未知的性别" }, { 1,"男性" }, { 2," ...

  3. hihoCoder 403 Forbidden 字典树

    题意:给定个规则,个ip,问这些ip是否能和某个规则匹配,如果有多个规则,则匹配第一个.如果没能匹配成功,则认为是"allow",否则根据规则决定是"allow" ...

  4. hdu4825 01字典树+贪心

    从高位向低位构造字典树,因为高位得到的数更大. AC代码: #include<cstdio> using namespace std; typedef long long LL; cons ...

  5. SSH服务端配置、优化加速、安全防护

    CentOS7自带的SSH服务是OpenSSH中的一个独立守护进程SSHD.由于使用telnet在网络中是明文传输所以用其管理服务器是非常不安全的不安全,SSH协议族可以用来对服务器的管理以及在计算机 ...

  6. PyQt5多点触控写字板实现及困惑

    Qt支持程序多点触控,就想使用PyQt5做一个触控画板,经过几番周折,查阅了TouchEvent官方文档,又参考了一篇QT for Android的例子,采用eventfilter过滤器来识别触屏事件 ...

  7. 【前端】JavaScript中prototype和__proto__的区别

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/prototype.html 经常有小伙伴问我关于prototype和__proto__的问题,觉得有必要写一篇博客 ...

  8. 安装JBoss Tool 出错

    安装JBoss Tool 出错 具体报错如下:

  9. Netty的并发编程实践4:线程安全类的应用

    在JDK1.5的发行版本中,Java平台新增了java.util.concurrent,这个包中提供了一系列的线程安全集合.容器和线程池,利用这些新的线程安全类可以极大地降低Java多线程编程的难度, ...

  10. Flex报错之一

    1.错误描述 TypeError: Error #1009: 无法访问空对象引用的属性或方法. at com.gwtjs.components::DetailWindow/completeHandle ...