-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
react testing library with formik & yup validation not erroring as expected #224
Comments
If I remember correctly. The problem here is that you forgot that the Yup validation schema in Formik is an asynchronous call. To solve this problem, I wrapped it around a
|
Thanks for the investigation @weyert |
Awesome, I've got this all working now, thanks @weyert and @kentcdodds
|
Out of curiosity. How awful is it to simply do describe('Input', () => {
it('displays an error message when a required field is touched', async () => {
const validate = () => ({ test: 'Required' });
const { container, getByText, getByLabelText } = render(
<Formik validate={validate}>
<form>
<Field id="test" name="test" label={requiredProps.label} component={Input} />,
</form>
</Formik>
);
// blur past input
const input = getByLabelText(requiredProps.label);
fireEvent.blur(input);
// Formik validations are async
await wait();
// Ensure error message shows
expect(getByText('Required')).not.toBeNull();
expect(container.querySelector('.ncss-error-msg')).not.toBeNull();
});
}); namely, simply doing: |
That's not a big deal honestly, but you could improve things a bit: describe('Input', () => {
it('displays an error message when a required field is touched', async () => {
const validate = () => ({ test: 'Required' });
const { container, getByText, getByLabelText } = render(
<Formik validate={validate}>
<form>
<Field id="test" name="test" label={requiredProps.label} component={Input} />,
</form>
</Formik>
);
// blur past input
const input = getByLabelText(requiredProps.label);
fireEvent.blur(input);
// Ensure error message shows
expect(await findByText(/required/i)).not.toBeNull();
expect(container.querySelector('.ncss-error-msg')).not.toBeNull();
});
}); |
I receive a typescript error with this:
It reads
|
react-testing-library
version: 5.2.3react
version: 16.7.0-alpha.2node
version: unknpm
(oryarn
) version: unkRelevant code or config:
https://codesandbox.io/s/lll3rl5q0q
What you did:
Attempted to create a test for a basic form with a disabled submit button and yup for validation
What happened:
Disabled submit button remains disabled in the ui until all fields are valid, but submit button becomes enabled after a single field becomes valid when tests are run
Reproduction:
https://codesandbox.io/s/lll3rl5q0q
Problem description:
As seen in the debug output of formik in the web after a single change has been made compared to the debug output from the test; the state of the validation seems very different.
codesandbox can be a little iffy with this so suggest it might be better running localy when examining.
The text was updated successfully, but these errors were encountered: