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

02/04 - Mocked date gives wrong day if time not explicitly set to be after 00:00 #14

Open
isimmons opened this issue Jun 5, 2024 · 2 comments

Comments

@isimmons
Copy link

isimmons commented Jun 5, 2024

In exercise 02 section 04 "Hooks" my test failed. When mocking the date it appears to be defaulting to "2024-01-01:00:00" but the day doesn't actually start until 1 second after that so what I get is "2023-12-31" if I console.log it out.

I fixed it in my code by explicitly setting the time to 1AM

beforeAll(() => {
	globalThis.Date = new Proxy(globalThis.Date, {
		construct: () => new OriginalDate('2024-01-01:01:00'),
	})
})

I just realized this is a problem on every lesson after this one. Is it something wrong on my end?

Windows 10, node 20

@isimmons isimmons changed the title exercise 02-04 mocked date gives wrong day if time on explicitly set to be after 00:00 exercise 02-04 mocked date gives wrong day if time not explicitly set to be after 00:00 Jun 5, 2024
@kettanaito
Copy link
Member

Hi, @isimmons. Thanks for sharing this!

I've encountered a similar issue when giving the live version of this workshop, where the culprit was the omission of the explicit locale when calling .toLocaleDateString(). I've then defaulted it to en-US, which should produce the same date for everyone:

const weekday = new Date().toLocaleDateString('en-US', { weekday: 'long' })

I'm trying to reproduce your issue, this is where I got:

--- a/exercises/02.test-structure/04.solution.hooks/greet.test.ts
+++ b/exercises/02.test-structure/04.solution.hooks/greet.test.ts
@@ -6,6 +6,8 @@ beforeAll(() => {
        globalThis.Date = new Proxy(globalThis.Date, {
                construct: () => new OriginalDate('2024-01-01'),
        })
+
+       console.log(new OriginalDate('2024-01-01'))
 })
 
 afterAll(() => {

This prints the following for me:

2024-01-01T00:00:00.000Z

Does this print 2023-12-31 for you?

@isimmons
Copy link
Author

isimmons commented Jun 6, 2024

Yes Sir, here is my output.

    const date = new OriginalDate('2024-01-01')
    console.log(date) // 2024-01-01T00:00:00.000Z
    console.log(date.toLocaleDateString()) // 12/31/2023
    console.log(date.toLocaleDateString('en-US', { weekday: 'long' })) // Sunday

    const anotherDate = new OriginalDate('2024-01-01:00:00:00.001')
    console.log(anotherDate) // 2024-01-01T00:00:00.001Z
    console.log(anotherDate.toLocaleDateString()) // 1/1/2024
    console.log(anotherDate.toLocaleDateString('en-US', { weekday: 'long' })) // Monday

@kettanaito kettanaito changed the title exercise 02-04 mocked date gives wrong day if time not explicitly set to be after 00:00 02/04 - Mocked date gives wrong day if time not explicitly set to be after 00:00 Jun 19, 2024
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

No branches or pull requests

2 participants