React项目搭建基于Karma的CI环境
简介
在浏览Github的时候是否经常看到这样的CI图标呢?

本文即为介绍如何为基于React的项目配置CircleCI的自动化测试环境
源码在此
本地实现
项目依赖如下:
"devDependencies": {
"jasmine-core": "^2.4.1",
"karma": "^0.13.19",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.23",
"karma-webpack": "^1.7.0",
"phantomjs-prebuilt": "^2.1.3",
"webpack": "^1.12.1",
"eslint-plugin-react": "^3.11.1",
"babel-core": "^5.8.24",
"babel-loader": "^5.3.2",
"babel-eslint": "^5.0.0-beta6",
"css-loader": "^0.18.0",
"phantomjs-polyfill": "0.0.1",
"style-loader": "^0.12.3"
},
"dependencies": {
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7"
}
是基于phantomjs作为运行环境,Karma作为Test Runner的工程结构。
karma.conf.js
const webpack = require('webpack')
module.exports = function (config) {
config.set({
browsers: ['PhantomJS'],
singleRun: true,
frameworks: [ 'jasmine' ],
files: [
'./node_modules/phantomjs-polyfill/bind-polyfill.js',
'./unit-test.webpack.js'
],
preprocessors: {
'./unit-test.webpack.js': [ 'webpack', 'sourcemap' ]
},
reporters: [ 'spec' ],
plugins: [
require('karma-webpack'),
require('karma-jasmine'),
require('karma-phantomjs-launcher'),
require('karma-sourcemap-loader'),
require('karma-spec-reporter')
],
webpack: {
devtool: 'inline-source-map',
module: {
loaders: [
{ test: /\.(jpe?g|png|gif|svg)$/, loader: 'url', query: {limit: 10240} },
{ test: /\.js$/, exclude: /node_modules/, loaders: ['babel']},
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.scss$/, loader: 'style!css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap' }
]
},
resolve: {
modulesDirectories: [
'components',
'node_modules'
],
extensions: ['', '.json', '.js']
},
plugins: [
new webpack.NoErrorsPlugin(),
]
},
webpackServer: {
noInfo: true
}
})
}
unit-test.webpack.js
//All test case naming follow /-test\.js$/ regexp pattern.
const context = require.context('./components', true, /-test\.js$/)
context.keys().forEach(context)
设置好工作环境后,我们可以写一个最基本的Component与对应的tests
Counter.js
import React, { Component } from 'react'
export default class Counter extends Component {
render() {
const { count } = this.props
return (
<div>
<span id='counter'>{ count }</span>
</div>
)
}
}
client-test.js
import React from 'react'
import { renderIntoDocument } from 'react-addons-test-utils'
import ReactDOM from 'react-dom'
import Counter from '../Counter'
function renderComponent(count) {
const renderer = renderIntoDocument(
<Counter count={count}/>
)
return ReactDOM.findDOMNode(renderer)
}
describe('Counter', () => {
it('should be renderered', () => {
const dom = renderComponent()
expect(dom).not.toBeUndefined()
})
it('should render correct number', () => {
const dom = renderComponent(10)
const count = dom.querySelector('#counter').textContent
expect(count).toBe('10')
})
})
现在即可以本地运行Karma start,结果图:

对接circleci
我们希望有自动化的CI环境,这样可以更方便团队开发。
本文以circleci为例。
关联好Github账号后,CI下的build目录中即可导入工程。

针对Node项目,circleci会自动运行npm run test,所以暂时没有必要配置相关的yml文件。
结果如下

