Fix flaky unit test (#2479)

This commit is contained in:
Devin Binnie 2022-12-16 17:18:16 -04:00 committed by GitHub
parent 7bbcbccbd3
commit a089d51d0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,12 +6,6 @@ import config from 'common/config';
import {addDurationToFnReturnObject, boundsOk, browserWindowVisibilityStatus, checkPathPermissions, truncateString, webContentsCheck} from './utils';
const sleep = (ms) => new Promise((resolve) => {
setTimeout(() => resolve(), ms);
});
const timeToSleep = 100;
jest.mock('fs', () => ({
promises: {
access: jest.fn(),
@ -26,13 +20,14 @@ jest.mock('common/config', () => ({
describe('main/diagnostics/utils', () => {
describe('addDurationToFnReturnObject', () => {
it('should measure the execution time of a function and include it in the response', async () => {
const now = jest.spyOn(Date, 'now');
now.mockReturnValue(0);
const functionToMeasure = async () => {
await sleep(timeToSleep);
now.mockReturnValue(100);
};
const fn = addDurationToFnReturnObject(functionToMeasure);
const b = await fn();
expect(b.duration).toBeGreaterThan(timeToSleep - 10);
expect(b.duration).toBeLessThan(timeToSleep * 1.5);
expect(b.duration).toBe(100);
});
});