在網上找了很久針對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'); }); });