You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constuseSelectedThemeContext=()=>{constcontext=useContext(SelectThemeContext);if(isEmptyObject(context)){thrownewError("You have to use useSelectThemeContext inside <SelectThemeContextProvider />",);}const{ theme, toggleTheme }=context;return{ theme, toggleTheme };};exportdefaultuseSelectedThemeContext;
What you did:
I'm trying to implement a custom hook which must be used together with a Provider, if it is used in a component that does not use the Provider it should throw an error
What happened:
The test is working, but it generates an error in the console.
Test
it("[ACAMP-03] - should return error message when trying to use custom hook without provider",()=>{
let message: string;try{renderHook(()=>useSelectedThemeContext());message="";}catch(e: any){message=e.message;}expect(message).toEqual("You have to use useSelectThemeContext inside <SelectThemeContextProvider />",);});
Log
atHTMLUnknownElementImpl._dispatch(node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:221:9)atHTMLUnknownElementImpl.dispatchEvent(node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:94:17)atHTMLUnknownElement.dispatchEvent(node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:231:34)console.errorTheaboveerroroccurredinthe<TestComponent>component:
atTestComponent(C:\Users\rafael.almeida\Documents\Projects\Estudo\acamp\base-reactv18-eslint-prettier-config\node_modules\@testing-library\react\dist\pure.js:281:5)Consideraddinganerrorboundarytoyourtreetocustomizeerrorhandlingbehavior.Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.atlogCapturedError(node_modules/react-dom/cjs/react-dom.development.js:18687:23)atupdate.callback(node_modules/react-dom/cjs/react-dom.development.js:18720:5)atcallCallback(node_modules/react-dom/cjs/react-dom.development.js:13923:12)atcommitUpdateQueue(node_modules/react-dom/cjs/react-dom.development.js:13944:9)atcommitLayoutEffectOnFiber(node_modules/react-dom/cjs/react-dom.development.js:23391:13)
If I try to render the custom hook to get the result it already breaks and I don't have the possibility to access the result.error.message.
I noticed that I don't have access to this property of result.
Is there any way I can test this without generating errors in the console?
Problem description:
I believe that if the test is passing it should not receive errors in the test console.
You are using renderHook from @testing-library/react and not @testing-library/react-hooks which is correct for React 18, but comes with a leaner API (e.g. no result.error property).
I’ll transfer this issue to their repo for you to get their thoughts on the console errors, but for what it’s worth, react is very noisy in the console around errors, even when using things like error boundaries, so your only option might be to manually suppress console errors in your test.
mpeyper
transferred this issue from testing-library/react-hooks-testing-library
Jun 29, 2022
react-testing-library
version: 13.3.0react
version: 18.0.9node
version: 16.14.2npm
version: 8.5.0Relevant code or config:
What you did:
I'm trying to implement a custom hook which must be used together with a Provider, if it is used in a component that does not use the Provider it should throw an error
What happened:
The test is working, but it generates an error in the console.
Test
Log
If I try to render the custom hook to get the result it already breaks and I don't have the possibility to access the result.error.message.
I noticed that I don't have access to this property of result.
Is there any way I can test this without generating errors in the console?
Problem description:
I believe that if the test is passing it should not receive errors in the test console.
CodeSandBox
CodeSandBox
The text was updated successfully, but these errors were encountered: