From 9c420d6f1e33a6967f0ea4ede48ed7e42a495357 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Thu, 16 Jun 2022 16:57:32 +0300 Subject: [PATCH] Try solving flaky unit test by switch to use different implementation of setImmediate (#5655) Signed-off-by: Janne Savolainen --- src/common/test-utils/flush-promises.ts | 4 +++- src/jest.setup.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/common/test-utils/flush-promises.ts b/src/common/test-utils/flush-promises.ts index e9f0ebea69..c2fdeff99e 100644 --- a/src/common/test-utils/flush-promises.ts +++ b/src/common/test-utils/flush-promises.ts @@ -2,4 +2,6 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -export const flushPromises = () => new Promise(resolve => setTimeout(resolve, 0)); +import { setImmediate } from "timers"; + +export const flushPromises = () => new Promise(setImmediate); diff --git a/src/jest.setup.ts b/src/jest.setup.ts index beb5d7d9e2..2ab9b1b52d 100644 --- a/src/jest.setup.ts +++ b/src/jest.setup.ts @@ -6,6 +6,7 @@ import fetchMock from "jest-fetch-mock"; import configurePackages from "./common/configure-packages"; import { configure } from "mobx"; +import { setImmediate } from "timers"; // setup default configuration for external npm-packages configurePackages(); @@ -22,7 +23,7 @@ fetchMock.enableMocks(); // Mock __non_webpack_require__ for tests globalThis.__non_webpack_require__ = jest.fn(); -global.setImmediate = global.setImmediate ?? ((callback: (...args: TArgs) => void, ...args: TArgs) => setTimeout(() => callback(...args), 0)); +global.setImmediate = setImmediate; global.fail = ((error = "Test failed without explicit error") => { console.error(error);