React Native Testing Essentials: Jest u0026 Detox Quiz Quiz

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.

  1. Jest: Mock Functions Basics

    Which method is commonly used to create a mock function in Jest while testing a React Native component?

    1. mockFunction()
    2. jest.mock()
    3. jest.fn()
    4. createMock()

    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.

  2. Testing Outputs with Jest

    When writing a Jest test for a button component, which matcher checks if a function was called when the button was pressed?

    1. toBeCalledWith()
    2. isCalled()
    3. toHaveBeenCalled()
    4. functionCalled()

    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.

  3. Detox: Launching the App

    Which command is typically used with Detox to initiate end-to-end tests on a React Native app?

    1. testDetox()
    2. runDetox()
    3. detox test
    4. jest start

    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.

  4. Snapshot Testing with Jest

    In Jest, what does snapshot testing help verify in a React Native component?

    1. Network request success
    2. The rendered output matches the stored snapshot
    3. The app navigation flow
    4. The component prop types

    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.

  5. Detox: Selecting Elements

    What property should you assign to React Native components so Detox can locate them during tests?

    1. idNumber
    2. testID
    3. dataKey
    4. elementID

    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.

  6. Jest: Handling Asynchronous Code

    When testing asynchronous operations in Jest, which keyword allows your test function to wait for an async task to complete?

    1. await
    2. pause
    3. stop
    4. waitFor

    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.

  7. Detox: Simulating User Actions

    Which Detox method would you use to simulate a user tapping on a button?

    1. tap()
    2. click()
    3. press()
    4. push()

    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.

  8. Jest: Ignoring Files in Testing

    Which configuration property helps Jest exclude specific files or folders from testing?

    1. ignoreDirectories
    2. excludeTests
    3. testPathIgnorePatterns
    4. skipFiles

    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.

  9. Detox: Expectation Syntax

    What does the Detox expectation .toBeVisible() check for on a given UI element?

    1. That the element exists in the code
    2. That the element is clickable
    3. That the element is currently visible on the screen
    4. That the element is enabled by default

    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.

  10. Jest: Resetting Mocks

    Which Jest method resets all information stored in mock functions between test cases?

    1. removeAllMocks()
    2. clearMocks()
    3. jest.clearAllMocks()
    4. jest.resetMocks()

    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.