阅读以下系列文章来深入了解如何使用 Jest 测试一个真实的 React Native 示例应用: 和 第二篇: Jest – Redux Snapshots for your Actions and Reducers.

Starting from react-native version 0.38, a Jest setup is included by default when running . The following configuration should be automatically added to your package.json file:

Note: If you are upgrading your react-native application and previously used the jest-react-native preset, remove the dependency from your package.json file and change the preset to react-native instead.

yarn test 来运行 Jest 测试。

下面来为一个入门的小型组件创建一个,它的内部有些View、Text组件和一些样式:

  1. // Intro.js
  2. import React, {Component} from 'react';
  3. import {StyleSheet, Text, View} from 'react-native';
  4. const styles = StyleSheet.create({
  5. container: {
  6. alignItems: 'center',
  7. backgroundColor: '#F5FCFF',
  8. flex: 1,
  9. justifyContent: 'center',
  10. },
  11. instructions: {
  12. color: '#333333',
  13. marginBottom: 5,
  14. textAlign: 'center',
  15. },
  16. fontSize: 20,
  17. margin: 10,
  18. textAlign: 'center',
  19. },
  20. });
  21. render() {
  22. return (
  23. <View style={styles.container}>
  24. <Text style={styles.welcome}>Welcome to React Native!</Text>
  25. <Text style={styles.instructions}>
  26. 这是一个 React Native 快照测试。
  27. </Text>
  28. </View>
  29. );
  30. }
  31. }

现在,使用React的test renderer和Jest的快照特性来和组件交互,获得渲染结果和生成快照文件:

  1. // __tests__/Intro-test.js
  2. import React from 'react';
  3. import Intro from '../Intro';
  4. import renderer from 'react-test-renderer';
  5. const tree = renderer.create(<Intro />).toJSON();
  6. expect(tree).toMatchSnapshot();
  7. });

下次你运行测试时,渲染的结果将会和之前创建的快照进行比较。 代码变动时快照必须按顺序提交。 当快照测试失败,你需要去检查是否是你想要或不想要的变动。 如果变动符合预期,你可以通过jest -u调用Jest从而重写存在的快照。

示例代码可在 examples/enzyme 找到。

The preset sets up the environment and is very opinionated and based on what we found to be useful at Facebook. All of the configuration options can be overwritten just as they can be customized when no preset is used.

react-native ships with a Jest preset, so the jest.preset field of your package.json should point to . The preset is a node environment that mimics the environment of a React Native app. Because it doesn't load any DOM or browser APIs, it greatly improves Jest's startup time.

选项可用于创建用于 Babel 的黑白名单。 Many react-native npm modules unfortunately don't pre-compile their source code before publishing.

By default the jest-react-native preset only processes the project's own source files and react-native. If you have npm dependencies that have to be transformed you can customize this configuration option by whitelisting modules other than react-native:

  1. "transformIgnorePatterns": [
  2. "node_modules/(?!(react-native|my-project|react-native-button)/)"
  3. ]

可以使用 modulenamemapper 将模块路径映射到其他模块。 By default the preset maps all images to an image stub module but if a module cannot be found this configuration option can help:

  1. "moduleNameMapper": {
  2. "my-module.js": "<rootDir>/path/to/my-module.js"
  3. }

The Jest preset built into react-native comes with a few default mocks that are applied on a react-native repository. However some react-native components or third party components rely on native code to be rendered. In such cases, Jest's manual mocking system can help to mock out the underlying implementation.

For example, if your code depends on a third party native video component called react-native-video you might want to stub it out with a manual mock like this:

This will render the component as <Video {…props} /> with all of its props in the snapshot output. 想了解更多还可以阅读 .

Sometimes you need to provide a more complex manual mock. For example if you'd like to forward the prop types or static fields of a native component to a mock, you can return a different React component from a mock through this helper from jest-react-native:

  1. jest.mock('path/to/MyNativeComponent', () => {
  2. const mockComponent = require('react-native/jest/mockComponent');
  3. return mockComponent('path/to/MyNativeComponent');
  4. });

Or if you'd like to create your own manual mock, you can do something like this:

  1. jest.mock('Text', () => {
  2. const RealComponent = jest.requireActual('Text');
  3. const React = require('React');
  4. class Text extends React.Component {
  5. render() {
  6. return React.createElement('Text', this.props, this.props.children);
  7. }
  8. }
  9. Text.propTypes = RealComponent.propTypes;
  10. return Text;
  11. });

If you end up mocking the same modules over and over it is recommended to define these mocks in a separate file and add it to the list of .