withPage

tcmts.react.test.withPage · #4nl6pbfk · term · runs anywhere

(with the page's globals set as the runtime sets them, then put back)

export const withPage = (globals: Record<string, unknown>, f: () => void) => {
  const g = globalThis as Record<string, unknown>;
  const before = Object.fromEntries(Object.keys(globals).map((k) => [k, g[k]]));
  Object.assign(g, globals);
  try {
    f();
  } finally {
    for (const [k, v] of Object.entries(before))
      if (v === undefined) delete g[k];
      else g[k] = v;
  }
}

used by