Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress.moment using locale

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 ?

like image 259
Jerome Bouvier Avatar asked Dec 18 '18 09:12

Jerome Bouvier


People also ask

How do I set a moment in locale?

You set locale with moment. locale('de') , and you create a new object representing the date of now by moment() (note the parenthesis) and then format('LLL') it. The parenthesis is important.

What is Moment default locale?

lang(String, Object); By default, Moment. js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.

What is Moment () format?

Moment's format() method is what you use to convert a Moment object to a string. For example, here's how you would convert a YYYY-MM-DD string into a more human-readable format: const moment = require('moment'); const d = new Date('2019/06/01'); moment(d).format('MMMM d, YYYY'); // June 1, 2019.


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

like image 104
Salomanuel Avatar answered Oct 14 '22 04:10

Salomanuel


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.

like image 31
Diogo Rocha Avatar answered Oct 14 '22 06:10

Diogo Rocha