| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
rwft3131
9年前发布

React项目搭建基于Karma的CI环境

来自: http://www.cnblogs.com/E-WALKER/p/5178358.html

简介

在浏览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文件。

结果如下

有问题欢迎讨论 :)

</div>

 本文由用户 rwft3131 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1454461090292.html
React 持续集成系统 webpack