Reputation:
beforeAll(() => {
...
const mockedData = '2020-11-26T00:00:00.000Z'
jest.spyOn(global, 'Date').mockImplementation(() => mockedData)
Date.now = () => 1606348800
})
describe('getIventory', () => {
it('should make the appropriate fetch call', async () => {
await inventoryStore.getInventory()
expect(db.listInv).toHaveBeenCalledWith(query)
})
})
I get:
Object {
"expected": Object {
- "$lte": "2020-11-26T00:00:00.000Z",
+ "$lte": mockConstructor {},
}
},
This is my query object:
const query = {
expected: { $lte: '2020-11-26T00:00:00.000Z' },
}
Is there a reason why the mockImplementation isn't working? Is there an easier more straightforward way to do this?
Tried a bunch of things including:
const spy = jest.spyOn(global, 'Date')
const date = spy.mock.instances[0]
const queryMocked = {
expected: { $lte: date },
}
I am getting: Property 'setSystemTime' does not exist on type 'typeof jest'.
I think it's one of the solutions I've tried, but I didn't mention it, because it's also the first one that failed.
Upvotes: 0
Views: 3270
Reputation: 124
The mock for Date.now should look like
Date.now = jest.fn(() => 1606348800)
You can also use a fake timer, provided by jest
jest
.setSystemTime(new Date('2020-01-01'));
This post may provide additional help
Upvotes: 2