Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NaridaL committed Jul 25, 2023
1 parent 1c5276b commit 9fa2b65
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 6.0.7

* [Fix now() sharing global state between tests #316](/~https://github.com/mobxjs/mobx-utils/pull/316), fixes [#306](/~https://github.com/mobxjs/mobx-utils/issues/306).

# 6.0.6

* [fromPromise carries forward old value from pending observable](/~https://github.com/mobxjs/mobx-utils/pull/311)
Expand Down
21 changes: 15 additions & 6 deletions src/now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ const tickers: {
[interval: string]: IResource<number>
} = {}

export function clearTimers() {
for (const key in tickers) {
if (tickers.hasOwnProperty(key)) {
tickers[key].dispose()
delete tickers[key]
}
/**
* Disposes of all the internal Observables created by invocations of `now()`.
*
* The use case for this is to ensure that unit tests can run independent of each other.
* You should not call this in regular application code.
*
* @example
* afterEach(() => {
* utils.resetNowInternalState()
* })
*/
export function resetNowInternalState() {
for (const key of Object.getOwnPropertyNames(tickers)) {
tickers[key].dispose()
delete tickers[key]
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/now.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("given desynchronization is enabled", () => {
})

afterEach(() => {
utils.clearTimers()
utils.resetNowInternalState()
})

it("given time passes, works", () => {
Expand Down

0 comments on commit 9fa2b65

Please sign in to comment.