freeCodeCamp / classroom

BSD 3-Clause "New" or "Revised" License
144 stars 121 forks source link

fix: process dashboard data test #422

Closed theGaryLarson closed 1 year ago

theGaryLarson commented 1 year ago

Closes #329 follows @ngillux merge in PR #421

image

UPDATE: CI Tests are passing!

Fix: used jest.SpyOn to mock the new Date().getTime() method being assigned to today in studentActivity to used a fixedTime for today. Updated the Date.toLocaleString() to Date.toLocaleString( 'en-US', { timeZone: 'America/Los Angeles' })

Also, as an aside there was another issue with the space between the seconds and AM/PM varying depending on which operating system I used to update the snapshot. If I used wsl2 CLI jest -u i would get a narrow no break space character NNBSP

Co-authored-by: Komal kaur91499@gmail.com

theGaryLarson commented 1 year ago

Hmm interesting I notice there is a difference in time. I have to look more into Date.toLocaleString() and why it returns differently in GitHub environment workflows.

I apologize for all the failed workflows in Actions but seems how the test passes fine for me locally. I had to push changes to observe the differences in how the GitHub workflow handles the Date.toLocaleString() used in components/StudentActivity.js.

lloydchang commented 1 year ago

FWIW

Invoking toLocaleString() in UTC

You can specify a parameter timeZone, which defines the target timezone to be used when formatting the date:


var date = new Date('2017-05-01T01:00:00.000Z');

console.log(date.toLocaleString("en-US", { month: "short", timeZone: 'America/New_York' })); // "Apr"

console.log(date.toLocaleString("en-US", { month: "short", timeZone: 'UTC' })); // "May"



https://stackoverflow.com/questions/42956104/invoking-tolocalestring-in-utc
lloydchang commented 1 year ago

:consoling-hug: the authors @theGaryLarson @Komal914

fix: process dashboard data test #333

https://github.com/freeCodeCamp/classroom/actions/runs/5757195554/job/15607894960?pr=422

failing after

fix: snapshot updated

https://github.com/freeCodeCamp/classroom/pull/422/commits/345626facf2b94bf7ee4ded74ab4812905264022

while you retry, wait for tests to run and pass/fail, re-re-re-re-re-try, rinse and repeat,

if you want, you can keep yourselves busy with time-zone and calendaring trivia via

  1. https://www.zainrizvi.io/blog/falsehoods-programmers-believe-about-time-zones/

  2. http://algeri-wong.com/yishan/great-unsolved-problems-in-computer-science.html

theGaryLarson commented 1 year ago

yea I am working on it. think I might have found a solution but we will see :) The time zone is an issue but the StudentActivity method uses new Date().getTime(). So even if it's fixed it will eventually get out of sync and cause another failure. I'm working on mocking the Date object using the Jest library. Reading up on it now.

UPDATE:

This resolved the issue

utsab commented 1 year ago

Thank you @theGaryLarson and @Komal914 for fixing the test. Good idea to mock the DateTime.now() function. This is a pattern which will likely be useful for future tests where the behavior depends on the comparison between two dates.