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
If you have not yet read the previous post about "How to support Reusable State in Effects", you can find it here.
StrictMode can be enabled for tests too. Tests that assert things about effects are somewhat uncommon, but the most common types of assertions are covered below (along with suggestions for how to update them).
Verifying an effect occurred at least once (e.g. on mount).
If the primary purpose of your test is to verify that a side effect is called on-mount, you can avoid asserting on the number of times that it’s called.
// If the code you're testing looks like this...functionExample({ mountCallback }){useEffect(()=>{mountCallback();},[mountCallback]);// ...}render(<ComponentYouAreTestingmountCallback={mountCallback}/>);// Won't work (unless a ref is used) ✘expect(mountCallback).toHaveBeenCalledTimes(1);// Will work ✓expect(mountCallback).toHaveBeenCalled();
Counting the number of times a component rendered.
Tests that count renders to prevent cascading updates can make use of the Profiler API. (The Profiler’s commit hooks are called once per commit.)
// If the code you're testing looks like this...functionExample({ mountCallback }){useEffect(()=>{mountCallback();},[mountCallback]);// ...}// Won't work (unless a ref is used) ✘render(<ExamplemountCallback={mountCallback}{...props}/>);expect(mountCallback).toHaveBeenCalledTimes(1);// Will work ✓constonRender=jest.fn();render(<Profilerid="test"onRender={onRender}><Example{...props}/></Profiler>);expect(onRender).toHaveBeenCalledTimes(1);
Counting how often an effect has run.
Tests that count the specific number of times a side effect is called (e.g. that it’s called only when some condition changes) can be written, but require special consideration for mounting.
// If the code you're testing looks like this...functionSomeComponent({ someEffect, someConditional }){useEffect(()=>{someEffect();},[someConditional,someEffect]);// ...}constsomeEffect=jest.fn();// The effect is expected to be called on mount.// It will be called more than once if React runs the effect more than once.render(<SomeComponentsomeEffect={someEffect}someConditional={1}/>);// Ignore mount for simplicity (since React calls the effect twice on mount).someEffect.mockReset();// Confirm the effect is not called after update if condition doesn't change.render(<SomeComponentsomeEffect={someEffect}someConditional={1}/>);expect(someEffect).not.toBeCalled();// Confirm the effect is called on update if condition does change.render(<SomeComponentsomeEffect={someEffect}someConditional={2}/>);expect(someEffect).toBeCalledTimes(1);
If it’s important for the effect to only run once on mount, a ref can be used as shown above and our test can be written to verify that behavior.
functionSomeComponent({ someEffect, someConditional }){constprevConditionalRef=useRef({});useEffect(()=>{// Only call our callback once when a value changes.if(prevConditionalRef.current!==someConditional){prevConditionalRef.current=someConditional;someEffect();}},[someConditional,someEffect]);// ...}constsomeEffect=jest.fn();render(<SomeComponentsomeEffect={someEffect}someConditional={1}/>);expect(someEffect).toBeCalledTimes(1);
If your test doesn’t match one of the patterns shown above– ask us and we’ll help.
Related posts
For more information on supporting Reusable State in StrictMode, see:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Overview
If you have not yet read the previous post about "How to support Reusable State in Effects", you can find it here.
StrictMode can be enabled for tests too. Tests that assert things about effects are somewhat uncommon, but the most common types of assertions are covered below (along with suggestions for how to update them).
Verifying an effect occurred at least once (e.g. on mount).
If the primary purpose of your test is to verify that a side effect is called on-mount, you can avoid asserting on the number of times that it’s called.
Counting the number of times a component rendered.
Tests that count renders to prevent cascading updates can make use of the Profiler API. (The Profiler’s commit hooks are called once per commit.)
Counting how often an effect has run.
Tests that count the specific number of times a side effect is called (e.g. that it’s called only when some condition changes) can be written, but require special consideration for mounting.
If it’s important for the effect to only run once on mount, a ref can be used as shown above and our test can be written to verify that behavior.
If your test doesn’t match one of the patterns shown above– ask us and we’ll help.
Related posts
For more information on supporting Reusable State in StrictMode, see:
Beta Was this translation helpful? Give feedback.
All reactions