Cypress.moment 使用语言环境

Cypress.moment using locale

我正在尝试使用 Cypress.moment 来比较日期。目标网站是法语的,所以日期格式。

因此,我打算使用moment.js方法来切换fr locale。

Cypress.moment.locale('fr')

我应该能够按照他们在赛普拉斯文档中所说的那样做:

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

然后,

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)

但断言失败,因为赛普拉斯拒绝考虑 fr 语言环境。例如,它一直在比较 'décembre' 和 'december'。这显然失败了。

我做错了什么?

不幸的是,在撰写本文时,我认为 Cypress.moment.locale() 无法做到这一点,正如您在 git 存储库中的 this 问题中所见。

正如那里评论的那样 moment.locale 除了标准 moment 导入之外还需要导入,该导入是 moment-with-locales.min.js 并且尚未包含在赛普拉斯中。

same aforementioned thread 中,最近出现了一个可行的答案:

将其放入 support/index.js:

Cypress.moment.locale('de');

它在我的项目中表现出色