-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate to @testing-library/react-native #77
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -3,11 +3,9 @@ | |||
*/ | |||
|
|||
import React from 'react'; | |||
import ReactTestRenderer from 'react-test-renderer'; | |||
import { render } from "@testing-library/react-native"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
render(<App />); | |
render(<App />); | |
expect(screen.getByText('Read the docs to discover what to do next')).toBeOnTheScreen(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -3,11 +3,9 @@ | |||
*/ | |||
|
|||
import React from 'react'; | |||
import ReactTestRenderer from 'react-test-renderer'; | |||
import { render } from "@testing-library/react-native"; | |||
import App from '../App'; | |||
|
|||
test('renders correctly', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test does not contain async code, so we can safely remove async keyword
test('renders correctly', async () => { | |
test('renders correctly', () => { |
"@types/jest": "^29.5.13", | ||
"@types/react": "^18.2.6", | ||
"@types/react-test-renderer": "^18.0.0", | ||
"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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: