1.引入react-addons-test-utils 2.引入斷言器 chai 3.安裝mocha 4.運(yùn)行測(cè)試(mocha –compilers js:babel-core/register ) 需要配置mocha 支持es6及jsx,mocha 自動(dòng)執(zhí)行test文件夾下的所有測(cè)試用例
babel配置
{ "參考文檔:http://www.ruanyifeng.com/blog/2016/02/react-testing-tutorial.html http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html 最簡(jiǎn)單demoimport TestUtils from 'react-addons-test-utils';import { expect } from 'chai';import React,{Component} from 'react';import ReactDOM from 'react-dom';class App extends Component { render() { return <h1>test</h1> }}function shallowRender(Component) { const renderer = TestUtils.createRenderer(); renderer.render(<Component/>); return renderer.getRenderOutput();}describe('Shallow Rendering', function () { it('App/'s title should be Todos', function () { const app = shallowRender(App); expect(app.props.children[0].type).to.equal('h1'); expect(app.props.children[0].props.children).to.equal('test'); });});新聞熱點(diǎn)
疑難解答
圖片精選