File tree Expand file tree Collapse file tree 4 files changed +33
-1
lines changed Expand file tree Collapse file tree 4 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " mobx-react-lite " : patch
3+ ---
4+
5+ expose ` clearTimers ` function to tidy up background timers, allowing test frameworks such as Jest to exit immediately
Original file line number Diff line number Diff line change @@ -154,3 +154,19 @@ Above imports are for a convenience to utilize standard versions of batching. If
154154import { observerBatching } from " mobx-react-lite"
155155observerBatching (customBatchedUpdates)
156156```
157+
158+ ## Testing
159+
160+ In order to avoid memory leaks due to aborted renders from React
161+ fiber handling or React ` StrictMode ` , this library needs to
162+ run timers to tidy up the remains of the aborted renders.
163+
164+ This can cause issues with test frameworks such as Jest
165+ which require that timers be cleaned up before the tests
166+ can exit.
167+
168+ ### ** `clearTimers()**
169+
170+ Call ` clearTimers() ` in the ` afterEach ` of your tests to ensure
171+ that ` mobx-react-lite ` cleans up immediately and allows tests
172+ to exit.
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ export { Observer } from "./ObserverComponent"
1414export { useLocalObservable } from "./useLocalObservable"
1515export { useLocalStore } from "./useLocalStore"
1616export { useAsObservableSource } from "./useAsObservableSource"
17+ export { resetCleanupScheduleForTests as clearTimers } from "./utils/reactionCleanupTracking"
1718
1819export function useObserver < T > ( fn : ( ) => T , baseComponentName : string = "observed" ) : T {
1920 if ( "production" !== process . env . NODE_ENV ) {
Original file line number Diff line number Diff line change @@ -119,9 +119,19 @@ export function forceCleanupTimerToRunNowForTests() {
119119
120120/* istanbul ignore next */
121121export function resetCleanupScheduleForTests ( ) {
122+ if ( uncommittedReactionRefs . size > 0 ) {
123+ for ( const ref of uncommittedReactionRefs ) {
124+ const tracking = ref . current
125+ if ( tracking ) {
126+ tracking . reaction . dispose ( )
127+ ref . current = null
128+ }
129+ }
130+ uncommittedReactionRefs . clear ( )
131+ }
132+
122133 if ( reactionCleanupHandle ) {
123134 clearTimeout ( reactionCleanupHandle )
124135 reactionCleanupHandle = undefined
125136 }
126- uncommittedReactionRefs . clear ( )
127137}
You can’t perform that action at this time.
0 commit comments