在网上找了很久针对react-native的测试方法,可是没有找到靠谱的方式。要么很浅只是跑了一下官方的例子,要么就是版本有点老旧,照着没法进行。jest提供的react-native例子不多,而enzyme提供的react-native-mock库也是各类报错,让人非常绝望。因而乎在搜索到的信息指引下,通过本身的尝试,我总结出了下面的测试方法,可是不是很满意,若是哪位大神有更好的方式,恳请留个言指明一下方向。javascript
react官方内置了jest做为单元测试的工具,再搭配上enzyme就能完美地开展测试工做。可是当测试的对象变成react-native时,测试工做就变得异常艰难。艰难的地方主要有如下几点:java
jest.mock('react-native-go', ()=>{ return { to: ()=>{} } });
'use strict'; /* eslint-env node */ const path = require('path'); const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction'); module.exports = { // Mocks asset requires to return the filename. Makes it possible to test that // the correct images are loaded for components. Essentially // require('img1.png') becomes `Object { "testUri": 'path/to/img1.png' }` in // the Jest snapshot. process: (_, filename) => `module.exports = { testUri: ${JSON.stringify(path.relative(__dirname, filename))} };`, getCacheKey: createCacheKeyFunction([__filename]), };
"devDependencies": { "enzyme": "^3.3.0", "enzyme-adapter-react-16": "^1.1.1", "jest": "^21.2.1", "react-dom": "^16.2.0" },
二、编写.babelrcnode
{ "presets": ["react-native"] }
三、jest配置react
"scripts": { "test": "jest" }, "jest": { "preset": "react-native", "transform": { "\\.(jpg|jpeg|png|webp|gif|svg|wav|mp3|mp4|m4a|aac)$": "<rootDir>/node_modules/react-native/jest/assetFileTransformer.js" } }
先写一个我要测试的组件web
import React, {Component} from 'react'; import { View } from 'react-native'; //工具方法包含获取数据请求send let Core = require('./core'); export default class AboutUsPage extends Component<{}>{ constructor(props){ super(props); if(typeof this.props.getThis === 'function'){ this.props.getThis.call(this); if (this.props.testCore) { Core = this.props.testCore; } } } async componentWillMount(){ this.setState({ name: await this.firstStep() }) } async firstStep(){ return await this.secondStep(); } async secondStep(){ return await Core.send('/getData'); } render(){ return ( <View></View> ) } }
core文件json
let Core = { async send() {//请求异步数据,返回promise ... } }; module.exports = Core;
testCore文件,暴露两个函数,一个send用以调用数据,一个setSendData用以设置要返回的数据react-native
"use strict"; let caches = { }; let currentRequest = {}; let Core = { async setSendData(key, status, data) { caches[key] = { status, data }; }, async send(key){ let res = caches[key]; if(res.status){ return Promise.resolve(res.data); }else{ return Promise.reject(res.data); } } }; module.exports = Core;
test.js测试文件promise
'use strict'; import React from 'react'; import AboutUsPage from './AboutUsPage'; import {configure, shallow} from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import testCore from './test.core'; configure({ adapter: new Adapter() }); testCore.setSendData('getData', true, 'aaa'); describe('AboutUsPage', () => { let component; let wrapper; wrapper = shallow(<AboutUsPage getThis={function(){component=this;}} testCore={testCore}/>); wrapper.setState({ name: 'tom' }); it('renders correctly', async () => { await component.componentWillMount(); expect(wrapper.state().name).toBe('aaa'); }); });