简体   繁体   中英

How to use moment in jest react unit test?

I have a Unit component that uses moment.js to set the format for a date:

let bdate= moment(this.props.person.birthDate).format(
                'hh:mm A, DD MMM YYYY'
              );

Later this bdate gets rendered in the component.

This component has a unit test:

describe('Unit', () => {
  it('should render correctly', () => {
    const person = 
      {      
        birthDate: '2018-06-14T00:00:00.000Z',
      }

    const wrapper = shallow(
      <Unit
        person={person}
      />
    );
    expect(wrapper).toMatchSnapshot();
  });
});

The issue is that the snapshot for Unit outputs the current date? Why not the mockdate? How can I fix this so the snaphot stays the same ?

I'm using mockdate util for that. It allows you to mock "now" value for the test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM