@moxy/jest-config-react-native

MOXY's Jest configuration enhancer for React Native apps

Usage no npm install needed!

<script type="module">
  import moxyJestConfigReactNative from 'https://cdn.skypack.dev/@moxy/jest-config-react-native';
</script>

README

jest-config-react-native

NPM version Downloads Dependency status Dev Dependency status

MOXY's Jest configuration enhancer for React Native apps.

⚠️ This enhancer requires you to have react-native installed in your project.

Installation

$ npm install --save-dev jest @moxy/jest-config-base @moxy/jest-config-react-native

This package should be used in conjunction with @moxy/jest-config-base.

What's included?

  • React Native Jest preset: Adds react-native/jest-preset to configure the environment.
  • Transform: Allows importing common files used in React Native projects, such as fonts, images and videos.
  • React Native mocks: Supress warnings and errors when trying to access native functionality.

Usage

To use this enhancer, use the compose function that comes with this package. Keep in mind, the first item should always be the base configuration!

'use strict';

const { compose, baseConfig } = require('@moxy/jest-config-base');
const withReactNative = require('@moxy/jest-config-react-native');

module.exports = compose(
    baseConfig('node'),
    withReactNative(),
);

The enhancer accepts an options object as the first argument with the following keys:

  • transformModules: A function that allows you to add (or remove) modules to be transformed by Babel. It has the following signature (patterns) => newPatterns, where the default patterns are ['(jest-)?react-native(-.*)?/', '@react-native-community/'].

To showcase the usefulness of transformModules, let's pretend that you use React Navigation in your project. You would need to compile its packages to make them work in Jest, so you would add them to transformModules like so:

'use strict';

const { compose, baseConfig } = require('@moxy/jest-config');
const withReactNative = require('@moxy/jest-config-react-native');

module.exports = compose(
    baseConfig('node'),
    withReactNative({
        transformModules: (patterns) => [...patterns, '@react-navigation/'],
    }),
);