🎥 Make videos programmatically with React
betters tests
Jonny Burgercd9a985a9e9c
diff --git a/jest.config.js b/jest.config.jsindex c564d9415c..de8d4b4e76 100644--- a/jest.config.js+++ b/jest.config.js@@ -3,5 +3,5 @@ module.exports = {testEnvironment: "node",rootDir: "tests",testTimeout: 240000,- setupFiles: ["../../packages/lambda/src/test/setup.ts"],+ resetMocks: true,};diff --git a/package.json b/package.jsonindex 7a222cd859..0fcc0c6459 100644--- a/package.json+++ b/package.json@@ -4,6 +4,7 @@"license": "SEE LICENSE IN LICENSE.md","scripts": {"test": "cd packages/core && npm test && cd ../babel-loader && npm test && cd ../bundler && npm test && cd ../cli && npm test && cd ../eslint-plugin && npm test && cd ../gif && npm test && cd ../lambda && npm test && cd ../media-utils && npm test && cd ../player && npm test && cd ../renderer && npm test && cd ../three && npm test && cd ../.. && npx jest --runInBand --root-dir=tests/integration",+ "testlambda": "npx jest --root-dir=tests/lambda --setup-files=../../packages/lambda/src/test/setup.ts","build": "pnpm run build --recursive && lerna bootstrap","watch": "pnpm run watch --recursive --stream --parallel","postinstall": "lerna bootstrap",diff --git a/packages/lambda/src/admin/bundle-lambda.ts b/packages/lambda/src/admin/bundle-lambda.tsindex 491c57857d..7f6fc35393 100644--- a/packages/lambda/src/admin/bundle-lambda.ts+++ b/packages/lambda/src/admin/bundle-lambda.ts@@ -3,6 +3,7 @@ import path from 'path';import {FUNCTION_ZIP} from '../shared/function-zip-path';import esbuild = require('esbuild');import zl = require('zip-lib');+import {quit} from '../cli/helpers/quit';const bundleLambda = async () => {const outdir = path.join(__dirname, '..', `build-render`);@@ -35,5 +36,5 @@ bundleLambda()}).catch((err) => {console.log(err);- process.exit(1);+ quit(1);});diff --git a/packages/lambda/src/admin/make-layer-public.ts b/packages/lambda/src/admin/make-layer-public.tsindex 852f411311..075c6e6d3e 100644--- a/packages/lambda/src/admin/make-layer-public.ts+++ b/packages/lambda/src/admin/make-layer-public.ts@@ -4,6 +4,7 @@ import {} from '@aws-sdk/client-lambda';import {lambda} from 'aws-policies';import {AwsRegion} from '..';+import {quit} from '../cli/helpers/quit';import {AWS_REGIONS} from '../regions';import {getLambdaClient} from '../shared/aws-clients';import {CURRENT_VERSION} from '../shared/constants';@@ -95,5 +96,5 @@ makeLayerPublic()}).catch((err) => {console.log(err);- process.exit(1);+ quit(1);});diff --git a/packages/lambda/src/api/deploy-function.ts b/packages/lambda/src/api/deploy-function.tsindex c8cf1868e3..aba7990845 100644--- a/packages/lambda/src/api/deploy-function.ts+++ b/packages/lambda/src/api/deploy-function.ts@@ -1,5 +1,6 @@+import {getFunctions} from '..';import {AwsRegion} from '../pricing/aws-regions';-import {RENDER_FN_PREFIX} from '../shared/constants';+import {CURRENT_VERSION, RENDER_FN_PREFIX} from '../shared/constants';import {FUNCTION_ZIP} from '../shared/function-zip-path';import {getAccountId} from '../shared/get-account-id';import {randomHash} from '../shared/random-hash';@@ -22,7 +23,9 @@ export const deployFunction = async (options: {region: AwsRegion;timeoutInSeconds: number;memorySizeInMb: number;-}) => {+}): Promise<{+ functionName: string;+}> => {validateMemorySize(options.memorySizeInMb);validateTimeout(options.timeoutInSeconds);validateAwsRegion(options.region);@@ -30,6 +33,19 @@ export const deployFunction = async (options: {const fnNameRender = RENDER_FN_PREFIX + randomHash();const accountId = await getAccountId({region: options.region});+ const fns = await getFunctions({+ compatibleOnly: true,+ region: options.region,+ });++ const alreadyDeployed = fns.find((f) => f.version === CURRENT_VERSION);++ if (alreadyDeployed) {+ throw new Error(+ `A function with version ${CURRENT_VERSION} is already deployed in region ${options.region}, it is called ${alreadyDeployed.functionName}`+ );+ }+const created = await createFunction({createCloudWatchLogGroup: options.createCloudWatchLogGroup,region: options.region,diff --git a/packages/lambda/src/cli/commands/functions/deploy.ts b/packages/lambda/src/cli/commands/functions/deploy.tsindex 44267a967c..f9a2cf6f86 100644--- a/packages/lambda/src/cli/commands/functions/deploy.ts+++ b/packages/lambda/src/cli/commands/functions/deploy.ts@@ -14,7 +14,6 @@ import {getAwsRegion} from '../../get-aws-region';export const FUNCTIONS_DEPLOY_SUBCOMMAND = 'deploy';export const functionsDeploySubcommand = async () => {- // TODO: Should only allow one lambda to be deployedconst region = getAwsRegion();const timeoutInSeconds = parsedLambdaCli.timeout ?? DEFAULT_TIMEOUT;const memorySizeInMb = parsedLambdaCli.memory ?? DEFAULT_MEMORY_SIZE;diff --git a/packages/lambda/src/cli/commands/functions/index.ts b/packages/lambda/src/cli/commands/functions/index.tsindex 9184949ca0..ba17ecb8bc 100644--- a/packages/lambda/src/cli/commands/functions/index.ts+++ b/packages/lambda/src/cli/commands/functions/index.ts@@ -1,6 +1,7 @@import {CliInternals} from '@remotion/cli';import {Log} from '@remotion/cli/dist/log';import {BINARY_NAME} from '../../../shared/constants';+import {quit} from '../../helpers/quit';import {functionsDeploySubcommand, FUNCTIONS_DEPLOY_SUBCOMMAND} from './deploy';import {functionsLsCommand, FUNCTIONS_LS_SUBCOMMAND} from './ls';import {functionsRmCommand, FUNCTIONS_RM_SUBCOMMAND} from './rm';@@ -42,7 +43,7 @@ export const functionsCommand = (args: string[]) => {if (args[0]) {Log.error(`Subcommand ${args[0]} not found.`);printFunctionsHelp();- process.exit(1);+ quit(1);}printFunctionsHelp();diff --git a/packages/lambda/src/cli/commands/functions/rm.ts b/packages/lambda/src/cli/commands/functions/rm.tsindex 39d7a0d99e..a2079c4fd6 100644--- a/packages/lambda/src/cli/commands/functions/rm.ts+++ b/packages/lambda/src/cli/commands/functions/rm.ts@@ -4,6 +4,7 @@ import {getFunctionInfo} from '../../../api/get-function-info';import {BINARY_NAME} from '../../../shared/constants';import {getAwsRegion} from '../../get-aws-region';import {confirmCli} from '../../helpers/confirm';+import {quit} from '../../helpers/quit';import {Log} from '../../log';import {FUNCTIONS_COMMAND} from './index';import {FUNCTIONS_LS_SUBCOMMAND} from './ls';@@ -21,7 +22,7 @@ export const functionsRmCommand = async (args: string[]) => {Log.info(`You can run \`${BINARY_NAME} ${FUNCTIONS_COMMAND} ${FUNCTIONS_LS_SUBCOMMAND}\` to see a list of deployed Lambda functions.`);- process.exit(1);+ quit(1);}for (const functionName of args) {diff --git a/packages/lambda/src/cli/commands/policies/policies.ts b/packages/lambda/src/cli/commands/policies/policies.tsindex ee8fa48d9b..d49e4ee4d9 100644--- a/packages/lambda/src/cli/commands/policies/policies.ts+++ b/packages/lambda/src/cli/commands/policies/policies.ts@@ -1,5 +1,6 @@import {CliInternals} from '@remotion/cli';import {BINARY_NAME} from '../../../shared/constants';+import {quit} from '../../helpers/quit';import {Log} from '../../log';import {roleSubcommand, ROLE_SUBCOMMAND} from './role';import {userSubcommand, USER_SUBCOMMAND} from './user';@@ -50,7 +51,7 @@ export const policiesCommand = (args: string[]) => {if (args[0]) {Log.error(`Subcommand ${args[0]} not found.`);printPoliciesHelp();- process.exit(1);+ quit(1);}printPoliciesHelp();diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.tsindex 556bb1f713..eaa5bc0e2e 100644--- a/packages/lambda/src/cli/commands/render/render.ts+++ b/packages/lambda/src/cli/commands/render/render.ts@@ -13,6 +13,7 @@ import {getAwsRegion} from '../../get-aws-region';import {findFunctionName} from '../../helpers/find-function-name';import {formatBytes} from '../../helpers/format-bytes';import {getCloudwatchStreamUrl} from '../../helpers/get-cloudwatch-stream-url';+import {quit} from '../../helpers/quit';import {Log} from '../../log';import {makeMultiProgressFromStatus, makeProgressString} from './progress';@@ -27,7 +28,7 @@ export const renderCommand = async (args: string[]) => {);Log.info();Log.info(`${BINARY_NAME} ${RENDER_COMMAND} <serve-url> <composition-id>`);- process.exit(1);+ quit(1);}const composition = args[1];@@ -37,7 +38,7 @@ export const renderCommand = async (args: string[]) => {Log.info();// TODO: Rename serveURLLog.info(`${BINARY_NAME} ${RENDER_COMMAND} <serve-url> <composition-id>`);- process.exit(1);+ quit(1);}const outName = args[2] ?? null;@@ -172,7 +173,7 @@ export const renderCommand = async (args: string[]) => {`${newStatus.renderMetadata?.estimatedTotalLambdaInvokations} λ's used, Estimated cost $${newStatus.costs.displayCost}`);- process.exit(0);+ quit(0);}if (newStatus.fatalErrorEncountered) {@@ -189,7 +190,7 @@ export const renderCommand = async (args: string[]) => {}Log.error('Fatal error encountered. Exiting.');- process.exit(1);+ quit(1);}}};diff --git a/packages/lambda/src/cli/commands/sites/create.ts b/packages/lambda/src/cli/commands/sites/create.tsindex ca87aad5e7..37b2ba9dff 100644--- a/packages/lambda/src/cli/commands/sites/create.ts+++ b/packages/lambda/src/cli/commands/sites/create.ts@@ -14,6 +14,7 @@ import {makeBundleProgress,makeDeployProgressBar,} from '../../helpers/progress-bar';+import {quit} from '../../helpers/quit';import {Log} from '../../log';export const SITES_CREATE_SUBCOMMAND = 'create';@@ -27,7 +28,7 @@ export const sitesCreateSubcommand = async (args: string[]) => {);Log.info();Log.info(`${BINARY_NAME} deploy <entry-file.ts>`);- process.exit(1);+ quit(1);}const absoluteFile = path.join(process.cwd(), fileName);@@ -35,14 +36,14 @@ export const sitesCreateSubcommand = async (args: string[]) => {Log.error(`No file exists at ${absoluteFile}. Make sure the path exists and try again.`);- process.exit(1);+ quit(1);}if (lstatSync(absoluteFile).isDirectory()) {Log.error(`You passed a path ${absoluteFile} but it is a directory. Pass a file instead.`);- process.exit(1);+ quit(1);}const progressBar = CliInternals.createOverwriteableCliOutput();diff --git a/packages/lambda/src/cli/commands/sites/index.ts b/packages/lambda/src/cli/commands/sites/index.tsindex 6d0ee6055d..6baaa3da09 100644--- a/packages/lambda/src/cli/commands/sites/index.ts+++ b/packages/lambda/src/cli/commands/sites/index.ts@@ -1,6 +1,7 @@import {CliInternals} from '@remotion/cli';import {Log} from '@remotion/cli/dist/log';import {BINARY_NAME} from '../../../shared/constants';+import {quit} from '../../helpers/quit';import {sitesCreateSubcommand, SITES_CREATE_SUBCOMMAND} from './create';import {sitesLsSubcommand, SITES_LS_SUBCOMMAND} from './ls';import {sitesRmSubcommand, SITES_RM_COMMAND} from './rm';@@ -42,7 +43,7 @@ export const sitesCommand = (args: string[]) => {if (args[0]) {Log.error(`Subcommand ${args[0]} not found.`);printSitesHelp();- process.exit(1);+ quit(1);}printSitesHelp();diff --git a/packages/lambda/src/cli/commands/sites/rm.ts b/packages/lambda/src/cli/commands/sites/rm.tsindex b2a5251495..c7f988dcfd 100644--- a/packages/lambda/src/cli/commands/sites/rm.ts+++ b/packages/lambda/src/cli/commands/sites/rm.ts@@ -5,6 +5,7 @@ import {getRemotionS3Buckets} from '../../../api/get-buckets';import {getAwsRegion} from '../../get-aws-region';import {confirmCli} from '../../helpers/confirm';import {formatBytes} from '../../helpers/format-bytes';+import {quit} from '../../helpers/quit';import {Log} from '../../log';export const SITES_RM_COMMAND = 'rm';@@ -13,7 +14,7 @@ export const sitesRmSubcommand = async (args: string[]) => {Log.error('No site name was passed. Run the command again and pass another argument <site-name>.');- process.exit(1);+ quit(1);}const region = getAwsRegion();@@ -28,14 +29,14 @@ export const sitesRmSubcommand = async (args: string[]) => {Log.error('You have more than one Remotion Lambda bucket. You should only have one - delete all but one before continuing.');- process.exit(1);+ quit(1);}if (remotionBuckets.length === 0) {Log.error(`You don't have a Remotion Lambda bucket in the ${region} region. Therefore nothing was deleted.`);- process.exit(1);+ quit(1);}const site = deployedSites.sites.find((s) => s.id === siteName.trim());@@ -45,7 +46,7 @@ export const sitesRmSubcommand = async (args: string[]) => {remotionBuckets[0].name}.`);- process.exit(1);+ return quit(1);}await confirmCli({diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.tsindex 55141dfb4b..f732518c3b 100644--- a/packages/lambda/src/cli/commands/still.ts+++ b/packages/lambda/src/cli/commands/still.ts@@ -5,6 +5,7 @@ import {BINARY_NAME} from '../../shared/constants';import {parsedLambdaCli} from '../args';import {getAwsRegion} from '../get-aws-region';import {findFunctionName} from '../helpers/find-function-name';+import {quit} from '../helpers/quit';import {Log} from '../log';export const STILL_COMMAND = 'still';@@ -18,7 +19,7 @@ export const stillCommand = async (args: string[]) => {);Log.info();Log.info(`${BINARY_NAME} ${STILL_COMMAND} <serve-url> <composition-id>`);- process.exit(1);+ quit(1);}const composition = args[1];@@ -28,7 +29,7 @@ export const stillCommand = async (args: string[]) => {Log.info();// TODO: Rename serveURLLog.info(`${BINARY_NAME} ${STILL_COMMAND} <serve-url> <composition-id>`);- process.exit(1);+ quit(1);}const cliOptions = await CliInternals.getCliOptions({diff --git a/packages/lambda/src/cli/helpers/__mocks__/quit.ts b/packages/lambda/src/cli/helpers/__mocks__/quit.tsnew file mode 100644index 0000000000..ef01c69959--- /dev/null+++ b/packages/lambda/src/cli/helpers/__mocks__/quit.ts@@ -0,0 +1,3 @@+export const quit = (exitCode: number) => {+ throw new Error(`Exited process with code ${exitCode}`);+};diff --git a/packages/lambda/src/cli/helpers/confirm.ts b/packages/lambda/src/cli/helpers/confirm.tsindex 13803725a7..e9822240be 100644--- a/packages/lambda/src/cli/helpers/confirm.ts+++ b/packages/lambda/src/cli/helpers/confirm.ts@@ -1,5 +1,6 @@import prompt from 'prompt';import {forceFlagProvided} from '../args';+import {quit} from './quit';export const confirmCli = async ({delMessage,@@ -28,7 +29,7 @@ export const confirmCli = async ({}if ((result[delMessage] as string).trim() === 'n') {- process.exit(1);+ quit(1);}}};diff --git a/packages/lambda/src/cli/helpers/find-function-name.ts b/packages/lambda/src/cli/helpers/find-function-name.tsindex 850a37979d..43757048e5 100644--- a/packages/lambda/src/cli/helpers/find-function-name.ts+++ b/packages/lambda/src/cli/helpers/find-function-name.ts@@ -6,6 +6,7 @@ import {FUNCTIONS_LS_SUBCOMMAND} from '../commands/functions/ls';import {FUNCTIONS_RM_SUBCOMMAND} from '../commands/functions/rm';import {getAwsRegion} from '../get-aws-region';import {Log} from '../log';+import {quit} from './quit';export const findFunctionName = async () => {const remotionLambdas = await getFunctions({@@ -31,7 +32,7 @@ export const findFunctionName = async () => {` npx ${BINARY_NAME} ${FUNCTIONS_COMMAND} ${FUNCTIONS_DEPLOY_SUBCOMMAND}`);Log.info(`to deploy a new lambda function.`);- process.exit(1);+ quit(1);}if (lambdasWithMatchingVersion.length > 1) {@@ -43,7 +44,7 @@ export const findFunctionName = async () => {` npx ${BINARY_NAME} ${FUNCTIONS_COMMAND} ${FUNCTIONS_RM_SUBCOMMAND} $(npx ${BINARY_NAME} ${FUNCTIONS_COMMAND} ${FUNCTIONS_LS_SUBCOMMAND} -q) -y`);Log.info('to delete all lambda functions.');- process.exit(1);+ quit(1);}const {functionName} = lambdasWithMatchingVersion[0];diff --git a/packages/lambda/src/cli/helpers/quit.ts b/packages/lambda/src/cli/helpers/quit.tsnew file mode 100644index 0000000000..3d142a276e--- /dev/null+++ b/packages/lambda/src/cli/helpers/quit.ts@@ -0,0 +1,3 @@+export const quit = (exitCode: number): never => {+ process.exit(exitCode);+};diff --git a/packages/lambda/src/cli/index.ts b/packages/lambda/src/cli/index.tsindex bf367383c8..8d67b8ac99 100644--- a/packages/lambda/src/cli/index.ts+++ b/packages/lambda/src/cli/index.ts@@ -9,6 +9,7 @@ import {renderCommand, RENDER_COMMAND} from './commands/render/render';import {sitesCommand, SITES_COMMAND} from './commands/sites';import {stillCommand, STILL_COMMAND} from './commands/still';import {printHelp} from './help';+import {quit} from './helpers/quit';import {Log} from './log';const requiresCredentials = (args: string[]) => {@@ -28,7 +29,7 @@ const requiresCredentials = (args: string[]) => {const matchCommand = async (args: string[]) => {if (parsedLambdaCli.help || args.length === 0) {printHelp();- process.exit(0);+ quit(0);}if (requiresCredentials(args)) {@@ -59,14 +60,14 @@ const matchCommand = async (args: string[]) => {Log.info('The command has been renamed.');Log.info('Before: remotion-lambda upload <entry-point>');Log.info('After: remotion lambda sites create <entry-point>');- process.exit(1);+ quit(1);}if (args[0] === 'deploy') {Log.info('The command has been renamed.');Log.info('Before: remotion-lambda deploy');Log.info('After: remotion lambda functions deploy');- process.exit(1);+ quit(1);}if (args[0] === 'ls') {@@ -86,7 +87,7 @@ const matchCommand = async (args: string[]) => {Log.error(`Command ${args[0]} not found.`);printHelp();- process.exit(1);+ quit(1);};export const executeCommand = async (args: string[]) => {@@ -100,7 +101,7 @@ export const executeCommand = async (args: string[]) => {}Log.error(error.stack);- process.exit(1);+ quit(1);}};diff --git a/packages/lambda/src/test/setup.ts b/packages/lambda/src/test/setup.tsindex d9f0f10907..69674720d1 100644--- a/packages/lambda/src/test/setup.ts+++ b/packages/lambda/src/test/setup.ts@@ -1,6 +1,9 @@jest.mock('../api/get-buckets', () =>jest.requireActual('../api/__mocks__/get-buckets'));+jest.mock('../cli/helpers/quit', () =>+ jest.requireActual('../cli/helpers/__mocks__/quit')+);jest.mock('../functions/helpers/io', () =>jest.requireActual('../functions/helpers/__mocks__/io'));diff --git a/tests/lambda/cli.test.ts b/tests/lambda/cli.test.tsindex 57213c01f0..10adb66613 100644--- a/tests/lambda/cli.test.ts+++ b/tests/lambda/cli.test.ts@@ -1,4 +1,5 @@-import { LambdaInternals, getFunctions } from "../../packages/lambda/src/index";+import { cleanFnStore } from "../../packages/lambda/src/api/mock-functions";+import { LambdaInternals } from "../../packages/lambda/src/index";const getConsoleOutput = () => {// @ts-expect-error@@ -12,6 +13,7 @@ test("Deploy function", async () => {});test("Deploy function and list it", async () => {+ cleanFnStore();await LambdaInternals.executeCommand(["functions", "deploy"]);process.stdout.write = jest.fn();await LambdaInternals.executeCommand(["functions", "ls"]);@@ -21,3 +23,15 @@ test("Deploy function and list it", async () => {/remotion-render-abcdef\s+(.*)\s+1024\s+120/g);});++test("Deploy function and it already exists should fail", async () => {+ cleanFnStore();+ await LambdaInternals.executeCommand(["functions", "deploy"]);+ await expect(() =>+ LambdaInternals.executeCommand(["functions", "deploy"])+ ).rejects.toThrow(/Exited process with code 1/);++ expect(getConsoleOutput()).toMatch(+ /A function with version (.*) is already deployed in region us-east-1, it is called remotion-render-abcdef/+ );+});