Behavior of formState is different when running on react native app vs when running on jest test
See original GitHub issueDescribe the bug
Behavior of formState
is different when running on react native app vs when running on jest test. Reproduced in version 6.3.0
.
To Reproduce Steps to reproduce the behavior:
- Create a React Native project.
- Install
react-hook-form
,@testing-library/react-native
, and@testing-library/jest-native
. - Copy example form implementation into
App.js
. - Change
useForm
to useonChange
mode.
const {control, handleSubmit, errors, formState} = useForm({
mode: 'onChange',
});
- Add
<Text>
to display current form state.
<Text>
{formState.isValid ? 'formState: "Valid"' : 'formState: "Invalid"'}
</Text>
- Disable
<Button>
if formState is not valid
<Button
disabled={!formState.isValid}
accessibilityLabel="Submit Button"
title="Submit"
onPress={handleSubmit(onSubmit)}
/>
- Replace
__tests__/App-test.js
with the below snippet.
import React from 'react';
import {render} from '@testing-library/react-native';
import App from '../App';
describe('checking formState.isValid', () => {
it('should initialize with an invalid state', () => {
const {getByText} = render(<App />);
getByText('formState: "Invalid"');
});
it('should have a disabled submit button', () => {
const {getByA11yLabel} = render(<App />);
const submitButton = getByA11yLabel('Submit Button');
expect(submitButton).toBeDisabled();
});
});
- Run test
yarn test
Github link As I encountered this issue in a React Native project, I’m providing a link to the template repo created to reproduce the error.
Expected behavior
Both app and jest test should start off with formState.isValid
as false
. However, jest test starts off with formState.isValid
as true
.
Screenshots Below is what happens in the react native app
Below is what happens in the jest test
Desktop (please complete the following information):
- N/A as this is an issue in React Native
Smartphone (please complete the following information):
- Device: iPhone 11 Pro Max Simulator
- OS: iOS 13.6
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Testing React Native apps with Jest | by Emily Xiong - Nx Blog
In my previous blog, I finally finished writing my awesome Nx React Native App. The app is running fine. Can I just commit...
Read more >Why is the behavior of `jest.useFakeTimers` different when ...
Overview. I encountered different behaviors of jest.useFakeTimers when it is called inside beforeEach versus called outside.
Read more >How to Test React Components in TypeScript | Pluralsight
In this guide, you'll learn how to get started quickly writing tests using TypeScript, React, and Jest in an idiomatic way. There are...
Read more >An in-depth beginner's guide to testing React applications in ...
Find out what to test and which testing framework to use. Learn a clear approach by writing tests for an extensive example app...
Read more >Test-Driven Development with React, Jest, and Enzyme - Part 1
Each component will have a separate set of tests housed in a ... Create React App runs the setupTests.js file before each test, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It works now 🎉🎉🎉 My apologies for missing out on the documentation.
For anyone who comes across this, add
setup.js
in the root directory with the following snippet:add
"<rootDir>/setup.js"
topackage.json
like this:Have updated the repo with the changes as well.
Thank you @keiya01 for your help!
I think this issue is caused by async function. We are updating
formState.isValid
by async function. Therefore you should wrapgetByText
withwait
method in@testing-library/react-native
. (link: https://www.native-testing-library.com/docs/api-async)