Home > other >  Typescript / How to expect Fixed date?
Typescript / How to expect Fixed date?

Time:11-03

I am currently trying to test a fixed date. How can I customize my code so that I return a fixed date:

it.('reports fix date', async () => {

  aService.returnValue = [{ 
     activeFrom: '2020-04-19T10:01:58.135Z'
  }];

const somethingSpy = jest.spyOn(PubSub, 'topic');

await service.report([ 
 {
  activeFrom: addDays(new Date(), 30),
 }
]},
expect(somethingSpy).toHaveBeenCalledWith(

  expect.objectContaining({ 
      activeFrom: '2020-04-19T10:01:58.135Z'
  })    
)

CodePudding user response:

I would recommend using a library like MockDate to override the new Date() constructor and replace it with a fixed value.

MockDate.set('2000-11-22');

new Date().toString() // "Tue Nov 21 2000 18:00:00 GMT-0600 (CST)"
  • Related