我测试了一个React组件:
onDateSelect(newDate) {
const date = moment(this.state.date);
const { day, month, year } = newDate;
date.date(day);
date.month(month-1);
date.year(year);
this.setState({
date,
value: date.format(DATE_FORMAT)
});
}
使用:
test('Method onDateSelect', () => {
const wrapper = shallow(<DatePickerField date={ moment('2022-11-22') } />)
wrapper.instance().onDateSelect({ day: 23, month: 11, year: 2022 }); // Run it once because of an issue with testing moment in Jest
expect(wrapper.state().date).toBe(moment('2022-11-23'));
})
Jest返回错误,但有非:
Expected value to be (using ===): "2022-11-22T23:00:00.000Z" Received: "2022-11-21T23:00:00.000Z" Difference: - Expected + Received - "2022-11-22T23:00:00.000Z" + "2022-11-21T23:00:00.000Z"
我在CET时区,因此它提前一小时显示。但仍然酶状态包含旧的价值。该功能工作正常。
我错过了什么?