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

feat(testing): Add utility for faking time #2069

Merged
merged 16 commits into from
Apr 14, 2022

Conversation

KyleJune
Copy link
Contributor

Depends on: #2048

My previous PR that this one depends on adds mock.ts. This PR adds a utility for faking time. It mocks time by replacing the Date, setTimeout, clearTimeout, setInterval, clearInterval globals with fake versions that are controlled by the user via the FakeTime instance.

The README.md section for faking time can be found here.

Here are the deno docs for time.ts.

@bartlomieju
Copy link
Member

@KyleJune is this ready for review?

@KyleJune
Copy link
Contributor Author

KyleJune commented Apr 13, 2022

@KyleJune is this ready for review?

Yes, all it needs is main to be merged back into it and Deno copyright header to be added. I could do that tonight. If there are no other issues and you decide you want to get it merged, feel free to make those changes yourself.

@bartlomieju
Copy link
Member

@KyleJune is this ready for review?

Yes, all it needs is main to be merged back into it and Deno copyright header to be added. I could do that tonight. If there are no other issues and you decide you want to get it merged, feel free to make those changes yourself.

Thanks! I'll give it as review. @kt3k please take a look as well.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Mostly looks good to me, thank you @KyleJune, this is very useful!

Could I ask you to open a PR to https://github.com/denoland/manual that updates testing chapters with this functionality?

testing/README.md Outdated Show resolved Hide resolved
testing/README.md Outdated Show resolved Hide resolved
testing/time.ts Outdated
import { ascend, RBTree } from "../collections/rb_tree.ts";
import { DelayOptions } from "../async/delay.ts";

export const _internals = {
Copy link
Member

Choose a reason for hiding this comment

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

Why do these need to be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are used by the test coverage to verify the real methods are called or not called when they should be. I believe the tests might replace them with stubs so that the real timer functions are never called. I could move them to a separate file with _ prefix to the name so that this constant is never exposed to end users.

Copy link
Member

Choose a reason for hiding this comment

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

I see, that makes sense, though I can see this not working properly if user's code replaced them before this file is loaded. I think this is fine as is, can you add a comment describing the purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure I can do that tonight and I think I'll just move them to _test_utils.ts. alternatively I could create a new file called _time.ts that could export just it instead.

Copy link
Member

Choose a reason for hiding this comment

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

_test_utils.ts seems fine 👍

Copy link
Contributor Author

@KyleJune KyleJune Apr 13, 2022

Choose a reason for hiding this comment

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

I thought about this and think _time.ts would be better so that when people import time.ts it won't have to load _test_utils.ts too which contains unrelated internal only utilities and could potentially grow in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the _internals const to _time.ts and added a comment describing its purpose.

testing/time.ts Outdated
}
}

/** The fake time replacement for se */
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this comment was cut-off by mistake

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into what it was supposed to say tonight. My bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment can just be deleted. The function is never exported and the fakeSetTimeout function doesn't have a comment above it 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.

I deleted this comment.

Comment on lines +222 to +223
* Overrides the real Date object and timer functions with fake ones that can be
* controlled through the fake time instance.
Copy link
Member

Choose a reason for hiding this comment

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

Could you add an example in a codeblock?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would the example from the README.md file be fine?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, definitely!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added the example from the README.md file.

KyleJune and others added 2 commits April 13, 2022 07:08
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
@KyleJune
Copy link
Contributor Author

Could I ask you to open a PR to https://github.com/denoland/manual that updates testing chapters with this functionality?

Looks like it doesn't have info regarding bdd.ts and mock.ts yet either. I could try updating the manual for those and time.ts this weekend.

@bartlomieju
Copy link
Member

Could I ask you to open a PR to https://github.com/denoland/manual that updates testing chapters with this functionality?

Looks like it doesn't have info regarding bdd.ts and mock.ts yet either. I could try updating the manual for those and time.ts this weekend.

@KyleJune that would be awesome, much obliged 🙏

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

LGTM. Awesome work! Thanks for your contribution!

@kt3k
Copy link
Member

kt3k commented Apr 13, 2022

We have a few test cases which depend on some specific date time. We currently use faketime command for setting it up in CI, but I hope we can replace those set ups with this utility

https://github.com/denoland/deno_std/blob/37b0b02f9114482ae7cd9bc7ea239785f3e31b02/datetime/test_2021_12_31.ts

@KyleJune
Copy link
Contributor Author

We have a few test cases which depend on some specific date time. We currently use faketime command for setting it up in CI, but I hope we can replace those set ups with this utility

https://github.com/denoland/deno_std/blob/37b0b02f9114482ae7cd9bc7ea239785f3e31b02/datetime/test_2021_12_31.ts

I went ahead and did that. The following PR branches off of this one.

#2109

@KyleJune
Copy link
Contributor Author

I've also made the remaining requested changes. I believe this is ready to go now.

@kt3k
Copy link
Member

kt3k commented Apr 14, 2022

@KyleJune Thanks for updating!

@kt3k kt3k merged commit 403f226 into denoland:main Apr 14, 2022
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.

None yet

3 participants