diff --git a/packages/kerosene-ui/package.json b/packages/kerosene-ui/package.json index 1265c3b..e2eecc1 100644 --- a/packages/kerosene-ui/package.json +++ b/packages/kerosene-ui/package.json @@ -1,6 +1,6 @@ { "name": "@kablamo/kerosene-ui", - "version": "0.0.26", + "version": "0.0.27", "repository": "https://github.com/KablamoOSS/kerosene/tree/master/packages/kerosene-ui", "bugs": { "url": "https://github.com/KablamoOSS/kerosene/issues" @@ -22,7 +22,7 @@ }, "dependencies": { "@babel/runtime": "^7.18.9", - "@kablamo/kerosene": "^0.0.26", + "@kablamo/kerosene": "^0.0.27", "@types/lodash": "^4.14.184", "lodash": "^4.17.21" }, diff --git a/packages/kerosene/package.json b/packages/kerosene/package.json index 869ed2f..f0b1d45 100644 --- a/packages/kerosene/package.json +++ b/packages/kerosene/package.json @@ -1,6 +1,6 @@ { "name": "@kablamo/kerosene", - "version": "0.0.26", + "version": "0.0.27", "repository": "https://github.com/KablamoOSS/kerosene/tree/master/packages/kerosene", "bugs": { "url": "https://github.com/KablamoOSS/kerosene/issues" diff --git a/packages/kerosene/src/function/waitForEventLoopToDrain.ts b/packages/kerosene/src/function/waitForEventLoopToDrain.ts index d839715..03322bf 100644 --- a/packages/kerosene/src/function/waitForEventLoopToDrain.ts +++ b/packages/kerosene/src/function/waitForEventLoopToDrain.ts @@ -1,7 +1,7 @@ import globalThis from "core-js-pure/features/global-this"; -// Grab a reference to the global `setImmediate` so mocking libraries like `sinon` don't interfere -const { setImmediate } = globalThis; +// Grab a reference to the global `setImmediate` and `setTimeout` so mocking libraries like `sinon` don't interfere +const { setImmediate, setTimeout } = globalThis; /** * Returns a Promise that resolves when the event loop has drained @@ -14,6 +14,20 @@ const { setImmediate } = globalThis; */ export default function waitForEventLoopToDrain(): Promise { return new Promise((resolve) => { - setImmediate(resolve); + if (setImmediate) { + setImmediate(resolve); + } else if ( + typeof jest !== "undefined" && + typeof jest.requireActual === "function" + ) { + // jest-environment-jsdom removes `setImmediate` from the Node globals, so instead use jest to require the Node + // API from the "timers" module. We can't just do `import { setImmediate } from "timers";` because of the browser + jest + .requireActual("timers") + .setImmediate(resolve); + } else { + // Fallback to setTimeout + setTimeout(resolve, 0); + } }); }