有问题欢迎讨论
React项目搭建基于Karma的CI环境的更多相关文章
- React项目搭建与部署
React项目搭建与部署 一,介绍与需求 1.1,介绍 1.1.1,React简介 React 是一个用于构建用户界面的 JAVASCRIPT 库. React主要用于构建UI,很多人认为 React ...
- 基于webpack的React项目搭建(三)
前言 搭建好前文的开发环境,已经可以进行开发.然而实际的项目中,不同环境有着不同的构建需求.这里就将开发环境和生产环境的配置单独提取出来,并做一些简单的优化. 分离不同环境公有配置 不同环境虽然有不同 ...
- 基于webpack的React项目搭建(一)
前言 工欲善其事,必先利其器.为了更好的学习React,我们先简要的把开发环境搭建起来.本文主要介绍使用webpack搭建React项目,如果你对React或es6的基础语法还不了解,建议先去学习学习 ...
- react项目搭建及webpack配置
1,配置webpack npm install -g webpack webpack的cli环境 npm install -g webpack-dev-se ...
- 车牌识别(end-to-end-for-chinese-plate-recognition)项目搭建基于Mxnet(Python 3.5)
最近看到geihub上有个车牌识别的项目,感觉很有意思,从上面fork了一下弄到本地自己跑了下.在安装过程中遇到了一些问题,记录如下. 项目github连接:https://github.com/sz ...
- React项目搭建及依赖安装
一.前提 首先保证node.js已安装完成... 安装完成后,打开cmd命令行,输入 node -v 和 npm -v 来查看版本号,如果显示则安装完成. 二.安装react脚手架 在cmd命令行中输 ...
- 基于webpack的React项目搭建(二)
前言 前面我们已经搭建了基础环境,现在将开发环境更完善一些. devtool 在开发的过程,我们会经常调试,so,为了方便我们在chrome中调试源代码,需要更改webpack.config.js,然 ...
- 通过nginx搭建基于python的web环境
前言: 在搭建开始前,我们先来梳理下web服务工作流程,先看下图: 1.用户(PC)向web服务器发起http请求 2.web服务器判断用户请求文件是否为静态文件,是则直接读取静态文件并返回给用户,不 ...
- react 项目搭建
1.首先运行环境-node是必须的,需要下载安装node的运行环境: 2.安装好了node之后,自然的就有了npm: 3.npm install -g creact-react-app/全局安装cre ...
随机推荐
- 【C/C++】获取当前系统时间
#include<iostream> #include<Ctime> using namespace std; int main() { time_t t; time(& ...
- tkinter.py
from tkinter import * def hello():print('hello world') win=Tk() win.title('hello tkinter') win.geome ...
- Android Studio工程Gradle编译报错
一.环境的搭建: 首先搭建好AndroidStudio环境.我使用的是Ubuntu 12.04系统(由于此机器还要运行其他程序,为避免兼容性问题,暂未更新到最新,而继续沿用此稳定版),java和jdk ...
- HBase之四--(3):hbasehbase分页查询
为了广大技术爱好者学习netty,在这里帮新浪微博@nettying宣传下他出版的新书 <netty权威指南>@nettying兄在华为NIO实践多年,这本书是他的技术和经验的一个结晶.N ...
- Visual Studio Ultimate 2013 下载地址
VS2013_RTM_ULT_CHS.iso 文件大小:2.87G 百度网盘下载地址: http://pan.baidu.com/s/1bn4gavX 微软官网下载地址: http://downloa ...
- httpclient:Ip 代理
参考:http://blog.csdn.net/sdfiiiiii/article/details/70432060 http://blog.csdn.net/qy20115549/article/ ...
- git常见错误及解决方案总结
git常见错误及解决方案总结 使用git在本地创建一个项目的过程 $ makdir ~/hello-world //创建一个项目hello- ...
- 【Linux学习】Vi / Vim编辑器—编辑器工作模式、vi编辑操作
Vi / Vim编辑器-编辑器工作模式.vi编辑操作 推荐一个很好的学习指南:http://www.oschina.net/translate/learn-vim-progressively 一.编辑 ...
- Flutter实战视频-移动电商-47.详细页_Flutter_html插件的使用
47.详细页_Flutter_html插件的使用 详情里面是hemlt和图片组成的,但是flutter是不支持html的所以需要其他插件 flutter webview plugin:这个不太好用 f ...
- Django框架简介,wsgiref 与 jinja2 模块
目录 框架简介 wsgiref模块 jinja2 模块 框架简介 Django是一个web开发框架,用来开发web应用,本质就是, web框架+socket服务端 MVC框架和MTV框架 MVC,全名 ...