Migrate to @testing-library/react-native #77
Migrate to @testing-library/react-native #77mgcrea wants to merge 1 commit intoreact-native-community:mainfrom
Conversation
|
👋 As a maintainer of the @testing-library/react-native (RNTL) package, I'd like to provide some insights. Although the React Test Renderer is deprecated and won't receive further updates, it still functions correctly with React 18.3 and React 19, despite the deprecation warnings. Currently, RNTL v12 (current stable version) and the upcoming RNTL v13 (expected in Q4 2024) will continue to use React Test Renderer as a peer dependency. If you can add Looking ahead, RNTL v14 (planned for Q1 2025) will adopt a new custom test renderer that I'm developing, called uniersal-test-renderer. This new renderer is in advanced stage and passes all internal RNTL. I soon plan to release RNTL v14 alpha with it for community testing. Here's an action plan I would recommend:
|
| @@ -1,3 +1,4 @@ | |||
| module.exports = { | |||
| preset: 'react-native', | |||
| setupFilesAfterEnv: ["@testing-library/react-native/extend-expect"], | |||
There was a problem hiding this comment.
I'd rather recommend creating jest-setup.ts file and putting the import "@testing-library/react-native/extend-expect" there. With typical RN app users quickly find need to add additional global mocks for popular community libraries.
|
|
||
| import React from 'react'; | ||
| import ReactTestRenderer from 'react-test-renderer'; | ||
| import { render } from "@testing-library/react-native"; |
There was a problem hiding this comment.
| import { render } from "@testing-library/react-native"; | |
| import { render, screen } from "@testing-library/react-native"; |
| await ReactTestRenderer.act(() => { | ||
| ReactTestRenderer.create(<App />); | ||
| }); | ||
| render(<App />); |
There was a problem hiding this comment.
| render(<App />); | |
| render(<App />); | |
| expect(screen.getByText('Read the docs to discover what to do next')).toBeOnTheScreen(); |
There was a problem hiding this comment.
All Jest test should contain some expect assertions. A typical one is that a certain element is rendered on the screen. I've picked a sample text from App.tsx here.
| import { render } from "@testing-library/react-native"; | ||
| import App from '../App'; | ||
|
|
||
| test('renders correctly', async () => { |
There was a problem hiding this comment.
This test does not contain async code, so we can safely remove async keyword
| test('renders correctly', async () => { | |
| test('renders correctly', () => { |
| "eslint": "^8.19.0", | ||
| "jest": "^29.6.3", | ||
| "prettier": "2.8.8", | ||
| "react-test-renderer": "19.0.0-rc-fb9a90fa48-20240614", |
There was a problem hiding this comment.
You should not remove react-test-renderer as it's required by RNTL. If this works, then only through package manager automatically installing react-test-renderer as a peer dep for RNTL. The risk here is that it will pick the wrong version of the package, and react and react-test-renderer versions need to match.
Summary:
Migrate to @testing-library/react-native since react-test-renderer is deprecated
Changelog:
GENERAL CHANGED - Migrate to @testing-library/react-native
Test Plan: