Explore fundamental concepts of testing React Native applications using Jest for unit testing and Detox for end-to-end testing. This quiz is designed to help developers reinforce best practices and core knowledge for ensuring robust and reliable mobile apps.
Which method is commonly used to create a mock function in Jest while testing a React Native component?
Explanation: The correct method is jest.fn(), which creates a new mock function for tracking calls or custom implementations. jest.mock() is used for mocking modules, not individual functions. mockFunction() and createMock() are not standard Jest API methods, so using them would result in errors.
When writing a Jest test for a button component, which matcher checks if a function was called when the button was pressed?
Explanation: toHaveBeenCalled() verifies that a mock function was called during the test, which is useful for checking event handlers like button presses. toBeCalledWith() is not an actual Jest matcher, and isCalled() and functionCalled() are invalid in this context. Only toHaveBeenCalled() is correct.
Which command is typically used with Detox to initiate end-to-end tests on a React Native app?
Explanation: detox test is the standard command for starting Detox tests on a React Native application. jest start does not exist, as Jest uses the test command. runDetox() and testDetox() are not recognized commands in any testing scripts.
In Jest, what does snapshot testing help verify in a React Native component?
Explanation: Snapshot testing captures the output of a component and compares it to a stored version to detect unexpected changes. It does not check prop types (those are handled by type checking), navigation flow, or network requests. The correct purpose is to verify rendered output consistency.
What property should you assign to React Native components so Detox can locate them during tests?
Explanation: Assigning a testID property allows Detox to reliably find and interact with components during automated tests. idNumber and elementID are not recognized properties, and dataKey is unrelated. Only testID serves this essential testing function.
When testing asynchronous operations in Jest, which keyword allows your test function to wait for an async task to complete?
Explanation: Using await in combination with async functions lets the test wait for asynchronous operations to complete before progressing. waitFor is a helper for waiting on conditions but is not a keyword. pause and stop are not part of JavaScript syntax for managing async logic.
Which Detox method would you use to simulate a user tapping on a button?
Explanation: tap() is the Detox method for simulating a tap interaction with a UI element such as a button. click() and press() may seem correct but are not part of Detox's API, and push() is unrelated to UI interactions. Thus, tap() is the right choice.
Which configuration property helps Jest exclude specific files or folders from testing?
Explanation: Setting testPathIgnorePatterns lets Jest skip specified paths when running tests. skipFiles and excludeTests are not valid Jest properties, and ignoreDirectories does not exist. Among the options, only testPathIgnorePatterns fulfills this configuration role.
What does the Detox expectation .toBeVisible() check for on a given UI element?
Explanation: The expectation .toBeVisible() asserts that the specified UI component is actually seen on the device screen during the test. It does not verify existence in code (which might be .toExist()), clickability, or default enabled state. Only on-screen visibility is checked.
Which Jest method resets all information stored in mock functions between test cases?
Explanation: jest.clearAllMocks() clears call history and state from all mock functions, helping tests stay independent. jest.resetMocks() can reset mock implementations, but not call history specifically. clearMocks() and removeAllMocks() are not part of the Jest API.