From 2309f4bf60d512e57241388189eb1a5ec69cbd26 Mon Sep 17 00:00:00 2001 From: Jenae Janzen <101715009+jenae-janzen@users.noreply.github.com> Date: Mon, 27 Jun 2022 16:27:35 -0400 Subject: [PATCH] fix: add error message if unable to find cron expression for scheduled function (#1123) * fix: add error message if cron expression not found or invalid in isc of scheduled funcs --- src/runtimes/node/in_source_config/index.ts | 21 +++++++++++++++++-- .../cron_esm_additional_schedule_import.js | 9 ++++++++ .../cron_cjs_invalid_cron_expression.js | 5 +++++ .../cron_cjs_schedule_not_called.js | 4 ++++ .../cron_esm_schedule_not_called.js | 5 +++++ tests/main.js | 20 ++++++++++++++++-- 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/in-source-config/functions/cron_esm_additional_schedule_import.js create mode 100644 tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_invalid_cron_expression.js create mode 100644 tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_schedule_not_called.js create mode 100644 tests/fixtures/in-source-config/functions_missing_cron_expression/cron_esm_schedule_not_called.js diff --git a/src/runtimes/node/in_source_config/index.ts b/src/runtimes/node/in_source_config/index.ts index 059985a4c..4cd361c31 100644 --- a/src/runtimes/node/in_source_config/index.ts +++ b/src/runtimes/node/in_source_config/index.ts @@ -23,6 +23,10 @@ export const findISCDeclarationsInPath = async (sourcePath: string): Promise getImports(node, IN_SOURCE_CONFIG_MODULE)) + + const scheduledFuncsExpected = imports.filter(({ imported }) => imported === 'schedule').length + let scheduledFuncsFound = 0 + const getAllBindings = createBindingsMethod(ast.body) const mainExports = getMainExport(ast.body, getAllBindings) const iscExports = mainExports @@ -34,9 +38,15 @@ export const findISCDeclarationsInPath = async (sourcePath: string): Promise ({ ...acc, ...obj }), {}) return mergedExports diff --git a/tests/fixtures/in-source-config/functions/cron_esm_additional_schedule_import.js b/tests/fixtures/in-source-config/functions/cron_esm_additional_schedule_import.js new file mode 100644 index 000000000..047dbdfb4 --- /dev/null +++ b/tests/fixtures/in-source-config/functions/cron_esm_additional_schedule_import.js @@ -0,0 +1,9 @@ +import { schedule as nfySchedule } from '@netlify/functions' +import { schedule } from '../node_modules/@netlify/functions/index.js' +// make sure cron expression is found/doesn't error if `schedule` is also imported from another source + +schedule() + +export const handler = nfySchedule('@daily', () => { + // function handler +}) diff --git a/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_invalid_cron_expression.js b/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_invalid_cron_expression.js new file mode 100644 index 000000000..d09ce10db --- /dev/null +++ b/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_invalid_cron_expression.js @@ -0,0 +1,5 @@ +const { schedule } = require('@netlify/functions') + +module.exports.handler = schedule(null, () => { + // function handler +}) diff --git a/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_schedule_not_called.js b/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_schedule_not_called.js new file mode 100644 index 000000000..4aa794477 --- /dev/null +++ b/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_cjs_schedule_not_called.js @@ -0,0 +1,4 @@ +const { schedule } = require('@netlify/functions') + +// Should throw an error that `schedule` is imported but cron expression not found +module.exports.handler = {} diff --git a/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_esm_schedule_not_called.js b/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_esm_schedule_not_called.js new file mode 100644 index 000000000..7c4d44cd6 --- /dev/null +++ b/tests/fixtures/in-source-config/functions_missing_cron_expression/cron_esm_schedule_not_called.js @@ -0,0 +1,5 @@ +// eslint-disable-next-line n/no-unsupported-features/es-syntax +import { schedule } from '@netlify/functions' + +// Should throw an error that schedule is imported but cron expression not found +export const handler = {} diff --git a/tests/main.js b/tests/main.js index d3960d941..fe604ad47 100644 --- a/tests/main.js +++ b/tests/main.js @@ -2633,7 +2633,7 @@ testMany( 'Finds in-source config declarations using the `schedule` helper', ['bundler_default', 'bundler_esbuild', 'bundler_nft'], async (options, t) => { - const FUNCTIONS_COUNT = 11 + const FUNCTIONS_COUNT = 12 const { files } = await zipFixture(t, join('in-source-config', 'functions'), { opts: options, length: FUNCTIONS_COUNT, @@ -2647,6 +2647,22 @@ testMany( }, ) +testMany( + 'Throws error when `schedule` helper is imported but cron expression not found', + ['bundler_default', 'bundler_esbuild', 'bundler_nft'], + async (options, t) => { + const rejected = (error) => { + t.true(error.message.startsWith('Warning: unable to find cron expression for scheduled function.')) + } + + const FUNCTIONS_COUNT = 3 + await zipFixture(t, join('in-source-config', 'functions_missing_cron_expression'), { + opts: options, + length: FUNCTIONS_COUNT, + }).catch(rejected) + }, +) + test('listFunctions surfaces schedule config property', async (t) => { const functions = await listFunctions(join(FIXTURES_DIR, 'many-functions'), { config: { @@ -2663,7 +2679,7 @@ test('listFunctions includes in-source config declarations', async (t) => { const functions = await listFunctions(join(FIXTURES_DIR, 'in-source-config', 'functions'), { parseISC: true, }) - const FUNCTIONS_COUNT = 11 + const FUNCTIONS_COUNT = 12 t.is(functions.length, FUNCTIONS_COUNT) functions.forEach((func) => { t.is(func.schedule, '@daily')