--- title: Unit testing description: Learn how to set up and configure the jest-expo package to write unit tests and snapshot tests for a project. --- import { Terminal } from '~/ui/components/Snippet'; import { BoxLink } from '~/ui/components/BoxLink'; import { FileTree } from '~/ui/components/FileTree'; import { Tabs, Tab } from '~/ui/components/Tabs'; [Jest](https://jestjs.io) is the most widely used JavaScript unit testing framework. In this guide, you'll learn how to set up Jest in your project, write a unit test, write a snapshot test, and best practices for structuring your tests when using Jest with React Native. You'll also use the `jest-expo` package which is a Jest preset and mocks the native part of the Expo SDK and handles most of the configuration. ## Installation To install `jest-expo` in your project, run the following command: > **info** If you are using TypeScript, then also install `@types/jest` as a dev dependency. Then, update **package.json** to add a script for running tests and add the preset for using the base configuration from `jest-expo`: {/* prettier-ignore */} ```json package.json "scripts": { /* @hide ... */ /* @end */ "test": "jest" }, /* @hide ... */ /* @end */ "jest": { "preset": "jest-expo" } ``` ## Configuration A starting configuration you can use is to make sure any modules you are using within the **node_modules** directory are transpiled when running Jest. This can be done by including the [`transformIgnorePatterns`](https://jestjs.io/docs/configuration#transformignorepatterns-arraystring) property that takes a regex pattern as its value: ```json package.json "jest": { "preset": "jest-expo", "transformIgnorePatterns": [ "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)" ] } ``` ```json package.json "jest": { "preset": "jest-expo", "transformIgnorePatterns": [ "node_modules/(?!(?:.pnpm/)?((jest-)?react-native|@react-native(-community)?|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg))" ] } ``` Jest has various configuration options. The above configuration covers the majority of your needs. However, you can always add to this pattern list. For more details, see [Configuring Jest](https://jestjs.io/docs/configuration). ## Unit test A unit test is used to check the smallest unit of code, usually a function. To write your first unit test, start by writing a simple test for **App.js**. Create a test file for it and call it **App.test.js**. Jest identifies a file with the **.test.js** extension as a test and includes it in the tests queue. There are also other ways to [structure a test](#structure-your-tests). The test will expect the state of the `` component to have one child element: ```js App.test.js import React from 'react'; import renderer from 'react-test-renderer'; import App from './App'; describe('', () => { it('has 1 child', () => { const tree = renderer.create().toJSON(); expect(tree.children.length).toBe(1); }); }); ``` > **info** If you are using TypeScript, use **.ts** or **.tsx** file extension. To run the test: If everything goes well, you should see the one test passed. For more information, see [expect and conditional matchers](https://jestjs.io/docs/en/expect). ## Structure your tests Right now, you have a single test file in the project directory. Adding more test files can make it hard to organize your project directory. The easiest way to avoid this is to create a **\_\_tests\_\_** directory and put all your tests inside it. An example structure is shown below: However, this approach causes a lot of long import paths, such as `../../src/components/button`. Alternatively, you can have multiple **\_\_tests\_\_** sub-directories for different areas of your project. For example, create a separate test directory for **components**, **navigation**, and so on: Now, if you move **\_\_tests\_\_** within the **components** directory, the import path of `