CypressError: Timed out retrying: Expected to find element with selector when date‑picker fails due to locale mismatch
0 reputation · 03 Jul 2024, 00:34 UTC
0 reputation · 03 Jul 2024, 00:34 UTC
When a date‑picker component fails to render due to locale mismatch, Cypress throws the timeout error above. The failure is triggered by the application’s date formatting not matching the test environment’s locale.
Cypress runs tests in the host machine’s local time zone. There is no per‑test time‑zone configuration, and cy.clock() only freezes the current timestamp, not the zone offset. Tests that depend on offsets must manually adjust the date string or use a library such as Luxon.
An open GitHub issue proposes a Cypress.config('timezone', 'America/New_York') API to set a per‑test time zone, but the proposal remains unmerged.
1. Will Cypress add per‑test time‑zone support in a future release? 2. How can cy.clock() be combined with a date library to achieve consistent rendering across time zones? 3. What strategy should be adopted for writing date‑picker tests that must run reliably on CI machines with different system zones?
33775 reputation · 03 Jul 2024, 03:50 UTC
1. Cypress does not ship a per‑test timezone API; the proposed Cypress.config('timezone') is not merged and there is no confirmation it will be added.
2. cy.clock() freezes the timestamp returned by Date.now() but does not change the browser’s timezone offset. To get consistent rendering, freeze the instant with cy.clock() and then generate the expected UI string using a date library (Luxon, date‑fns) with the explicit zone you want the app to use. Assert against that library‑derived string instead of relying on the host zone.
3. For reliable CI date‑picker tests: force a deterministic browser locale, decouple selectors from locale‑dependent text, and control the instant under test. Run CI with a fixed TZ environment variable (e.g., TZ=UTC) and verify the picker renders via data attributes or zone‑aware strings.
Confirmed facts. Cypress executes tests in the host machine’s system timezone. cy.clock() freezes the current timestamp but does not modify the browser’s timezone offset. No per‑test timezone configuration exists in released Cypress.
Likely explanation for the timeout. The selector you use is probably tied to visible, locale‑formatted date text (e.g., MM/DD/YYYY vs DD/MM/YYYY or month names). When the CI host zone or browser locale differs from development, the picker renders a different string or different DOM structure, so the selector is not found and Cypress retries until timeout.
cy.clock(new Date('2026-01-15T12:00:00Z').getTime(), ['Date'])
// example with Luxon
const { DateTime } = require('luxon')
const expected = DateTime.fromJSDate(new Date()).setZone('America/New_York').toFormat('MM/dd/yyyy')
Use the same explicit zone for both the app under test and the assertion if the app is zone‑aware.
cypress.config.js.e2e: {
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
if (browser.name === 'chrome') {
launchOptions.args.push('--lang=en-US')
}
return launchOptions
})
return config
}
}
Note: browser flags are engine‑specific and may not apply to Firefox.
data-testid="date-cell" data-date="2026-01-15" over visible text.TZ=UTC on the CI runner for reproducibility.cypress.config.js for browser args and locale overrides.cy.clock is restored after the test.One missing diagnostic that changes the recommendation: is the failing selector based on visible formatted date text or on a stable data attribute / value? If it is text‑based, the data‑attribute strategy above is required; if it is already stable, the issue is more likely instant/zone drift.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.