fix(jsx): make merged context-isolation tests pass tsc type check (#5037)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 6735fea · Browse files · 3 files·+55 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+55 added−42 removed
- tests:+55 added−42 removed
Provenance
- Commit
d4853a8- Tree
de0871c- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
6735fea
3 files changed+55 added−42 removed
modified:src/jsx/components.test.tsx+1 added-1 removed
587 unmodified lines588 )589 })590 591 it('waits for an in-flight async fallback when multiple children reject', async () => {592- let resolveFallback!: (value: HtmlEscapedString) => void592+ let resolveFallback!: (value: HtmlEscapedString | PromiseLike<HtmlEscapedString>) => void593 const fallback = new Promise<HtmlEscapedString>((resolve) => {594 resolveFallback = resolve595 })596 205 unmodified linesmodified:src/jsx/context-isolation.test.ts+50 added-37 removed
1 import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'2 3+const importIndex = () =>4+ // @ts-expect-error es6 module target disallows dynamic import5+ import('./index')6+const importStreaming = () =>7+ // @ts-expect-error es6 module target disallows dynamic import8+ import('./streaming')9+const importHooks = () =>10+ // @ts-expect-error es6 module target disallows dynamic import11+ import('./hooks')12+const importAsyncHooks = () =>13+ // @ts-expect-error es6 module target disallows dynamic import14+ import('node:async_hooks')15+16 /**17 * These tests exercise the non-`AsyncLocalStorage` fallback path. They force18 * every `AsyncLocalStorage` acquisition route to fail (and keep it disabled19 * while the module lazily probes for it), then verify the React-style24 unmodified lines44 warnSpy.mockRestore()45 })46 47 it('falls back to the default value after await and never leaks across concurrent renders', async () => {35- const { createContext, useContext, jsx } = await import('./index')48+ const { createContext, useContext, jsx } = await importIndex()49 50 const SessionContext = createContext<{ username: string; role: string }>({51 username: 'nobody',52 role: 'none',58 unmodified lines111 expect(attackerHtml).toBe('<div>Access Denied for nobody</div>')112 })113 114 it('warns once when a post-await read falls back to the default value', async () => {102- const { createContext, useContext, jsx } = await import('./index')115+ const { createContext, useContext, jsx } = await importIndex()116 117 const ThemeContext = createContext('default')118 const Reader = async () => {119 await new Promise((r) => setTimeout(r, 10))10 unmodified lines130 expect(warnSpy.mock.calls[0][0]).toContain('AsyncLocalStorage')131 })132 133 it('clears the fallback warning window after an async render rejects', async () => {121- const { createContext, useContext, jsx } = await import('./index')134+ const { createContext, useContext, jsx } = await importIndex()135 136 const ThemeContext = createContext('default')137 const Broken = async () => {138 await Promise.resolve()9 unmodified lines148 it('finds AsyncLocalStorage via process.mainModule.require (Node < 20.16 CJS)', async () => {149 // Re-enable only the legacy route: a CJS entrypoint exposes the main150 // module's `require` even where `process.getBuiltinModule` doesn't exist.151 // Context must then survive `await` with no warning.139- const { AsyncLocalStorage } = await import('node:async_hooks')152+ const { AsyncLocalStorage } = await importAsyncHooks()153 ;(process as { mainModule?: unknown }).mainModule = {154 require: (specifier: string) =>155 specifier === 'node:async_hooks' ? { AsyncLocalStorage } : {},156 }157 145- const { createContext, useContext, jsx } = await import('./index')158+ const { createContext, useContext, jsx } = await importIndex()159 160 const ThemeContext = createContext('default')161 const Reader = async () => {162 await new Promise((r) => setTimeout(r, 10))6 unmodified lines169 expect(warnSpy).not.toHaveBeenCalled()170 })171 172 it('keeps a synchronous render synchronous and does not warn', async () => {160- const { createContext, useContext, jsx } = await import('./index')173+ const { createContext, useContext, jsx } = await importIndex()174 175 const ThemeContext = createContext('light')176 const Consumer = () => jsx('span', {}, useContext(ThemeContext))177 13 unmodified lines191 return html192 }193 194 it('restores provider values for the subtree returned by a resumed async component', async () => {182- const { createContext, useContext, jsx } = await import('./index')195+ const { createContext, useContext, jsx } = await importIndex()196 197 const ThemeContext = createContext('default')198 const Inner = () => jsx('span', {}, useContext(ThemeContext))199 const Outer = async () => {10 unmodified lines210 expect(warnSpy).not.toHaveBeenCalled()211 })212 213 it('resumes a Suspense-deferred subtree in the captured request store', async () => {201- const { createContext, useContext, jsx } = await import('./index')202- const { Suspense, renderToReadableStream } = await import('./streaming')203- const { use } = await import('./hooks')214+ const { createContext, useContext, jsx } = await importIndex()215+ const { Suspense, renderToReadableStream } = await importStreaming()216+ const { use } = await importHooks()217 218 // A `use()`-suspended child takes the deferred re-render path, which on the219 // fallback path re-establishes the captured render store synchronously.220 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))15 unmodified lines236 expect(warnSpy).not.toHaveBeenCalled()237 })238 239 it('resumes a Suspense-deferred subtree with outer provider values', async () => {227- const { createContext, useContext, jsx } = await import('./index')228- const { Suspense, renderToReadableStream } = await import('./streaming')229- const { use } = await import('./hooks')240+ const { createContext, useContext, jsx } = await importIndex()241+ const { Suspense, renderToReadableStream } = await importStreaming()242+ const { use } = await importHooks()243 244 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))245 const ThemeContext = createContext('default')246 const Content = () => {17 unmodified lines264 expect(warnSpy).not.toHaveBeenCalled()265 })266 267 it('renders Suspense fallback with outer provider values', async () => {255- const { createContext, useContext, jsx } = await import('./index')256- const { Suspense, renderToReadableStream } = await import('./streaming')257- const { use } = await import('./hooks')268+ const { createContext, useContext, jsx } = await importIndex()269+ const { Suspense, renderToReadableStream } = await importStreaming()270+ const { use } = await importHooks()271 272 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))273 const ThemeContext = createContext('default')274 const Fallback = () => jsx('p', {}, `Loading ${useContext(ThemeContext)}`)18 unmodified lines293 expect(warnSpy).not.toHaveBeenCalled()294 })295 296 it('restores nested provider values in a resumed Suspense subtree', async () => {284- const { createContext, useContext, jsx, Fragment } = await import('./index')285- const { Suspense, renderToReadableStream } = await import('./streaming')286- const { use } = await import('./hooks')297+ const { createContext, useContext, jsx, Fragment } = await importIndex()298+ const { Suspense, renderToReadableStream } = await importStreaming()299+ const { use } = await importHooks()300 301 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))302 const ThemeContext = createContext('default')303 const Inner = () => jsx('span', {}, useContext(ThemeContext))23 unmodified lines327 expect(warnSpy).not.toHaveBeenCalled()328 })329 330 it('keeps sibling Suspense resume contexts isolated when they resolve out of order', async () => {318- const { createContext, useContext, jsx, Fragment } = await import('./index')319- const { Suspense, renderToReadableStream } = await import('./streaming')320- const { use } = await import('./hooks')331+ const { createContext, useContext, jsx, Fragment } = await importIndex()332+ const { Suspense, renderToReadableStream } = await importStreaming()333+ const { use } = await importHooks()334 335 const ThemeContext = createContext('default')336 const waits = new Map<string, Promise<void>>()337 const entered = new Map<string, () => void>()63 unmodified lines401 expect(warnSpy).not.toHaveBeenCalled()402 })403 404 it('resumes ErrorBoundary-deferred children in the captured request store', async () => {392- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')393- const { renderToReadableStream } = await import('./streaming')405+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()406+ const { renderToReadableStream } = await importStreaming()407 408 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))409 let resolved: string | undefined410 deferred.then((value) => {19 unmodified lines430 expect(warnSpy).not.toHaveBeenCalled()431 })432 433 it('resumes ErrorBoundary-deferred children with outer provider values', async () => {421- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')422- const { renderToReadableStream } = await import('./streaming')434+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()435+ const { renderToReadableStream } = await importStreaming()436 437 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))438 let resolved: string | undefined439 deferred.then((value) => {23 unmodified lines463 expect(warnSpy).not.toHaveBeenCalled()464 })465 466 it('resumes ErrorBoundary fallback rendering with outer provider values', async () => {454- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')455- const { renderToReadableStream } = await import('./streaming')467+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()468+ const { renderToReadableStream } = await importStreaming()469 470 const deferred = new Promise((_resolve, reject) =>471 setTimeout(() => reject(new Error('boom')), 10)472 )24 unmodified lines497 expect(warnSpy).not.toHaveBeenCalled()498 })499 500 it('resumes ErrorBoundary fallbackRender components with outer provider values', async () => {488- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')489- const { renderToReadableStream } = await import('./streaming')501+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()502+ const { renderToReadableStream } = await importStreaming()503 504 const deferred = new Promise((_resolve, reject) =>505 setTimeout(() => reject(new Error('boom')), 10)506 )24 unmodified lines531 expect(warnSpy).not.toHaveBeenCalled()532 })533 534 it('resumes ErrorBoundary fallback prop rendering with outer provider values', async () => {522- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')523- const { renderToReadableStream } = await import('./streaming')535+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()536+ const { renderToReadableStream } = await importStreaming()537 538 const deferred = new Promise((_resolve, reject) =>539 setTimeout(() => reject(new Error('boom')), 10)540 )18 unmodified lines559 expect(warnSpy).not.toHaveBeenCalled()560 })561 562 it('resumes thenable ErrorBoundary fallback prop rendering with outer provider values', async () => {550- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')551- const { renderToReadableStream } = await import('./streaming')563+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()564+ const { renderToReadableStream } = await importStreaming()565 566 const deferred = new Promise((_resolve, reject) =>567 setTimeout(() => reject(new Error('boom')), 10)568 )23 unmodified lines592 expect(warnSpy).not.toHaveBeenCalled()593 })594 595 it('resumes synchronous ErrorBoundary fallback rendering with outer provider values', async () => {583- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')596+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()597 598 const ThemeContext = createContext('default')599 const Content = () => {600 throw new Error('boom')16 unmodified lines617 expect(warnSpy).not.toHaveBeenCalled()618 })619 620 it('resumes synchronous ErrorBoundary fallbackRender components with outer provider values', async () => {608- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')621+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()622 623 const ThemeContext = createContext('default')624 const Content = () => {625 throw new Error('boom')16 unmodified lines642 expect(warnSpy).not.toHaveBeenCalled()643 })644 645 it('resumes synchronous ErrorBoundary fallback prop rendering with outer provider values', async () => {633- const { createContext, useContext, jsx, ErrorBoundary } = await import('./index')646+ const { createContext, useContext, jsx, ErrorBoundary } = await importIndex()647 648 const ThemeContext = createContext('default')649 const Content = () => {650 throw new Error('boom')13 unmodified linesmodified:src/jsx/index.test.tsx+4 added-4 removed
1278 unmodified lines1279 const resume = captureRenderContext()1280 return resume(async () => {1281 markReaderEntered()1282 await readerWait1283- return useContext(ThemeContext)1283+ return <>{useContext(ThemeContext)}</>1284 })1285 }1286 1287 const htmlPromise = (14 unmodified lines1302 resume(() => {1303 throw new Error('boom')1304 })1305 } catch {}1306- return useContext(ThemeContext)1306+ return <>{useContext(ThemeContext)}</>1307 }1308 const CaptureOuter = () => {1309 const resume = captureRenderContext()1310 return (23 unmodified lines1334 await Promise.resolve()1335 throw new Error('boom')1336 })1337 } catch {}1338- return useContext(ThemeContext)1338+ return <>{useContext(ThemeContext)}</>1339 }1340 const CaptureOuter = () => {1341 const resume = captureRenderContext()1342 return (128 unmodified lines1471 const Broken = () => {1472 throw new Error('boom')1473 }1474 const fallback = {1475- then(resolve: (value: JSX.Element) => void) {1475+ then(resolve: (value: unknown) => void) {1476 resolve(<span>Recovered</span>)1477 },1478 }1479 103 unmodified lines