0
0
React Nativemobile~20 mins

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

Choose your learning style9 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.