question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Behavior of formState is different when running on react native app vs when running on jest test

See original GitHub issue

Describe 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:

  1. Create a React Native project.
  2. Install react-hook-form, @testing-library/react-native, and @testing-library/jest-native.
  3. Copy example form implementation into App.js.
  4. Change useForm to use onChange mode.
const {control, handleSubmit, errors, formState} = useForm({
  mode: 'onChange',
});
  1. Add <Text> to display current form state.
<Text>
  {formState.isValid ? 'formState: "Valid"' : 'formState: "Invalid"'}
</Text>
  1. Disable <Button> if formState is not valid
<Button
  disabled={!formState.isValid}
  accessibilityLabel="Submit Button"
  title="Submit"
  onPress={handleSubmit(onSubmit)}
/>
  1. 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();
  });
});

  1. 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 formState demo

Below is what happens in the jest test formState 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:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

10reactions
tingzhouucommented, Aug 6, 2020

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:

global.window = {};
global.window = global;

add "<rootDir>/setup.js" to package.json like this:

"jest": {
  "preset": "react-native",
  "setupFilesAfterEnv": [
    "@testing-library/jest-native/extend-expect",
    "<rootDir>/setup.js"
  ]
}

Have updated the repo with the changes as well.

Thank you @keiya01 for your help!

3reactions
keiya01commented, Aug 5, 2020

I think this issue is caused by async function. We are updating formState.isValid by async function. Therefore you should wrap getByText with wait method in @testing-library/react-native. (link: https://www.native-testing-library.com/docs/api-async)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found