🎥 Make videos programmatically with React
Docs: Testing Remotion components / `noSuspense` prop for Player+Thumbnail
Jonny Burgerca69724ea69d
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsxindex c7e8b8bd71..b6e7c30d60 100644--- a/packages/core/src/Composition.tsx+++ b/packages/core/src/Composition.tsx@@ -139,6 +139,7 @@ export const Composition = <const lazy = useLazyComponent<Props>({compProps: compProps as CompProps<Props>,componentName: 'Composition',+ noSuspense: false,});const nonce = useNonce();const isPlayer = useIsPlayer();diff --git a/packages/core/src/use-lazy-component.ts b/packages/core/src/use-lazy-component.tsindex b5f30d0889..a6393c4dcf 100644--- a/packages/core/src/use-lazy-component.ts+++ b/packages/core/src/use-lazy-component.ts@@ -16,31 +16,16 @@ type LazyExoticComponent<T extends ComponentType<any>> = ExoticComponent<export const useLazyComponent = <Props>({compProps,componentName,+ noSuspense,}: {compProps: CompProps<Props>;componentName: string;+ noSuspense: boolean;}): LazyExoticComponent<ComponentType<Props>> => {const lazy = useMemo(() => {- if (- 'lazyComponent' in compProps &&- typeof compProps.lazyComponent !== 'undefined'- ) {- if (typeof compProps.lazyComponent === 'undefined') {- throw new Error(- `A value of \`undefined\` was passed to the \`lazyComponent\` prop. Check the value you are passing to the <${componentName}/> component.`,- );- }-- return React.lazy(- compProps.lazyComponent as () => Promise<{- default: ComponentType<Props>;- }>,- );- }-if ('component' in compProps) {// In SSR, suspense is not yet supported, we cannot use React.lazy- if (typeof document === 'undefined') {+ if (typeof document === 'undefined' || noSuspense) {return compProps.component as unknown as React.LazyExoticComponent<ComponentType<Props>>;@@ -57,6 +42,23 @@ export const useLazyComponent = <Props>({);}+ if (+ 'lazyComponent' in compProps &&+ typeof compProps.lazyComponent !== 'undefined'+ ) {+ if (typeof compProps.lazyComponent === 'undefined') {+ throw new Error(+ `A value of \`undefined\` was passed to the \`lazyComponent\` prop. Check the value you are passing to the <${componentName}/> component.`,+ );+ }++ return React.lazy(+ compProps.lazyComponent as () => Promise<{+ default: ComponentType<Props>;+ }>,+ );+ }+throw new Error("You must pass either 'component' or 'lazyComponent'");// Very important to leave the dependencies as they are, or insteaddiff --git a/packages/docs/docs/player/api.mdx b/packages/docs/docs/player/api.mdxindex 22852cea8f..534bdb446e 100644--- a/packages/docs/docs/player/api.mdx+++ b/packages/docs/docs/player/api.mdx@@ -472,6 +472,10 @@ A HTML class name to be used in place of the default `__remotion-player`.<Options id="log" />+### `noSuspense`<AvailableFrom v="4.0.271" />++Disables React Suspense, which is [useful for writing tests](/docs/player/thumbnail).+### `acknowledgeRemotionLicense?`<AvailableFrom v="4.0.253" />Acknowledge the [Remotion License](/docs/license) to make the console message disappear.diff --git a/packages/docs/docs/player/thumbnail.mdx b/packages/docs/docs/player/thumbnail.mdxindex ea8acea54b..93155539e3 100644--- a/packages/docs/docs/player/thumbnail.mdx+++ b/packages/docs/docs/player/thumbnail.mdx@@ -150,6 +150,10 @@ Makes the Player render things outside of the canvas. Useful if you have interac<Options id="log" />+### `noSuspense`<AvailableFrom v="4.0.271" />++Disables React Suspense, which is [useful for writing tests](/docs/player/thumbnail).+## ThumbnailRefYou may attach a ref to the thumbnail and get some layout info.diff --git a/packages/docs/docs/testing.mdx b/packages/docs/docs/testing.mdxnew file mode 100644index 0000000000..e803428442--- /dev/null+++ b/packages/docs/docs/testing.mdx@@ -0,0 +1,62 @@+---+image: /generated/articles-docs-testing.png+id: testing+title: Testing Remotion components+sidebar_label: Testing+crumb: Tooling+---++Remotion components are regular React component, albeit they need to be wrapped in some extra contexts to render them.++You can approach testing Remotion components the same way as testing regular React components, by using some of these popular libraries:++- [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/)+- [Bun + Happy DOM](https://bun.sh/guides/test/happy-dom)+- [Playwright](https://playwright.dev/) for end-to-end tests++## Adding Remotion contexts++To add the contexts that Remotion needs, consider using the [`<Thumbnail>`](https://www.remotion.dev/docs/player/thumbnail) component to add the necessary React contexts that Remotion needs and to show the component at a specific frame.++Add the [`noSuspense`](/docs/player/thumbnail#nosuspense) prop (available from Remotion v4.0.271) to prevent the component to be wrapped in React `<Suspense>`, which might delay the rendering of the markup.++## Example using Happy DOM and Bun++While not the most sophisticated, this is a very fast and lightweight way to test Remotion components.++```tsx title="src/test/example.test.ts"+import {Thumbnail} from '@remotion/player';+import {expect, test} from 'bun:test';++import {renderToString} from 'react-dom/server';+import {useCurrentFrame} from 'remotion';++const Comp: React.FC<{}> = () => {+ const frame = useCurrentFrame();+ const data = `We are on frame ${frame}`;+ return <div>{data}</div>;+};++test('should work', () => {+ const readStream = renderToString(<Thumbnail component={Comp} compositionHeight={1000} compositionWidth={1000} durationInFrames={1000} fps={30} frameToDisplay={10} noSuspense />);++ expect(readStream).toContain('<div>We are on frame 10</div>');+});+```++```toml title="bunfig.tom"+[test]+preload = "./happydom.ts"+```++```ts title="happydom.ts"+import {GlobalRegistrator} from '@happy-dom/global-registrator';++GlobalRegistrator.register();+```++Run the test using++```sh+bun test src/test/example.test.ts+```diff --git a/packages/docs/package.json b/packages/docs/package.jsonindex 17bf71c5dc..2a69338b64 100644--- a/packages/docs/package.json+++ b/packages/docs/package.json@@ -102,6 +102,7 @@"@types/hls.js": "^0.13.3","@types/unist": "^2.0.0","@types/node": "20.12.14",+ "@types/bun": "1.2.0","lodash.sortby": "^4.7.0","url-loader": "^4.1.1","openai": "4.67.1"diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 899f0c53ab..972d33055c 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -990,6 +990,7 @@ module.exports = {'javascript','third-party','miscellaneous/typescript-aliases',+ 'testing','figma','spline','after-effects',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex f787b89731..3975a5fb56 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -4777,6 +4777,15 @@ export const articles = [noAi: false,slug: 'terminology/studio',},+ {+ id: 'testing',+ title: 'Testing Remotion components',+ relativePath: 'docs/testing.mdx',+ compId: 'articles-docs-testing',+ crumb: 'Tooling',+ noAi: false,+ slug: 'testing',+ },{id: 'the-fundamentals',title: 'The fundamentals',diff --git a/packages/docs/static/generated/articles-docs-testing.png b/packages/docs/static/generated/articles-docs-testing.pngnew file mode 100644index 0000000000..ec591da1c1Binary files /dev/null and b/packages/docs/static/generated/articles-docs-testing.png differdiff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsxindex 5a0ff13280..8089610b9e 100644--- a/packages/player/src/Player.tsx+++ b/packages/player/src/Player.tsx@@ -91,6 +91,7 @@ export type PlayerProps<readonly browserMediaControlsBehavior?: BrowserMediaControlsBehavior;readonly overrideInternalClassName?: string;readonly logLevel?: LogLevel;+ readonly noSuspense?: boolean;readonly acknowledgeRemotionLicense?: boolean;} & CompProps<Props> &PropsIfHasProps<Schema, Props>;@@ -156,6 +157,7 @@ const PlayerFn = <browserMediaControlsBehavior: passedBrowserMediaControlsBehavior,overrideInternalClassName,logLevel = 'info',+ noSuspense,acknowledgeRemotionLicense,...componentProps}: PlayerProps<Schema, Props>,@@ -202,6 +204,7 @@ const PlayerFn = <const component = Internals.useLazyComponent({compProps: componentProps,componentName: 'Player',+ noSuspense: Boolean(noSuspense),}) as LazyExoticComponent<ComponentType<unknown>>;validateInitialFrame({initialFrame, durationInFrames});@@ -449,6 +452,7 @@ const PlayerFn = <overflowVisible={overflowVisible}browserMediaControlsBehavior={browserMediaControlsBehavior}overrideInternalClassName={overrideInternalClassName ?? undefined}+ noSuspense={Boolean(noSuspense)}/></PlayerEmitterProvider></Internals.Timeline.SetTimelineContext.Provider>diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsxindex fc4455d4c0..5ae572c597 100644--- a/packages/player/src/PlayerUI.tsx+++ b/packages/player/src/PlayerUI.tsx@@ -91,6 +91,7 @@ const PlayerUI: React.ForwardRefRenderFunction<readonly overflowVisible: boolean;readonly browserMediaControlsBehavior: BrowserMediaControlsBehavior;readonly overrideInternalClassName: string | undefined;+ readonly noSuspense: boolean;}> = ({@@ -129,6 +130,7 @@ const PlayerUI: React.ForwardRefRenderFunction<overflowVisible,browserMediaControlsBehavior,overrideInternalClassName,+ noSuspense,},ref,) => {@@ -705,7 +707,7 @@ const PlayerUI: React.ForwardRefRenderFunction<) : null}</>);- if (IS_NODE && !doesReactVersionSupportSuspense) {+ if (noSuspense || (IS_NODE && !doesReactVersionSupportSuspense)) {return (<div ref={container} style={outerStyle} className={className}>{content}diff --git a/packages/player/src/Thumbnail.tsx b/packages/player/src/Thumbnail.tsxindex b0f047c46b..72f33a3f35 100644--- a/packages/player/src/Thumbnail.tsx+++ b/packages/player/src/Thumbnail.tsx@@ -40,6 +40,7 @@ export type ThumbnailProps<readonly className?: string;readonly overrideInternalClassName?: string;readonly logLevel?: LogLevel;+ readonly noSuspense?: boolean;};export type ThumbnailPropsWithoutZod<Props extends Record<string, unknown>> =@@ -63,6 +64,7 @@ const ThumbnailFn = <overflowVisible = false,overrideInternalClassName,logLevel = 'info',+ noSuspense,...componentProps}: ThumbnailProps<Schema, Props>,ref: MutableRefObject<ThumbnailMethods>,@@ -102,6 +104,7 @@ const ThumbnailFn = <const Component = Internals.useLazyComponent({compProps: componentProps,componentName: 'Thumbnail',+ noSuspense: Boolean(noSuspense),}) as LazyExoticComponent<ComponentType<unknown>>;const [emitter] = useState(() => new ThumbnailEmitter());@@ -133,6 +136,7 @@ const ThumbnailFn = <style={style}overflowVisible={overflowVisible}overrideInternalClassName={overrideInternalClassName}+ noSuspense={Boolean(noSuspense)}/></ThumbnailEmitterContext.Provider></SharedPlayerContexts>diff --git a/packages/player/src/ThumbnailUI.tsx b/packages/player/src/ThumbnailUI.tsxindex b02d9fe7a1..a44c06ec45 100644--- a/packages/player/src/ThumbnailUI.tsx+++ b/packages/player/src/ThumbnailUI.tsx@@ -42,6 +42,7 @@ const ThumbnailUI: React.ForwardRefRenderFunction<readonly className: string | undefined;readonly overflowVisible: boolean;readonly overrideInternalClassName: string | undefined;+ readonly noSuspense: boolean;}> = ({@@ -51,6 +52,7 @@ const ThumbnailUI: React.ForwardRefRenderFunction<renderLoading,className,overflowVisible,+ noSuspense,overrideInternalClassName,},ref,@@ -164,7 +166,7 @@ const ThumbnailUI: React.ForwardRefRenderFunction<</div>);- if (IS_NODE && !doesReactVersionSupportSuspense) {+ if (noSuspense || (IS_NODE && !doesReactVersionSupportSuspense)) {return (<div ref={container} style={outerStyle} className={className}>{content}diff --git a/packages/transitions/package.json b/packages/transitions/package.jsonindex 8374c11aec..47be827e20 100644--- a/packages/transitions/package.json+++ b/packages/transitions/package.json@@ -32,6 +32,7 @@"react": "19.0.0","react-dom": "19.0.0","@remotion/test-utils": "workspace:*",+ "@remotion/player": "workspace:*","@remotion/eslint-config-internal": "workspace:*","eslint": "9.19.0"},diff --git a/packages/transitions/src/test/sample-test.test.tsx b/packages/transitions/src/test/sample-test.test.tsxnew file mode 100644index 0000000000..5381702c49--- /dev/null+++ b/packages/transitions/src/test/sample-test.test.tsx@@ -0,0 +1,27 @@+import {Thumbnail} from '@remotion/player';+import {expect, test} from 'bun:test';+import type React from 'react';+import {renderToString} from 'react-dom/server';+import {useCurrentFrame} from 'remotion';++const Comp: React.FC<{}> = () => {+ const frame = useCurrentFrame();+ const data = `We are on frame ${frame}`;+ return <div>{data}</div>;+};++test('should work', () => {+ const readStream = renderToString(+ <Thumbnail+ component={Comp}+ compositionHeight={1000}+ compositionWidth={1000}+ durationInFrames={1000}+ fps={30}+ frameToDisplay={10}+ noSuspense+ />,+ );++ expect(readStream).toContain('<div>We are on frame 10</div>');+});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a591361f8f..dc59ef08cd 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -780,6 +780,9 @@ importers:'@tsconfig/docusaurus':specifier: ^1.0.5version: 1.0.5+ '@types/bun':+ specifier: 1.2.0+ version: 1.2.0'@types/color-namer':specifier: ^1.3.0version: 1.3.0@@ -3326,6 +3329,9 @@ importers:'@remotion/eslint-config-internal':specifier: workspace:*version: link:../eslint-config-internal+ '@remotion/player':+ specifier: workspace:*+ version: link:../player'@remotion/test-utils':specifier: workspace:*version: link:../test-utils