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(#324): runs xpath tests with the same timezone #323

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defaults:

env:
TZ: 'America/Phoenix'
LOCALE_ID: 'en-US'
Copy link
Collaborator Author

@latin-panda latin-panda Mar 4, 2025

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


jobs:
install-and-build:
Expand Down
23 changes: 23 additions & 0 deletions packages/xpath/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,33 @@ type AnyParentNode =
| XMLDocument;

declare global {
/**
* The timezone identifier used for all date and time operations in tests.
* This string follows the IANA Time Zone Database format (e.g., 'America/Phoenix').
* It determines the offset and DST behavior for `Date` objects and
* related functions.
*
* @example 'America/Phoenix' // Fixed UTC-7, no DST
* @example 'Europe/London' // UTC+0 (GMT) or UTC+1 (BST) with DST
*/
// eslint-disable-next-line no-var
var TZ: string | undefined;
// eslint-disable-next-line no-var
var LOCALE_ID: string | undefined;
/**
* The locale string defining the language and regional formatting for tests.
* This follows the BCP 47 language tag format (e.g., 'en-US'). It ensures consistent formatting
* across tests.
*
* @example 'en-US' // American English
*/
// eslint-disable-next-line no-var
var IMPLEMENTATION: string | undefined;
Copy link
Collaborator Author

@latin-panda latin-panda Mar 4, 2025

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.

}

globalThis.IMPLEMENTATION = typeof IMPLEMENTATION === 'string' ? IMPLEMENTATION : undefined;
globalThis.TZ = typeof TZ === 'string' ? TZ : undefined;
globalThis.LOCALE_ID = typeof LOCALE_ID === 'string' ? LOCALE_ID : undefined;

const namespaces: Record<string, string> = {
xhtml: 'http://www.w3.org/1999/xhtml',
Expand Down Expand Up @@ -338,3 +357,7 @@ export const getNonNamespaceAttributes = (element: Element): readonly Attr[] =>

return attrs.filter(({ name }) => name !== 'xmlns' && !name.startsWith('xmlns:'));
};

export const getDefaultDateTimeLocale = (): string => {
return new Date().toLocaleString(LOCALE_ID, { timeZone: TZ });
};
14 changes: 14 additions & 0 deletions packages/xpath/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { afterEach, beforeEach } from 'vitest';
import { vi } from 'vitest';
import { getDefaultDateTimeLocale } from './helpers.ts';

beforeEach(() => {
const dateOnTimezone = getDefaultDateTimeLocale();
vi.useFakeTimers({
now: new Date(dateOnTimezone).getTime(),
});
});

afterEach(() => {
vi.useRealTimers();
});
2 changes: 0 additions & 2 deletions packages/xpath/test/xforms/now.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;-)

// given
const now = new Date();
const today = `${now.getFullYear()}-${(1 + now.getMonth()).toString().padStart(2, '0')}-${now
Expand Down
16 changes: 15 additions & 1 deletion packages/xpath/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,26 @@ const TEST_ENVIRONMENT = BROWSER_ENABLED ? 'node' : 'jsdom';
*/
const TEST_TIME_ZONE = 'America/Phoenix';

/**
* The locale used for formatting dates and times in all test cases.
* This ensures consistent language and regional settings across tests.
* Currently set to 'en-US' (American English), which affects date formats
* (e.g., MM/DD/YYYY) and time separators.
*
* @constant
* @default 'en-US'
*/
const TEST_LOCALE = 'en-US';

export default defineConfig(({ mode }) => {
const isTest = mode === 'test';

let timeZoneId: string | null = process.env.TZ ?? null;
let localeId: string | null = process.env.LOCALE_ID ?? null;

if (isTest) {
timeZoneId = timeZoneId ?? TEST_TIME_ZONE;
localeId = localeId ?? TEST_LOCALE;
}

// `expressionParser.ts` is built as a separate entry so it can be consumed
Expand Down Expand Up @@ -83,6 +96,7 @@ export default defineConfig(({ mode }) => {
},
define: {
TZ: JSON.stringify(timeZoneId),
LOCALE_ID: JSON.stringify(localeId),
},
esbuild: {
target: 'esnext',
Expand Down Expand Up @@ -122,7 +136,7 @@ export default defineConfig(({ mode }) => {
headless: true,
screenshotFailures: false,
},

setupFiles: ['test/setup.ts'],
environment: TEST_ENVIRONMENT,
globals: false,
include: ['test/**/*.test.ts'],
Expand Down
Loading