从 Cypress 中已弃用的 Moment 转到 DayJS 时,我遇到了语言环境问题。
原
Moment()
用法:
public moment(): Moment {
const moment = Cypress.moment();
moment.locale("nb-NO");
return moment;
}
const in2Months = moment().add(2, "months");
这效果很好,例如“三月”被“火星”取代。
但是,当尝试以与 days.js 相同的方式使用它时,区域设置不会更改:
public day() {
return dayjs().locale('nb-NO');
}
const in2Months = page.day().add(2, "months");
这里的结果仍然是“March”。我尝试过仅对区域设置使用“否”,但结果相同。
我在这里缺少什么?
更新: 根据以下答案:
import dayjs from "dayjs";
import 'dayjs/locale/nb'
const in2Months = dayjs().locale('nb').add(2, 'Month');
cy.log(in2monthstoString());
RESULT: Mon, 28 Mar 2022 12:39:29 GMT
...这不是语言环境格式。
此外,尝试
.month()
的任何变体,我总是得到月份数的结果。但我不知道名字。
正如Days JS github readme中提到的,您必须导入您想要使用的国际化,默认情况下不会包含它。所以我写了一个简单的程序来检查这一点并且它有效
const dayjs = require('dayjs')
import 'dayjs/locale/nb'
describe('SO Ques', () => {
it('SO Ques', () => {
cy.log(dayjs().locale('nb').add(2, 'Month').format('MMMM'))
})
})
测试运行者:
所以你的程序应该是这样的:
const dayjs = require('dayjs')
import 'dayjs/locale/nb'
public day() {
return dayjs().locale('nb');
}
const in2Months = page.day().add(2, 'Month').format('MMMM');
您可以评估函数中的所有值,然后返回它,然后在测试中使用它,如下所示:
function date() {
return {
day: dayjs().locale('nb').add(2, 'Month').format('DD'),
month: dayjs().locale('nb').add(2, 'Month').format('MMMM'),
year: dayjs().locale('nb').add(2, 'Month').format('YYYY'),
};
}
date().day //28
date().month //mars
date().year //2022