Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
fix: expose function to clear timers and allow immediate test exit (#329
Browse files Browse the repository at this point in the history
)

* fix: expose function to clear timers and allow immediate test exit

Exposes `clearTimers` function which clears up background
'aborted render' reactions immediately.  Some test frameworks
such as Jest prevent exit unless all such timers are cleared.

For: mobxjs/mobx#2528

* Update .changeset/loud-starfishes-shave.md

Co-authored-by: Daniel K <FredyC@users.noreply.github.com>
  • Loading branch information
RoystonS and FredyC committed Oct 18, 2020
1 parent 9219288 commit a0e5fea
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/loud-starfishes-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx-react-lite": minor
---

expose `clearTimers` function to tidy up background timers, allowing test frameworks such as Jest to exit immediately
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ Above imports are for a convenience to utilize standard versions of batching. If
import { observerBatching } from "mobx-react-lite"
observerBatching(customBatchedUpdates)
```

## Testing

In order to avoid memory leaks due to aborted renders from React
fiber handling or React `StrictMode`, this library needs to
run timers to tidy up the remains of the aborted renders.

This can cause issues with test frameworks such as Jest
which require that timers be cleaned up before the tests
can exit.

### **`clearTimers()**

Call `clearTimers()` in the `afterEach` of your tests to ensure
that `mobx-react-lite` cleans up immediately and allows tests
to exit.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { Observer } from "./ObserverComponent"
export { useLocalObservable } from "./useLocalObservable"
export { useLocalStore } from "./useLocalStore"
export { useAsObservableSource } from "./useAsObservableSource"
export { resetCleanupScheduleForTests as clearTimers } from "./utils/reactionCleanupTracking"

export function useObserver<T>(fn: () => T, baseComponentName: string = "observed"): T {
if ("production" !== process.env.NODE_ENV) {
Expand Down
12 changes: 11 additions & 1 deletion src/utils/reactionCleanupTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,19 @@ export function forceCleanupTimerToRunNowForTests() {

/* istanbul ignore next */
export function resetCleanupScheduleForTests() {
if (uncommittedReactionRefs.size > 0) {
for (const ref of uncommittedReactionRefs) {
const tracking = ref.current
if (tracking) {
tracking.reaction.dispose()
ref.current = null
}
}
uncommittedReactionRefs.clear()
}

if (reactionCleanupHandle) {
clearTimeout(reactionCleanupHandle)
reactionCleanupHandle = undefined
}
uncommittedReactionRefs.clear()
}

0 comments on commit a0e5fea

Please sign in to comment.