menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

How to tes...
source image

Dev

4w

read

419

img
dot

Image Credit: Dev

How to test if a mocked component receives the correct props in Jest and RTL (new since React 19)

  • To test if a mocked component receives the correct props in Jest and RTL (since React 19), the approach has changed.
  • Initially, set up a simple example with a component that takes an array of names, calling a component.
  • In testing, mock the component and verify its invocation to ensure proper isolation.
  • When testing props, a new test checks if was called with the correct props in React 19.
  • In React 19, the second argument is now required to be undefined while it used to be an empty object {} pre-React 19.
  • A practical example demonstrates the usage of expect.objectContaining to accurately validate props.
  • An improvement suggestion is to utilize expect.objectContaining for testing specific props while allowing flexibility for other properties.
  • Understanding the changes in React 19 regarding the second argument for prop validation in mocked components is vital for accurate testing.
  • Ensure tests are updated post-React 19 to align with the new requirement of using undefined as the second argument for prop validation.
  • The article emphasizes the importance of adapting testing approaches to changes in React to maintain accurate component testing.

Read Full Article

like

25 Likes

For uninterrupted reading, download the app