How to Test FormProvider / useFormContext ? #3815
-
Hi there, I would like to know how to test with FormProvider? I am doing the following: EmailProvider.test.tsx import * as React from 'react';
import { render } from '@testing-library/react';
import { FormProvider, useForm } from 'react-hook-form';
import EmailProvider from '.';
const customRender = (ui) => {
const Wrapper: React.FC = ({ children }) => {
const methods = useForm();
return <FormProvider {...methods}>{children}</FormProvider>;
};
return render(<Wrapper>{ui}</Wrapper>);
};
describe('Email Provider component', () => {
test('should render without error', async () => {
// const { container } = await renderWithReactHookForm(<EmailProvider />);
const { container } = customRender(<EmailProvider />);
const tree = container.firstChild;
// await waitFor(() => {
expect(tree).toMatchSnapshot();
// });
});
}); and getting the following error: console.error
Warning: An update to Wrapper inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at Wrapper (/Users/guilhermeramos/Desktop/projects.nosync/meket-client/src/features/SignIn/components/EmailProvider/EmailProvider.test.tsx:8:40)
at printWarning (node_modules/react-dom/cjs/react-dom.development.js:67:30)
at error (node_modules/react-dom/cjs/react-dom.development.js:43:5)
at warnIfNotCurrentlyActingUpdatesInDEV (node_modules/react-dom/cjs/react-dom.development.js:24064:9)
at dispatchAction (node_modules/react-dom/cjs/react-dom.development.js:16135:9)
at node_modules/react-hook-form/src/useForm.ts:169:9
at node_modules/react-hook-form/src/useForm.ts:752:11
at MutationObserverImpl._callback (node_modules/react-hook-form/src/utils/onDomRemove.ts:21:9)
console.error
Warning: An update to Wrapper inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at Wrapper (/Users/guilhermeramos/Desktop/projects.nosync/meket-client/src/features/SignIn/components/EmailProvider/EmailProvider.test.tsx:8:40)
at printWarning (node_modules/react-dom/cjs/react-dom.development.js:67:30)
at error (node_modules/react-dom/cjs/react-dom.development.js:43:5)
at warnIfNotCurrentlyActingUpdatesInDEV (node_modules/react-dom/cjs/react-dom.development.js:24064:9)
at dispatchAction (node_modules/react-dom/cjs/react-dom.development.js:16135:9)
at node_modules/react-hook-form/src/useForm.ts:169:9
at node_modules/react-hook-form/src/useForm.ts:731:11
at node_modules/react-hook-form/src/useForm.ts:759:11
console.error
Warning: An update to Wrapper inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at Wrapper (/Users/guilhermeramos/Desktop/projects.nosync/meket-client/src/features/SignIn/components/EmailProvider/EmailProvider.test.tsx:8:40)
at printWarning (node_modules/react-dom/cjs/react-dom.development.js:67:30)
at error (node_modules/react-dom/cjs/react-dom.development.js:43:5)
at warnIfNotCurrentlyActingUpdatesInDEV (node_modules/react-dom/cjs/react-dom.development.js:24064:9)
at dispatchAction (node_modules/react-dom/cjs/react-dom.development.js:16135:9)
at node_modules/react-hook-form/src/useForm.ts:169:9
at node_modules/react-hook-form/src/useForm.ts:752:11
at MutationObserverImpl._callback (node_modules/react-hook-form/src/utils/onDomRemove.ts:21:9)
console.error
Warning: An update to Wrapper inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at Wrapper (/Users/guilhermeramos/Desktop/projects.nosync/meket-client/src/features/SignIn/components/EmailProvider/EmailProvider.test.tsx:8:40)
at printWarning (node_modules/react-dom/cjs/react-dom.development.js:67:30)
at error (node_modules/react-dom/cjs/react-dom.development.js:43:5)
at warnIfNotCurrentlyActingUpdatesInDEV (node_modules/react-dom/cjs/react-dom.development.js:24064:9)
at dispatchAction (node_modules/react-dom/cjs/react-dom.development.js:16135:9)
at node_modules/react-hook-form/src/useForm.ts:169:9
at node_modules/react-hook-form/src/useForm.ts:731:11
at node_modules/react-hook-form/src/useForm.ts:759:11 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
you will need to include hook form Provider into your test. |
Beta Was this translation helpful? Give feedback.
-
I'm facing different problem, I was able to mock form context, but I'm unable to to assert submit handler is called or not?. I mean the test for handlesubmit assertion is always failing. |
Beta Was this translation helpful? Give feedback.
you will need to include hook form Provider into your test.