e2e Testing Custom React Components with Detox

Josiah T Mahachi
2 min readOct 6, 2023

--

Testing Custom React Components with Detox

In the realm of React Native, testing components remains a crucial aspect of the development process. While tools like Jest combined with react-test-renderer have been standard for a while, the real challenge surfaces when we get into end-to-end (E2E) testing with tools like Detox. How do you target specific components or parts of a component effectively?

Drawing inspiration from an insightful article by Shane Burgess on Medium, let’s explore the art of testing custom React Native components using Detox, focusing on a component named AppIcon.

Why the testID Prop?

The testID prop in React Native serves as an accessible identifier, primarily for automation testing. Unlike searching components by type, which is tied to implementation details, testID provides a stable identifier that's immune to refactoring or renaming components.

Structuring testID in Custom Components

A recommended approach, which aligns with Shane’s method, involves appending a postfix to the testID to denote child components or specific elements.

Consider the custom component AppIcon:

import React from 'react';
import { View, Image } from 'react-native';

const AppIcon = ({ testID, source, label }) => {
return (
<View testID={`${testID}-container`}>
<Image source={source} testID={`${testID}-image`} />
<Text testID={`${testID}-label`}>{label}</Text>
</View>
);
};

By establishing this structure, we have the ability to:

  • Target the entire container: ${testID}-container
  • Target the image: ${testID}-image
  • Target the label: ${testID}-label

E2E Testing with Detox

With our testID structure in place, let's see how we can use Detox to perform E2E testing:

  1. Setting Up Detox:

Ensure that Detox is set up correctly with your React Native project. Follow the official Detox documentation for setup guidelines.

2. Writing Tests:

After initializing Detox, let’s write tests targeting our AppIcon component:

describe('AppIcon Component', () => {

it('should display the icon image', async () => {
await waitFor(element(by.id('appIcon-image'))).toBeVisible().withTimeout(2000);
});

it('should display the icon label', async () => {
await expect(element(by.id('appIcon-label'))).toBeVisible();
});

// You can add more tests like tapping the icon, checking props, etc.
});

…in conclusion

E2E testing in React Native with Detox becomes much more manageable and efficient when adopting a consistent testID naming convention. By following a structured approach, as inspired by Shane Burgess's article, we ensure that our tests remain resilient to refactoring and are clear for both developers and QA teams.

If you’re diving into React Native testing, consider the testID approach. It not only simplifies the process but also ensures your tests are robust and maintainable in the long run.

--

--

Josiah T Mahachi

Full-stack Developer, C#, ASP.Net, Laravel, React Native, PHP, Azure Dev Ops, MSSQL, MySQL