|
| 1 | +import * as jest from "jest-circus-browser/dist/umd/jest-circus"; |
| 2 | +import expectMod from "expect/build-es5/index"; |
| 3 | + |
| 4 | +type CallSignature<T extends (...a: any[]) => unknown> = ( |
| 5 | + ...a: Parameters<T> |
| 6 | +) => ReturnType<T>; |
| 7 | + |
| 8 | +declare global { |
| 9 | + var __jest__: typeof jest; |
| 10 | + var expect: typeof expectMod; |
| 11 | + // We need to extract only the call signature as `autoReady(jest.describe)` drops all the other properties |
| 12 | + var describe: CallSignature<typeof jest.describe>; |
| 13 | + var it: CallSignature<typeof jest.it>; |
| 14 | + var READY: (arg: string) => void; |
| 15 | + var nsObj: (obj: any) => any; |
| 16 | + |
| 17 | + interface Window { |
| 18 | + NEXT_HYDRATED?: boolean; |
| 19 | + onNextHydrated?: () => void; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +let isReady = false; |
| 24 | +function autoReady<T extends (...a: any[]) => unknown>( |
| 25 | + fn: (...args: Parameters<T>) => ReturnType<T> |
| 26 | +): (...args: Parameters<T>) => ReturnType<T> { |
| 27 | + return (...args) => { |
| 28 | + if (!isReady) { |
| 29 | + isReady = true; |
| 30 | + setImmediate(() => { |
| 31 | + READY(""); |
| 32 | + }); |
| 33 | + } |
| 34 | + return fn(...args); |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +globalThis.__jest__ = jest; |
| 39 | +globalThis.expect = expectMod; |
| 40 | +globalThis.describe = autoReady(jest.describe); |
| 41 | +globalThis.it = autoReady(jest.it); |
| 42 | + |
| 43 | +// From https://github.com/webpack/webpack/blob/9fcaa243573005d6fdece9a3f8d89a0e8b399613/test/TestCases.template.js#L422 |
| 44 | +globalThis.nsObj = function nsObj(obj) { |
| 45 | + Object.defineProperty(obj, Symbol.toStringTag, { |
| 46 | + value: "Module", |
| 47 | + }); |
| 48 | + return obj; |
| 49 | +}; |
| 50 | + |
| 51 | +function wait(ms: number): Promise<void> { |
| 52 | + return new Promise((resolve) => { |
| 53 | + setTimeout(resolve, ms); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +async function waitForPath(contentWindow: Window, path: string): Promise<void> { |
| 58 | + while (true) { |
| 59 | + if (contentWindow.location.pathname === path) { |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + await wait(1); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export function waitForHydration( |
| 68 | + iframe: HTMLIFrameElement, |
| 69 | + path: string |
| 70 | +): Promise<void> { |
| 71 | + return new Promise((resolve) => { |
| 72 | + if ( |
| 73 | + iframe.contentDocument != null && |
| 74 | + iframe.contentDocument.readyState === "complete" |
| 75 | + ) { |
| 76 | + waitForHydrationAndResolve(iframe.contentWindow!, path).then(resolve); |
| 77 | + } else { |
| 78 | + iframe.addEventListener("load", () => { |
| 79 | + waitForHydrationAndResolve(iframe.contentWindow!, path).then(resolve); |
| 80 | + }); |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +async function waitForHydrationAndResolve( |
| 86 | + contentWindow: Window, |
| 87 | + path: string |
| 88 | +): Promise<void> { |
| 89 | + await waitForPath(contentWindow, path); |
| 90 | + |
| 91 | + return await new Promise((resolve) => { |
| 92 | + if (contentWindow.NEXT_HYDRATED) { |
| 93 | + resolve(); |
| 94 | + } else { |
| 95 | + contentWindow.onNextHydrated = () => { |
| 96 | + resolve(); |
| 97 | + }; |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +export function markAsHydrated() { |
| 103 | + window.NEXT_HYDRATED = true; |
| 104 | + if (typeof window.onNextHydrated === "function") { |
| 105 | + window.onNextHydrated(); |
| 106 | + } |
| 107 | +} |
0 commit comments