Cypress.moment using locale

Viewed 5514

I am trying to use Cypress.moment to compare dates. The targeted website is in french so the date format.

Therefore, I intended to use moment.js method to switch fr locale.

Cypress.moment.locale('fr')

I should be able to do that as they say on cypress documentation :

Cypress automatically includes moment.js and exposes it as Cypress.moment https://docs.cypress.io/api/utilities/moment.html#Syntax

Then,

const todaysDate = Cypress.moment().format('Do')
const currentMonth = Cypress.moment().format('MMMM')

cy.get('.date__title').should('contain', todaysDate)
cy.get('.c-title').should('contain', currentMonth)

But the assertion fails as cypress refuse to take into account the fr locale. It keeps comparing 'décembre' with 'december' for instance. Which fails obviously.

I am doing something wrong ?

2 Answers

in the same aforementioned thread, a working answer recently surfaced:

put this in support/index.js:

Cypress.moment.locale('de');

it worked brilliantly in my project

Unfortunately, at the time of writing, I think this is not possible with Cypress.moment.locale(), as you can see in this issue in their git repository.

As commented there moment.locale requires an import in addition to the standard moment import, that import is moment-with-locales.min.js and has not been included in Cypress.

Related