Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

react-testing does not print name of component wrapped in memo correctly #1237

@BPScott

Description

@BPScott

Overview

I was recently testing some stuff in web when I came across a case where react-testing was not returning the name of a component correctly and was instead falling back to the default of "Component"

 ● <FileInsuranceClaim /> › on save › with errors › displays server error toast and restore submit button if server error occurs

    expect(received).toContainReactComponent(expected)

    Expected the React element:
      <Async(FileInsuranceClaim) />
    To contain component:
      <Component />
    But no matching <Component /> elements were found.

      1438 |         await submit(fileInsuranceClaim);
      1439 | 
    > 1440 |         expect(fileInsuranceClaim).toContainReactComponent(Toast);

I would have expected that to say:

To contain component:
      <Toast />

I believe this is because this Toast component in question is the one from polaris which is a Component wrapped in a React.memo, rather than a plain old functional component.

I suspect this is because the printType utility does not correctly handle finding the name for components that are wrapped in React.memo(). I'd wager components wrapped in React.forwardRef() are also affected in the same way. You should also test components that are do React.memo(React.forwardRef(function SomeComponent() { return 'hi'; }))

facebook/react#17274 from the react repo might be some useful related reading as it fixes up some logic around how names are extracted from these exotic components in the react dev tools.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions