Skip to content
Myr

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

3 files+55 added−42 removed

modified:src/jsx/components.test.tsx+1 added-1 removed
587 unmodified lines
588 )589 })590591 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 lines
modified:src/jsx/context-isolation.test.ts+50 added-37 removed
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'23const importIndex = () =>4 // @ts-expect-error es6 module target disallows dynamic import5 import('./index')6const importStreaming = () =>7 // @ts-expect-error es6 module target disallows dynamic import8 import('./streaming')9const importHooks = () =>10 // @ts-expect-error es6 module target disallows dynamic import11 import('./hooks')12const importAsyncHooks = () =>13 // @ts-expect-error es6 module target disallows dynamic import14 import('node:async_hooks')1516/**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-style
24 unmodified lines
44 warnSpy.mockRestore()45 })4647 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()4950 const SessionContext = createContext<{ username: string; role: string }>({51 username: 'nobody',52 role: 'none',
58 unmodified lines
111 expect(attackerHtml).toBe('<div>Access Denied for nobody</div>')112 })113114 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()116117 const ThemeContext = createContext('default')118 const Reader = async () => {119 await new Promise((r) => setTimeout(r, 10))
10 unmodified lines
130 expect(warnSpy.mock.calls[0][0]).toContain('AsyncLocalStorage')131 })132133 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()135136 const ThemeContext = createContext('default')137 const Broken = async () => {138 await Promise.resolve()
9 unmodified lines
148 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 }157145 const { createContext, useContext, jsx } = await import('./index')158 const { createContext, useContext, jsx } = await importIndex()159160 const ThemeContext = createContext('default')161 const Reader = async () => {162 await new Promise((r) => setTimeout(r, 10))
6 unmodified lines
169 expect(warnSpy).not.toHaveBeenCalled()170 })171172 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()174175 const ThemeContext = createContext('light')176 const Consumer = () => jsx('span', {}, useContext(ThemeContext))177
13 unmodified lines
191 return html192 }193194 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()196197 const ThemeContext = createContext('default')198 const Inner = () => jsx('span', {}, useContext(ThemeContext))199 const Outer = async () => {
10 unmodified lines
210 expect(warnSpy).not.toHaveBeenCalled()211 })212213 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()217218 // 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 lines
236 expect(warnSpy).not.toHaveBeenCalled()237 })238239 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()243244 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))245 const ThemeContext = createContext('default')246 const Content = () => {
17 unmodified lines
264 expect(warnSpy).not.toHaveBeenCalled()265 })266267 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()271272 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 lines
293 expect(warnSpy).not.toHaveBeenCalled()294 })295296 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()300301 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))302 const ThemeContext = createContext('default')303 const Inner = () => jsx('span', {}, useContext(ThemeContext))
23 unmodified lines
327 expect(warnSpy).not.toHaveBeenCalled()328 })329330 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()334335 const ThemeContext = createContext('default')336 const waits = new Map<string, Promise<void>>()337 const entered = new Map<string, () => void>()
63 unmodified lines
401 expect(warnSpy).not.toHaveBeenCalled()402 })403404 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()407408 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))409 let resolved: string | undefined410 deferred.then((value) => {
19 unmodified lines
430 expect(warnSpy).not.toHaveBeenCalled()431 })432433 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()436437 const deferred = new Promise<string>((resolve) => setTimeout(() => resolve('done'), 10))438 let resolved: string | undefined439 deferred.then((value) => {
23 unmodified lines
463 expect(warnSpy).not.toHaveBeenCalled()464 })465466 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()469470 const deferred = new Promise((_resolve, reject) =>471 setTimeout(() => reject(new Error('boom')), 10)472 )
24 unmodified lines
497 expect(warnSpy).not.toHaveBeenCalled()498 })499500 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()503504 const deferred = new Promise((_resolve, reject) =>505 setTimeout(() => reject(new Error('boom')), 10)506 )
24 unmodified lines
531 expect(warnSpy).not.toHaveBeenCalled()532 })533534 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()537538 const deferred = new Promise((_resolve, reject) =>539 setTimeout(() => reject(new Error('boom')), 10)540 )
18 unmodified lines
559 expect(warnSpy).not.toHaveBeenCalled()560 })561562 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()565566 const deferred = new Promise((_resolve, reject) =>567 setTimeout(() => reject(new Error('boom')), 10)568 )
23 unmodified lines
592 expect(warnSpy).not.toHaveBeenCalled()593 })594595 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()597598 const ThemeContext = createContext('default')599 const Content = () => {600 throw new Error('boom')
16 unmodified lines
617 expect(warnSpy).not.toHaveBeenCalled()618 })619620 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()622623 const ThemeContext = createContext('default')624 const Content = () => {625 throw new Error('boom')
16 unmodified lines
642 expect(warnSpy).not.toHaveBeenCalled()643 })644645 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()647648 const ThemeContext = createContext('default')649 const Content = () => {650 throw new Error('boom')
13 unmodified lines
modified:src/jsx/index.test.tsx+4 added-4 removed
1278 unmodified lines
1279 const resume = captureRenderContext()1280 return resume(async () => {1281 markReaderEntered()1282 await readerWait1283 return useContext(ThemeContext)1283 return <>{useContext(ThemeContext)}</>1284 })1285 }12861287 const htmlPromise = (
14 unmodified lines
1302 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 lines
1334 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 lines
1471 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