diff --git a/package.json b/package.json index 9aa42035d..f70202272 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "scripts": { "bench": "tsc && node --expose-gc dist/benchmark", "format": "prettier --write .", - "test": "tsc && c8 --reporter lcov node --trace-warnings --test dist/test", + "test": "tsc && c8 --reporter lcov --reporter text node --trace-warnings --test dist/test", "test:only": "tsc; node --trace-warnings --test-only --test dist/test" }, "dependencies": { diff --git a/register.js b/register.js index c365f868a..ba8a10f71 100644 --- a/register.js +++ b/register.js @@ -4,6 +4,7 @@ let root; try { root = Function('return this')(); + /* c8 ignore next 3 */ } catch (_) { root = window; } diff --git a/suspense.js b/suspense.js index 6a63a32a1..fdc41e53c 100644 --- a/suspense.js +++ b/suspense.js @@ -57,7 +57,7 @@ function Suspense(props) { const fallback = contentsToString([props.fallback]); if (!props.children) { - return fallback; + return ''; } const children = contentsToString([props.children]); diff --git a/test/error-boundary.test.tsx b/test/error-boundary.test.tsx index 2086e0d11..8a04a8228 100644 --- a/test/error-boundary.test.tsx +++ b/test/error-boundary.test.tsx @@ -2,7 +2,7 @@ import assert from 'node:assert'; import { describe, it } from 'node:test'; import { setTimeout } from 'timers/promises'; import Html from '../'; -import { ErrorBoundary } from '../error-boundary'; +import { ErrorBoundary, isTimeoutError } from '../error-boundary'; describe('Error Boundary', () => { it('should render error boundary', async () => { @@ -63,4 +63,34 @@ describe('Error Boundary', () => { assert.equal(html,
2
); }); + + it('Catches timed out promise', async () => { + const html = await ( + <> + { + assert.ok(isTimeoutError(err)); + return
1
; + }} + timeout={5} + > + {setTimeout(10,
2
)} +
+ + ); + + assert.equal(html, '
1
'); + }); + + it('doesnt do nothing on sync children', () => { + const html = ( + <> + 1}> +
2
+
+ + ); + + assert.equal(html, '
2
'); + }); }); diff --git a/test/suspense.test.tsx b/test/suspense.test.tsx index a4c497683..f261299e3 100644 --- a/test/suspense.test.tsx +++ b/test/suspense.test.tsx @@ -566,6 +566,29 @@ describe('Suspense', () => { assert.ok(stream.closed); }); + + it('tests suspense without children', async () => { + assert.equal( + await renderToString((r) => ( + //@ts-expect-error - testing invalid children + 1} /> + )), + '' + ); + }); + + it('throws if no stream is used', () => { + SUSPENSE_ROOT.enabled = true; + + assert.throws( + () => ( + + {Promise.resolve(2)} + + ), + /Suspense resource closed before all suspense components were resolved./ + ); + }); }); describe('Suspense errors', () => { @@ -690,4 +713,30 @@ describe('Suspense errors', () => { ); }); + + it('tests suspense with error boundary', async () => { + const err = new Error('component failed'); + + // Sync does not needs autoScript + assert.equal( + await renderToString((r) => ( + 1} catch={
3
}> + {Promise.reject(err)} +
+ )), + <> +
+
1
+
+ + {SuspenseScript} + + + + ); + }); });