FG
💻 Software🌐 Web & Full-Stack

Express more tests via public API

Fresh5 days ago
Mar 14, 20260 views
Confidence Score88%
88%

Problem

This is a great contribution opportunity. We need to rewrite more unit tests in terms of public API. This means that they can only import npm entry points like `react`, `react-dom`, `react-dom/test-utils`, `react-test-renderer`, etc, but not internal modules like `SyntheticEvent` or `ReactDOMComponentTree`. The “bad” requires are already marked with a TODO in tests so you won’t miss them. To help with this: 1. Find `// TODO: can we express this test with only public API?` in the unclaimed test files below. 2. Comment in this issue if you want to take a particular unit test file, with its name. 3. Submit a PR that rewrites the test to use public APIs instead. Step 3 requires some thinking. You can use previous examples where we rewrote tests with public API for inspiration. For example: https://github.com/facebook/react/pull/10429 https://github.com/facebook/react/pull/10281 https://github.com/facebook/react/pull/9080 https://github.com/facebook/react/pull/8148 Generally, you need to think about how the behavior you’re testing actually reproduces in a React app, and then test for that. In rare cases it may involve exposing some API as public which we’ll need to discuss separately, so don’t hesitate to start a discussion! If you can’t figure out how to rewrite some particular test with a public API, comment here and we can brainstorm. Here is the full list of tests that need to change. Some of them may be simple one-liner changes, some may involve a bit of a rewrite,

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Refactor Unit Tests to Use Public API

Medium Risk

Unit tests currently rely on internal modules instead of the public API, which limits their accessibility and maintainability. This makes it difficult for contributors to understand and modify tests, as well as potentially leading to issues when internal implementations change.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Identify Tests with TODO Comments

    Search through the unclaimed test files for the comment '// TODO: can we express this test with only public API?'. This will help you pinpoint the tests that need to be rewritten.

    bash
    grep -r '// TODO: can we express this test with only public API?' ./tests/
  2. 2

    Choose a Test File to Refactor

    Once you have identified the tests, comment on this issue with the name of the test file you wish to refactor. Ensure that you are not duplicating efforts by checking existing comments.

  3. 3

    Rewrite the Test Using Public API

    Refactor the identified test to use only public APIs. Use previous examples as references to understand how to structure your tests. Ensure that the behavior being tested is accurately represented in a way that mimics real-world usage of the React components.

    javascript
    import { render } from 'react-dom/test-utils';
    import MyComponent from './MyComponent';
    
    test('renders correctly', () => {
      const { getByText } = render(<MyComponent />);
      expect(getByText('Hello World')).toBeInTheDocument();
    });
  4. 4

    Run Tests to Validate Changes

    After refactoring the tests, run the test suite to ensure that all tests pass and that the refactored tests function as expected. This will confirm that the changes did not introduce any regressions.

    bash
    npm test
  5. 5

    Submit a Pull Request

    Once you have validated that the tests work correctly, submit a pull request with your changes. Include a description of what you changed and why, referencing the original TODO comments.

Validation

To confirm the fix worked, ensure that all tests pass after the refactor and that the tests are now using only public APIs. Review the test output to check for any errors or warnings related to the changes made.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

reactjavascriptdifficulty:-mediumgood-first-issue-(taken)