-
Notifications
You must be signed in to change notification settings - Fork 13
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(#324): runs xpath tests with the same timezone #323
Conversation
|
* | ||
* @example 'en-US' // American English | ||
*/ | ||
// eslint-disable-next-line no-var | ||
var IMPLEMENTATION: string | undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't find the usage of this IMPLEMENTATION
. I wanted to add a JSDoc description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was added in this commit . It's unclear what it is for. I'll leave it there for now and ask Trevor later
@eyelidlessness When you have a chance, can you please review it? Thank you! |
@@ -21,6 +21,7 @@ defaults: | |||
|
|||
env: | |||
TZ: 'America/Phoenix' | |||
LOCALE_ID: 'en-US' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added for completeness and to clarify that the tests expect the date format in this locale.
When switching to another locale (e.g., es-AR or es-MX), some tests fail because they expect a different date format.
With es-MX
the now()
returns a 1-1-1970 date, and tests using today()
fail.
With es-AR
the now()
returns the current date correctly. But other tests fails, where tomorrow is set in en-US standard 2025-03-05
FAIL test/xforms/date-comparison.test.ts > date comparison > tomorrow > should be greater than or equal to today()
AssertionError: expected false to deeply equal true
- Expected
+ Received
- true
+ false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the root cause of #324 then that the test was written with the assumption that the timezone was Arizona time and because that wasn't set uniformly the device time was being used in some cases?
f37c289
to
08f8a05
Compare
@lognaturel, yes, that's what I understand. The default timezone (Arizona) was added in this commit, with this JSDoc
This PR ensures the timezone is applied to all tests by introducing a global |
@@ -14,8 +14,6 @@ describe('#now()', () => { | |||
// > including timezone offset (i.e. not normalized to UTC) as described | |||
// > under the dateTime datatype. | |||
it('should return a timestamp for this instant', () => { | |||
// this check might fail if run at precisely midnight ;-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment wasn't related to the issue here but was a tongue and cheek remark about how it's possible for new Date();
to be run milliseconds before midnight and then the now()
function to be evaluated milliseconds after midnight in which case the test would fail even if the code is all correct. I do agree with removing the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation! I didn't interpret it like that, but it makes sense.
@@ -83,6 +96,7 @@ export default defineConfig(({ mode }) => { | |||
}, | |||
define: { | |||
TZ: JSON.stringify(timeZoneId), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was intended/expected to set the global timezone for tests, does that sound right? Is the issue then that this doesn't work or at least not consistently? Is some of what's at vitest-dev/vitest#1575 (comment) related?
If there was an attempted alternative approach to set a consistent timezone that didn't work, I think it'd be helpful to remove it so there isn't ambiguity about the source of truth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question
The define
option in the Vite config is for defining global constants that are replaced at build time or made available as globals during development. The test runner doesn't automatically set the timezone by defining the TZ constant.
We still need the constant to setup the xpath's evaluator option and to get the default locale for mocking the timezone and locale in the global beforeEach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation! I'm not super familiar with these test options. So now I'm seeing that the evaluator timezone was being set but there was nothing previously setting the vitest timezone. From what I've read, using a global beforeEach
seems like a good approach to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation! I'm not super familiar with these test options.
No worries, I'm also learning as I fix this bug
Closes #324
I have verified this PR works in these browsers (latest versions):
What else has been done to verify this works as intended?
To reproduce the error:
yarn workspace @getodk/xpath test
Other things done:
After implementing the fix, I tried setting my machine in different timezones, ran the tests, and they passed. The different timezones have been passed to the "now" function correctly.
The offset is being appended to the localDateTimeString
I tried making a test about DST, but it's hard to trick reliably the test to take a different DST of the same timezone. However, I added some debugging code in localDateTimeString function, and I can see the offset switching to/from DST for the same timezone.
Why is this the best possible solution? Were any other approaches considered?
This ensures the tests are run in the same timezone by setting it in the
beforeEach
hook using theuseFakeTimers
utility. However, this doesn't mock the DST, which has been challenging to tick the system timer.How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?
N/A
Do we need any specific form for testing your changes? If so, please attach one.
I don't think so
What's changed