Bird
Raised Fist0
React Nativemobile~20 mins

Jest setup for unit tests in React Native - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Jest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Jest configuration file syntax
Which of the following is the correct way to export a Jest configuration in a jest.config.js file for a React Native project?
Aexport default { preset: 'react-native' };
Bmodule.exports = { preset: 'react-native' };
Cexports = { preset: 'react-native' };
Dmodule.export = { preset: 'react-native' };
Attempts:
2 left
💡 Hint
Remember that Jest config files use CommonJS syntax by default.
❓ ui_behavior
intermediate
2:00remaining
Jest test output for a failing test
Given this simple React Native component test, what will Jest output if the test fails because the text is not found?
import React from 'react';
import { render } from '@testing-library/react-native';
import Greeting from './Greeting';

test('renders greeting text', () => {
  const { getByText } = render(<Greeting name="Alice" />);
  getByText('Hello Bob');
});
AThrows an error: Unable to find an element with text: Hello Bob
BTest passes silently with no output
CThrows a syntax error due to invalid JSX
DReturns null and test passes
Attempts:
2 left
💡 Hint
Think about what happens when getByText does not find the text.
❓ lifecycle
advanced
2:00remaining
Jest setup and teardown methods
Which Jest lifecycle method is best to use for cleaning up mocks after each test in a React Native test suite?
AbeforeEach(() => { jest.clearAllMocks(); });
BafterAll(() => { jest.clearAllMocks(); });
CafterEach(() => { jest.clearAllMocks(); });
DbeforeAll(() => { jest.clearAllMocks(); });
Attempts:
2 left
💡 Hint
Think about when you want to reset mocks to avoid test interference.
❓ navigation
advanced
2:00remaining
Mocking React Navigation in Jest tests
In a React Native app using React Navigation, which Jest mock setup correctly mocks the useNavigation hook to avoid errors in unit tests?
Ajest.mock('@react-navigation/native', () => ({ useNavigation: () => ({ navigate: jest.fn() }) }));
Bjest.mock('react-navigation', () => ({ useNavigation: () => ({ navigate: jest.fn() }) }));
Cjest.mock('@react-navigation/native', () => ({ useNavigation: jest.fn() }));
Djest.mock('@react-navigation/native', () => ({ useNavigation: () => 'navigate' }));
Attempts:
2 left
💡 Hint
Check the correct package name and the shape of the mocked hook.
🔧 Debug
expert
2:00remaining
Diagnosing Jest test timeout in React Native
A Jest test for a React Native component using async storage hangs and eventually times out. Which of the following is the most likely cause?
AThe test file is named incorrectly and Jest does not find it.
BThe component has a syntax error preventing it from rendering.
CJest configuration file is missing the preset 'react-native'.
DThe async storage mock is missing or not properly set up, causing unresolved promises.
Attempts:
2 left
💡 Hint
Think about what happens when async functions never resolve in tests.

Practice

(1/5)
1. What is the primary purpose of setting up Jest in a React Native project?
easy
A. To run automated tests and catch bugs early
B. To improve app performance on devices
C. To style components with CSS
D. To deploy the app to app stores

Solution

  1. Step 1: Understand Jest's role

    Jest is a testing framework used to run automated tests on code.
  2. Step 2: Identify its main benefit

    Running tests helps find bugs early before releasing the app.
  3. Final Answer:

    To run automated tests and catch bugs early -> Option A
  4. Quick 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
A. import { test, expect } from '@testing-library/react-native';
B. No import needed; Jest globals are available automatically
C. const { test, expect } = require('jest');
D. import { test, expect } from 'jest';

Solution

  1. Step 1: Recall Jest globals

    Jest provides test and expect as global functions by default.
  2. Step 2: Understand import necessity

    Because they are globals, you do not need to import them explicitly.
  3. Final Answer:

    No import needed; Jest globals are available automatically -> Option B
  4. Quick 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
A. Test fails because 1 + 2 is not 3
B. Test fails with a syntax error
C. Test passes successfully
D. Test throws a runtime error

Solution

  1. Step 1: Evaluate the expression inside expect

    1 + 2 equals 3, so expect(3).toBe(3) is true.
  2. Step 2: Understand Jest test behavior

    Since the expectation matches, the test passes without errors.
  3. Final Answer:

    Test passes successfully -> Option C
  4. Quick 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:
test('check true value', () => {
  expect(true).toBeTrue();
});

What is the likely cause?
medium
A. toBeTrue() is not a Jest matcher; use toBe(true) instead
B. Missing import of toBeTrue from Jest library
C. Syntax error due to missing semicolon after expect
D. Jest does not support boolean tests

Solution

  1. Step 1: Check Jest matchers for booleans

    Jest does not have a matcher called toBeTrue(); it uses toBe(true) instead.
  2. Step 2: Identify cause of syntax error

    Using an unknown matcher causes Jest to throw an error.
  3. Final Answer:

    toBeTrue() is not a Jest matcher; use toBe(true) instead -> Option A
  4. Quick 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
A. import { render } from '@testing-library/react-native'; test('button text', () => { const { getByText } = render(); expect(getByText('Click me')).toBeFalsy(); });
B. import { render } from 'react-native-testing-library'; test('button text', () => { const { getByText } = render(); expect(getByText('Click me')).toBeNull(); });
C. import { render } from '@testing-library/react-native'; test('button text', () => { const { getByText } = render(); expect(getByText('Click me')).toBeNull(); });
D. import { render } from '@testing-library/react-native'; test('button text', () => { const { getByText } = render(); expect(getByText('Click me')).toBeTruthy(); });

Solution

  1. Step 1: Use correct testing library import

    @testing-library/react-native is the recommended library for rendering components in Jest tests.
  2. Step 2: Check assertion correctness

    getByText returns the element if found; expect(...).toBeTruthy() confirms it exists.
  3. Final Answer:

    Correct import and expect(getByText('Click me')).toBeTruthy() -> Option D
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard