Jest helps you check if your React Native code works right. Setting it up means you can find mistakes early and fix them fast.
Jest setup for unit tests in React Native
Introduction
Syntax
React Native
npm install --save-dev jest @testing-library/react-native @testing-library/jest-native react-test-renderer // Add to package.json "scripts": { "test": "jest" } // Create jest.config.js module.exports = { preset: 'react-native', setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'], transformIgnorePatterns: [ 'node_modules/(?!(react-native|@react-native|@react-navigation)/)' ] };
Use npm install --save-dev to add testing tools only for development.
The jest.config.js file tells Jest how to work with React Native.
Examples
React Native
npm install --save-dev jest @testing-library/react-native @testing-library/jest-native react-test-renderer
npm test in the terminal.React Native
"scripts": { "test": "jest" }
React Native
module.exports = {
preset: 'react-native',
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
transformIgnorePatterns: [
'node_modules/(?!(react-native|@react-native|@react-navigation)/)'
]
};Sample App
This test checks if the button calls the function when pressed. It uses Jest and React Native Testing Library.
React Native
import React from 'react'; import { Text, TouchableOpacity } from 'react-native'; import { render, fireEvent } from '@testing-library/react-native'; function Button({ onPress, title }) { return ( <TouchableOpacity onPress={onPress} accessibilityLabel="button"> <Text>{title}</Text> </TouchableOpacity> ); } test('Button calls onPress when pressed', () => { const onPressMock = jest.fn(); const { getByA11yLabel } = render(<Button onPress={onPressMock} title="Click me" />); const button = getByA11yLabel('button'); fireEvent.press(button); expect(onPressMock).toHaveBeenCalledTimes(1); });
Important Notes
Always add accessibilityLabel to components to find them easily in tests.
Run npm test to start Jest and see test results.
Keep tests small and focused on one thing at a time.
Summary
Jest helps catch bugs early by running tests on your React Native code.
Install Jest and testing libraries, then add a config file to set it up.
Write simple tests to check if components behave as expected.
Practice
1. What is the primary purpose of setting up Jest in a React Native project?
easy
Solution
Step 1: Understand Jest's role
Jest is a testing framework used to run automated tests on code.Step 2: Identify its main benefit
Running tests helps find bugs early before releasing the app.Final Answer:
To run automated tests and catch bugs early -> Option AQuick Check:
Jest purpose = testing and bug catching [OK]
Hint: Jest is for testing code, not styling or deployment [OK]
Common Mistakes:
- Confusing Jest with styling tools
- Thinking Jest improves app speed
- Assuming Jest deploys apps
2. Which of the following is the correct way to import Jest functions in a React Native test file?
easy
Solution
Step 1: Recall Jest globals
Jest provides test and expect as global functions by default.Step 2: Understand import necessity
Because they are globals, you do not need to import them explicitly.Final Answer:
No import needed; Jest globals are available automatically -> Option BQuick Check:
Jest globals = no import needed [OK]
Hint: Jest test and expect are global, no import required [OK]
Common Mistakes:
- Trying to import test and expect explicitly
- Confusing Jest with other libraries needing imports
- Using require instead of relying on globals
3. Given this test code snippet, what will be the test result?
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});medium
Solution
Step 1: Evaluate the expression inside expect
1 + 2 equals 3, so expect(3).toBe(3) is true.Step 2: Understand Jest test behavior
Since the expectation matches, the test passes without errors.Final Answer:
Test passes successfully -> Option CQuick Check:
1 + 2 = 3, test passes [OK]
Hint: Check math inside expect; if true, test passes [OK]
Common Mistakes:
- Thinking 1 + 2 is not 3
- Confusing syntax errors with logic errors
- Assuming runtime errors without cause
4. You wrote this test but it fails with a syntax error:
What is the likely cause?
test('check true value', () => {
expect(true).toBeTrue();
});What is the likely cause?
medium
Solution
Step 1: Check Jest matchers for booleans
Jest does not have a matcher called toBeTrue(); it uses toBe(true) instead.Step 2: Identify cause of syntax error
Using an unknown matcher causes Jest to throw an error.Final Answer:
toBeTrue() is not a Jest matcher; use toBe(true) instead -> Option AQuick Check:
Use toBe(true), not toBeTrue() [OK]
Hint: Use toBe(true) for boolean checks, not toBeTrue() [OK]
Common Mistakes:
- Trying to import non-existent matchers
- Assuming semicolon causes syntax error
- Believing Jest can't test booleans
5. You want to test a React Native component that renders a button with text 'Click me'. Which setup is correct to write a Jest test using @testing-library/react-native?
hard
Solution
Step 1: Use correct testing library import
@testing-library/react-native is the recommended library for rendering components in Jest tests.Step 2: Check assertion correctness
getByText returns the element if found; expect(...).toBeTruthy() confirms it exists.Final Answer:
Correct import and expect(getByText('Click me')).toBeTruthy() -> Option DQuick Check:
Render + getByText + toBeTruthy() = correct test [OK]
Hint: Use getByText and expect(...).toBeTruthy() to check rendered text [OK]
Common Mistakes:
- Using wrong testing library import
- Expecting getByText to return null when text exists
- Using toBeFalsy() instead of toBeTruthy() for found elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
