remove system time mock

This commit is contained in:
Ben Irvin 2023-04-28 16:44:04 +02:00
parent 5b175a71f9
commit f541b0868c
2 changed files with 14 additions and 8 deletions

View File

@ -20,19 +20,22 @@ describe('API Token', () => {
hexedString: '6170692d746f6b656e5f746573742d72616e646f6d2d6279746573', hexedString: '6170692d746f6b656e5f746573742d72616e646f6d2d6279746573',
}; };
const now = Date.now(); let now;
let nowSpy;
beforeAll(() => { beforeAll(() => {
jest jest
.spyOn(crypto, 'randomBytes') .spyOn(crypto, 'randomBytes')
.mockImplementation(() => Buffer.from(mockedApiToken.randomBytes)); .mockImplementation(() => Buffer.from(mockedApiToken.randomBytes));
jest.useFakeTimers('modern').setSystemTime(now); // To eliminate latency in the request and predict the expiry timestamp, we freeze Date.now()
now = Date.now();
nowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now);
}); });
afterAll(() => { afterAll(() => {
nowSpy.mockRestore();
jest.clearAllMocks(); jest.clearAllMocks();
jest.useRealTimers();
}); });
describe('create', () => { describe('create', () => {

View File

@ -27,19 +27,22 @@ describe('Transfer Token', () => {
hexedString: '7472616e736665722d746f6b656e5f746573742d72616e646f6d2d6279746573', hexedString: '7472616e736665722d746f6b656e5f746573742d72616e646f6d2d6279746573',
}; };
const now = Date.now(); let now;
let nowSpy;
beforeAll(() => { beforeAll(() => {
jest jest
.spyOn(crypto, 'randomBytes') .spyOn(crypto, 'randomBytes')
.mockImplementation(() => Buffer.from(mockedTransferToken.randomBytes)); .mockImplementation(() => Buffer.from(mockedTransferToken.randomBytes));
jest.useFakeTimers('modern').setSystemTime(now); // To eliminate latency in the request and predict the expiry timestamp, we freeze Date.now()
now = Date.now();
nowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now);
}); });
afterAll(() => { afterAll(() => {
nowSpy.mockRestore();
jest.clearAllMocks(); jest.clearAllMocks();
jest.useRealTimers();
}); });
describe('create', () => { describe('create', () => {