Skip to content

🎥 Make videos programmatically with React

easier tests for studio-server

JonnyBurgerfd2e06c7a2df
diff --git a/packages/studio-server/src/test/compute-sequence-props-status.test.ts b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
index 00b1a8fdcc..ce6846ce00 100644
--- a/packages/studio-server/src/test/compute-sequence-props-status.test.ts
+++ b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
@@ -19,7 +19,7 @@ const getNodePath = (filePath: string, line: number) => {
};
test('canUpdateSequenceProps should flag computed props', () => {
- const filePath = path.join(__dirname, 'snapshots', 'light-leak-computed.txt');
+ const filePath = path.join(__dirname, 'snapshots', 'light-leak-computed.tsx');
const result = computeSequencePropsStatus({
fileName: filePath,
nodePath: getNodePath(filePath, 8),
@@ -45,7 +45,7 @@ test('canUpdateSequenceProps should flag computed props', () => {
});
test('computeSequencePropsStatus should detect static nested props', () => {
- const filePath = path.join(__dirname, 'snapshots', 'nested-props.txt');
+ const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
fileName: filePath,
nodePath: getNodePath(filePath, 7),
@@ -67,7 +67,7 @@ test('computeSequencePropsStatus should detect static nested props', () => {
});
test('computeSequencePropsStatus should flag computed nested props', () => {
- const filePath = path.join(__dirname, 'snapshots', 'nested-props.txt');
+ const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
fileName: filePath,
nodePath: getNodePath(filePath, 8),
@@ -91,7 +91,7 @@ test('computeSequencePropsStatus should flag computed nested props', () => {
});
test('computeSequencePropsStatus should flag computed when parent is not an object', () => {
- const filePath = path.join(__dirname, 'snapshots', 'nested-props.txt');
+ const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
fileName: filePath,
nodePath: getNodePath(filePath, 9),
@@ -110,7 +110,7 @@ test('computeSequencePropsStatus should flag computed when parent is not an obje
});
test('computeSequencePropsStatus should report unset nested props as undefined', () => {
- const filePath = path.join(__dirname, 'snapshots', 'nested-props.txt');
+ const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
fileName: filePath,
nodePath: getNodePath(filePath, 7),
@@ -128,7 +128,7 @@ test('computeSequencePropsStatus should report unset nested props as undefined',
});
test('computeSequencePropsStatus should report unset when parent attribute missing', () => {
- const filePath = path.join(__dirname, 'snapshots', 'nested-props.txt');
+ const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
fileName: filePath,
nodePath: getNodePath(filePath, 10),
diff --git a/packages/studio-server/src/test/discriminated-union-update.test.ts b/packages/studio-server/src/test/discriminated-union-update.test.ts
index 05b1fb4cb4..29a915f3e6 100644
--- a/packages/studio-server/src/test/discriminated-union-update.test.ts
+++ b/packages/studio-server/src/test/discriminated-union-update.test.ts
@@ -7,6 +7,7 @@ import {Internals} from 'remotion';
import {parseAst} from '../codemods/parse-ast';
import {updateSequencePropsAst} from '../codemods/update-sequence-props/update-sequence-props';
import {lineColumnToNodePath} from '../preview-server/routes/can-update-sequence-props';
+import {prettify} from './test-utils';
test('Should correctly separate discriminated union for layout', () => {
const schemaFields = getFieldsToShow({
@@ -41,7 +42,7 @@ test('Should expose absolute-fill variant fields when active', () => {
]);
});
-test('Should be able to update a discriminated union', () => {
+test('Should be able to update a discriminated union', async () => {
const file = readFileSync(
path.join(__dirname, 'snapshots', 'discriminated-union.tsx'),
'utf-8',
@@ -69,23 +70,11 @@ test('Should be able to update a discriminated union', () => {
path.join(__dirname, 'snapshots', 'discriminated-union-expected.tsx'),
'utf-8',
);
- const actualLines = update.serialized.split('\n');
- const expectedLines = expected.split('\n');
- const maxLines = Math.max(actualLines.length, expectedLines.length);
- for (let i = 0; i < maxLines; i++) {
- if (actualLines[i] !== expectedLines[i]) {
- // eslint-disable-next-line no-console
- console.log(update);
- // eslint-disable-next-line no-console
- console.log(actualLines[i], expectedLines[i]);
- throw new Error(
- `Line ${i + 1} differs ${actualLines[i]} ${expectedLines[i]}`,
- );
- }
- }
+
+ expect(await prettify(update.serialized)).toBe(await prettify(expected));
});
-test('Should remove variant-specific props when switching enum value', () => {
+test('Should remove variant-specific props when switching enum value', async () => {
const file = readFileSync(
path.join(__dirname, 'snapshots', 'discriminated-union-with-style.tsx'),
'utf-8',
@@ -117,23 +106,11 @@ test('Should remove variant-specific props when switching enum value', () => {
),
'utf-8',
);
- const actualLines = update.serialized.split('\n');
- const expectedLines = expected.split('\n');
- const maxLines = Math.max(actualLines.length, expectedLines.length);
- for (let i = 0; i < maxLines; i++) {
- if (actualLines[i] !== expectedLines[i]) {
- // eslint-disable-next-line no-console
- console.log(update);
- // eslint-disable-next-line no-console
- console.log(actualLines[i], expectedLines[i]);
- throw new Error(
- `Line ${i + 1} differs ${actualLines[i]} ${expectedLines[i]}`,
- );
- }
- }
+
+ expect(await prettify(update.serialized)).toBe(await prettify(expected));
});
-test('Should remove premountFor and styleWhile* when switching to layout="none"', () => {
+test('Should remove premountFor and styleWhile* when switching to layout="none"', async () => {
const file = readFileSync(
path.join(__dirname, 'snapshots', 'discriminated-union-with-premount.tsx'),
'utf-8',
@@ -165,18 +142,6 @@ test('Should remove premountFor and styleWhile* when switching to layout="none"'
),
'utf-8',
);
- const actualLines = update.serialized.split('\n');
- const expectedLines = expected.split('\n');
- const maxLines = Math.max(actualLines.length, expectedLines.length);
- for (let i = 0; i < maxLines; i++) {
- if (actualLines[i] !== expectedLines[i]) {
- // eslint-disable-next-line no-console
- console.log(update);
- // eslint-disable-next-line no-console
- console.log(actualLines[i], expectedLines[i]);
- throw new Error(
- `Line ${i + 1} differs ${actualLines[i]} ${expectedLines[i]}`,
- );
- }
- }
+
+ expect(await prettify(update.serialized)).toBe(await prettify(expected));
});
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx b/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx
index dddc119336..871cf0fe77 100644
--- a/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-expected.tsx
@@ -1,8 +1,3 @@
export const ExperimentalControlsShowcase: React.FC = () => {
- return (
- <Sequence
- layout={"none"}
- >hi
- </Sequence>
- );
+ return <Sequence layout={'none'}>hi</Sequence>;
};
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-with-premount-expected.tsx b/packages/studio-server/src/test/snapshots/discriminated-union-with-premount-expected.tsx
index 40927b1ab7..f50024acdc 100644
--- a/packages/studio-server/src/test/snapshots/discriminated-union-with-premount-expected.tsx
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-with-premount-expected.tsx
@@ -1,3 +1,3 @@
export const ExperimentalControlsShowcase: React.FC = () => {
- return (<Sequence layout={"none"}></Sequence>);
+ return <Sequence layout={'none'}></Sequence>;
};
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx b/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx
index dde0d4d867..871cf0fe77 100644
--- a/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-with-style-expected.tsx
@@ -1,6 +1,3 @@
export const ExperimentalControlsShowcase: React.FC = () => {
- return (
- <Sequence layout={"none"}>hi
- </Sequence>
- );
+ return <Sequence layout={'none'}>hi</Sequence>;
};
diff --git a/packages/studio-server/src/test/snapshots/fixed.txt b/packages/studio-server/src/test/snapshots/fixed.tsx
similarity index 100%
rename from packages/studio-server/src/test/snapshots/fixed.txt
rename to packages/studio-server/src/test/snapshots/fixed.tsx
diff --git a/packages/studio-server/src/test/snapshots/light-leak-computed.tsx b/packages/studio-server/src/test/snapshots/light-leak-computed.tsx
new file mode 100644
index 0000000000..aed9396b52
--- /dev/null
+++ b/packages/studio-server/src/test/snapshots/light-leak-computed.tsx
@@ -0,0 +1,11 @@
+import {LightLeak} from '@remotion/light-leaks';
+import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const LightLeakExample: React.FC = () => {
+ return (
+ <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <LightLeak durationInFrames={60} seed={1 + 2} hueShift={30} />
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/studio-server/src/test/snapshots/light-leak-computed.txt b/packages/studio-server/src/test/snapshots/light-leak-computed.txt
deleted file mode 100644
index d19ceb2fa2..0000000000
--- a/packages/studio-server/src/test/snapshots/light-leak-computed.txt
+++ /dev/null
@@ -1,11 +0,0 @@
-import {LightLeak} from "@remotion/light-leaks";
-import React from "react";
-import {AbsoluteFill} from "remotion";
-
-export const LightLeakExample: React.FC = () => {
- return (
- <AbsoluteFill style={{backgroundColor: "black"}}>
- <LightLeak durationInFrames={60} seed={1 + 2} hueShift={30} />
- </AbsoluteFill>
- );
-};
diff --git a/packages/studio-server/src/test/snapshots/nested-props.txt b/packages/studio-server/src/test/snapshots/nested-props.tsx
similarity index 78%
rename from packages/studio-server/src/test/snapshots/nested-props.txt
rename to packages/studio-server/src/test/snapshots/nested-props.tsx
index ff58d9842e..f352497863 100644
--- a/packages/studio-server/src/test/snapshots/nested-props.txt
+++ b/packages/studio-server/src/test/snapshots/nested-props.tsx
@@ -1,5 +1,5 @@
-import React from "react";
-import {AbsoluteFill} from "remotion";
+import React from 'react';
+import {AbsoluteFill} from 'remotion';
export const Example: React.FC = () => {
return (
diff --git a/packages/studio-server/src/test/snapshots/problematic.txt b/packages/studio-server/src/test/snapshots/problematic.tsx
similarity index 100%
rename from packages/studio-server/src/test/snapshots/problematic.txt
rename to packages/studio-server/src/test/snapshots/problematic.tsx
diff --git a/packages/studio-server/src/test/test-utils.ts b/packages/studio-server/src/test/test-utils.ts
index 5f25f6f9b4..4a42cfe716 100644
--- a/packages/studio-server/src/test/test-utils.ts
+++ b/packages/studio-server/src/test/test-utils.ts
@@ -1,8 +1,18 @@
import type {Expression} from '@babel/types';
import type {SequenceNodePath} from 'remotion';
+import {formatFileContent} from '../codemods/format-file-content';
import {parseAst} from '../codemods/parse-ast';
import {lineColumnToNodePath as _lineColumnToNodePath} from '../preview-server/routes/can-update-sequence-props';
+export const prettify = async (input: string): Promise<string> => {
+ const {output, formatted} = await formatFileContent({input});
+ if (!formatted) {
+ throw new Error('prettier could not format input');
+ }
+
+ return output;
+};
+
export const parseExpression = (code: string): Expression => {
const ast = parseAst(`a = ${code}`);
const stmt = ast.program.body[0];
diff --git a/packages/studio-server/src/test/update-default-props.test.ts b/packages/studio-server/src/test/update-default-props.test.ts
index 153838890b..9c63efdf63 100644
--- a/packages/studio-server/src/test/update-default-props.test.ts
+++ b/packages/studio-server/src/test/update-default-props.test.ts
@@ -5,6 +5,7 @@ import {
getCompositionDefaultPropsLine,
updateDefaultProps,
} from '../codemods/update-default-props';
+import {prettify} from './test-utils';
test('Should be able to update default props', async () => {
const file = readFileSync(
@@ -23,7 +24,7 @@ test('Should be able to update default props', async () => {
enumPaths: [],
});
- expect(output).toBe(expected);
+ expect(await prettify(output)).toBe(await prettify(expected));
});
test('getCompositionDefaultPropsLine returns the opening tag line (ast-types visitor must traverse)', () => {
@@ -42,11 +43,11 @@ test('getCompositionDefaultPropsLine returns the opening tag line (ast-types vis
test('Should be able to update default props', async () => {
const file = readFileSync(
- path.join(__dirname, 'snapshots', 'problematic.txt'),
+ path.join(__dirname, 'snapshots', 'problematic.tsx'),
'utf-8',
);
const expected = readFileSync(
- path.join(__dirname, 'snapshots', 'fixed.txt'),
+ path.join(__dirname, 'snapshots', 'fixed.tsx'),
'utf-8',
);
@@ -57,5 +58,5 @@ test('Should be able to update default props', async () => {
enumPaths: [],
});
- expect(output).toBe(expected);
+ expect(await prettify(output)).toBe(await prettify(expected));
});