Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix now() sharing global state between tests #316

Merged
merged 3 commits into from
Jul 25, 2023

Conversation

Nokel81
Copy link
Contributor

@Nokel81 Nokel81 commented Mar 2, 2023

Signed-off-by: Sebastian Malton sebastian@malton.name

fix #306

Signed-off-by: Sebastian Malton <sebastian@malton.name>
Signed-off-by: Sebastian Malton <sebastian@malton.name>
@Nokel81
Copy link
Contributor Author

Nokel81 commented Jun 20, 2023

@NaridaL or @mweststrate Sorry to ping you but this PR has been here for a couple months, would I be able to get a review please?

@NaridaL
Copy link
Collaborator

NaridaL commented Jun 20, 2023

I can take a look this week.

src/now.ts Outdated
Comment on lines 57 to 61
const timer = tickers[interval]
? tickers[interval]
: typeof interval === "number"
? createIntervalTicker(interval)
: createAnimationFrameTicker()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but tickets[interval] now never gets assigned, ie no caching.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes because during testing we don't want caching

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but it's not caching when not testing either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have updated so it can

src/now.ts Outdated
Comment on lines 49 to 54
if (!synchronized) {
if (typeof interval === "number") {
return createIntervalTicker(interval).current()
}

return createAnimationFrameTicker().current()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the docs for fromResource https://github.com/mobxjs/mobx-utils/blob/master/src/from-resource.ts , which this uses, the usual usage is that you define your observable outside a reactive context, and then use it inside the reactive context. utils.now gets around this by caching based on the interval. Without this it kinda breaks, and we unsubscribe/resubscrive to the observable on every autorun execution:

grafik

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be a problem as this is designed only for tests.

test/now.js Outdated
jest.useFakeTimers("modern")
jest.setSystemTime(new Date("2015-10-21T07:28:00Z"))

utils.desynchronizeNowForTests()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commenting out this line did not cause the test to fail, even fixing the above bug.

See also comment on original bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I see that now. So I am somehow modifying the behaviour otherwise. The test does fail if I revert all the changes to src/now.ts so the bug does exist.

I guess another solution for us would be for there to be an easy way create IComputedValue<number> for the date

Signed-off-by: Sebastian Malton <sebastian@malton.name>
@Nokel81
Copy link
Contributor Author

Nokel81 commented Jul 24, 2023

@NaridaL Would you be able to take another look? I decided to switch to a different implementation so that the tests still cache the but only within a single test case.

I guess another option would be for the project that I am working on to switch to managing state ourselves and just copy over (or expose here) the createIntervalTicker function.

@NaridaL NaridaL merged commit 1c5276b into mobxjs:master Jul 25, 2023
@Nokel81 Nokel81 deleted the fix/issue-306 branch July 25, 2023 20:23
@NaridaL
Copy link
Collaborator

NaridaL commented Jul 25, 2023

This approach seems much simpler.

I've made a couple of minor improvements: renamed it to resetNowInternalState (because the relation to now is currently not visible, and we don't use the "timers" terminology anywhere; and added documentation comments.

I can't push to this branch though, (this is why you "allow edits by maintainers" ;) )

@NaridaL
Copy link
Collaborator

NaridaL commented Jul 25, 2023

published in 6.0.8

@Nokel81
Copy link
Contributor Author

Nokel81 commented Jul 25, 2023

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Testing stuff using now() leads to tests that are failing for wrong reason
2 participants