From bf90d64f08e109abce11b219d3819b1d44ba29fe Mon Sep 17 00:00:00 2001 From: mrickard Date: Thu, 15 Jun 2023 16:54:37 -0400 Subject: [PATCH 1/4] chore: Added error-state non-happy-path tests to increase coverage Signed-off-by: mrickard --- nodejs/test/unit/errorStates.tap.js | 90 +++++++++++++++++++++++++++++ nodejs/test/unit/fixtures/errors.js | 3 + 2 files changed, 93 insertions(+) create mode 100644 nodejs/test/unit/errorStates.tap.js create mode 100644 nodejs/test/unit/fixtures/errors.js diff --git a/nodejs/test/unit/errorStates.tap.js b/nodejs/test/unit/errorStates.tap.js new file mode 100644 index 0000000..7e528d2 --- /dev/null +++ b/nodejs/test/unit/errorStates.tap.js @@ -0,0 +1,90 @@ +'use strict' + +const tap = require('tap') +const proxyquire = require('proxyquire').noCallThru().noPreserveCache() +const utils = require('@newrelic/test-utilities') +const path = require('node:path') + +tap.test('Edge cases', (t) => { + t.autoend() + let handler + let helper + let originalEnv + + t.beforeEach(() => { + originalEnv = {...process.env} + process.env.NEW_RELIC_USE_ESM = 'false' + + helper = utils.TestAgent.makeInstrumented() + + const newrelic = helper.getAgentApi() + + ;({ handler } = proxyquire('../../index', { + 'newrelic': newrelic + })) + }) + + t.afterEach(() => { + process.env = {...originalEnv} + helper.unload() + }) + + t.test('should delete serverless mode env var if defined', (t) => { + process.env.LAMBDA_TASK_ROOT = './' + process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED = 'true' + + // redundant, but needed for this test + const newrelic = helper.getAgentApi() + + ;({ handler } = proxyquire('../../index', { + 'newrelic': newrelic + })) + + t.notOk(process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED, + 'NEW_RELIC_SERVERLESS_MODE_ENABLED env var should have been deleted') + t.end() + }) + + t.test('should throw', async(t) => { + t.throws(() => { + return handler({ key: 'this is a test'}, { functionName: 'testFn'}) + }, 'No NEW_RELIC_LAMBDA_HANDLER environment variable set.', + 'when NEW_RELIC_LAMBDA_HANDLER is unset') + + process.env.NEW_RELIC_LAMBDA_HANDLER = 'test/unit/fixtures/cjs/handler' + t.throws(() => { + return handler({ key: 'this is a test'}, { functionName: 'testFn'}) + }, `Improperly formatted handler environment variable: test/unit/fixtures/cjs/handler`, + 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + + let handlerPath = 'test/unit/fixtures/' + let handlerFile = 'notFound' + let handlerMethod = 'noMethodFound' + let modulePath = path.resolve('./', handlerPath) + const extensions = ['.cjs', '.js'] + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws(() => { + return handler({ key: 'this is a test'}, { functionName: handlerMethod }) + }, `Unable to resolve module file at ${modulePath} with the following extensions: ${extensions.join(',')}`, + 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + + handlerFile = 'errors' + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws(() => { + return handler({ key: 'this is a test'}, { functionName: handlerMethod }) + }, `Handler '${handlerMethod}' missing on module '${handlerPath}'`, + 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + + handlerMethod = 'notAfunction' + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws(() => { + return handler({ key: 'this is a test'}, { functionName: handlerMethod }) + }, `Handler '${handlerMethod}' from 'test/unit/fixtures/errors' is not a function`, + 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + + t.end() + }) +}) diff --git a/nodejs/test/unit/fixtures/errors.js b/nodejs/test/unit/fixtures/errors.js new file mode 100644 index 0000000..7d40047 --- /dev/null +++ b/nodejs/test/unit/fixtures/errors.js @@ -0,0 +1,3 @@ +"use strict" + +exports.notAfunction = `This is a string.` From ebf8dd1fe23c604c36cc40a88993cf19e459b077 Mon Sep 17 00:00:00 2001 From: mrickard Date: Fri, 16 Jun 2023 14:08:49 -0400 Subject: [PATCH 2/4] chore: Increased coverage, including hitting handleRequireImportError Signed-off-by: mrickard --- nodejs/index.js | 2 +- nodejs/test/unit/errorStates.tap.js | 16 ++++++++++++---- nodejs/test/unit/fixtures/badRequire.js | 10 ++++++++++ nodejs/test/unit/fixtures/errors.js | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 nodejs/test/unit/fixtures/badRequire.js diff --git a/nodejs/index.js b/nodejs/index.js index fb19bbc..3629457 100644 --- a/nodejs/index.js +++ b/nodejs/index.js @@ -139,7 +139,7 @@ function patchCommonJSHandler() { return wrappedHandler.apply(this, args) } -module.exports = { +module.exports = { handler: process.env.NEW_RELIC_USE_ESM === 'true' ? patchESModuleHandler : patchCommonJSHandler, getHandlerPath } diff --git a/nodejs/test/unit/errorStates.tap.js b/nodejs/test/unit/errorStates.tap.js index 7e528d2..a1f5206 100644 --- a/nodejs/test/unit/errorStates.tap.js +++ b/nodejs/test/unit/errorStates.tap.js @@ -5,7 +5,7 @@ const proxyquire = require('proxyquire').noCallThru().noPreserveCache() const utils = require('@newrelic/test-utilities') const path = require('node:path') -tap.test('Edge cases', (t) => { +tap.test('Edge cases (CJS)', (t) => { t.autoend() let handler let helper @@ -67,7 +67,7 @@ tap.test('Edge cases', (t) => { t.throws(() => { return handler({ key: 'this is a test'}, { functionName: handlerMethod }) }, `Unable to resolve module file at ${modulePath} with the following extensions: ${extensions.join(',')}`, - 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + `when module file can't be found`) handlerFile = 'errors' process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` @@ -75,7 +75,7 @@ tap.test('Edge cases', (t) => { t.throws(() => { return handler({ key: 'this is a test'}, { functionName: handlerMethod }) }, `Handler '${handlerMethod}' missing on module '${handlerPath}'`, - 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + `when file is found, but handler function is not found`) handlerMethod = 'notAfunction' process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` @@ -83,7 +83,15 @@ tap.test('Edge cases', (t) => { t.throws(() => { return handler({ key: 'this is a test'}, { functionName: handlerMethod }) }, `Handler '${handlerMethod}' from 'test/unit/fixtures/errors' is not a function`, - 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') + `when NEW_RELIC_LAMBDA_HANDLER is not a function`) + + handlerFile = 'badRequire' + handlerMethod = 'handler' + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + t.throws(() => { + return handler({ key: 'this is a test'}, { functionName: handlerMethod }) + }, `Unable to import module '${handlerPath}${handlerFile}'`, + `when handler file can't load dependencies`) t.end() }) diff --git a/nodejs/test/unit/fixtures/badRequire.js b/nodejs/test/unit/fixtures/badRequire.js new file mode 100644 index 0000000..6da2dc0 --- /dev/null +++ b/nodejs/test/unit/fixtures/badRequire.js @@ -0,0 +1,10 @@ +'use strict' + +const notFoundDependency = require('path/not/found') + +exports.handler = (event) => { + return { + statusCode: 200, + body: JSON.stringify(event) + } +} diff --git a/nodejs/test/unit/fixtures/errors.js b/nodejs/test/unit/fixtures/errors.js index 7d40047..6f54c24 100644 --- a/nodejs/test/unit/fixtures/errors.js +++ b/nodejs/test/unit/fixtures/errors.js @@ -1,3 +1,3 @@ -"use strict" +'use strict' exports.notAfunction = `This is a string.` From d41a90a0241a5ec3f5226d13bcb5a55b89f78996 Mon Sep 17 00:00:00 2001 From: mrickard Date: Fri, 23 Jun 2023 15:26:09 -0400 Subject: [PATCH 3/4] chore: Fixed eslint complaints Signed-off-by: mrickard --- nodejs/test/unit/errorStates.tap.js | 6 +++--- nodejs/test/unit/fixtures/badRequire.js | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/nodejs/test/unit/errorStates.tap.js b/nodejs/test/unit/errorStates.tap.js index a1f5206..3d0965e 100644 --- a/nodejs/test/unit/errorStates.tap.js +++ b/nodejs/test/unit/errorStates.tap.js @@ -89,9 +89,9 @@ tap.test('Edge cases (CJS)', (t) => { handlerMethod = 'handler' process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: handlerMethod }) - }, `Unable to import module '${handlerPath}${handlerFile}'`, - `when handler file can't load dependencies`) + return handler({ key: 'this is a test'}, { functionName: handlerMethod }) + }, `Unable to import module '${handlerPath}${handlerFile}'`, + `when handler file can't load dependencies`) t.end() }) diff --git a/nodejs/test/unit/fixtures/badRequire.js b/nodejs/test/unit/fixtures/badRequire.js index 6da2dc0..7324956 100644 --- a/nodejs/test/unit/fixtures/badRequire.js +++ b/nodejs/test/unit/fixtures/badRequire.js @@ -1,10 +1,11 @@ 'use strict' +// eslint-disable-next-line no-unused-vars const notFoundDependency = require('path/not/found') exports.handler = (event) => { - return { - statusCode: 200, - body: JSON.stringify(event) - } + return { + statusCode: 200, + body: JSON.stringify(event) + } } From a4356239d99c9eb99881b5918c966dd8b4c7c48b Mon Sep 17 00:00:00 2001 From: Jessica Lopatta Date: Fri, 30 Jun 2023 11:02:40 -0400 Subject: [PATCH 4/4] test: add unit test coverage of wrapper --- nodejs/test/unit/cjsErrorStates.tap.js | 129 ++++++++++++++++++ nodejs/test/unit/errorStates.tap.js | 98 ------------- nodejs/test/unit/esmErrorStates.tap.js | 129 ++++++++++++++++++ .../unit/fixtures/{ => cjs}/badRequire.js | 0 nodejs/test/unit/fixtures/{ => cjs}/errors.js | 0 nodejs/test/unit/fixtures/esm/badImport.js | 9 ++ nodejs/test/unit/fixtures/esm/errors.js | 1 + 7 files changed, 268 insertions(+), 98 deletions(-) create mode 100644 nodejs/test/unit/cjsErrorStates.tap.js delete mode 100644 nodejs/test/unit/errorStates.tap.js create mode 100644 nodejs/test/unit/esmErrorStates.tap.js rename nodejs/test/unit/fixtures/{ => cjs}/badRequire.js (100%) rename nodejs/test/unit/fixtures/{ => cjs}/errors.js (100%) create mode 100644 nodejs/test/unit/fixtures/esm/badImport.js create mode 100644 nodejs/test/unit/fixtures/esm/errors.js diff --git a/nodejs/test/unit/cjsErrorStates.tap.js b/nodejs/test/unit/cjsErrorStates.tap.js new file mode 100644 index 0000000..20f35bd --- /dev/null +++ b/nodejs/test/unit/cjsErrorStates.tap.js @@ -0,0 +1,129 @@ +'use strict' + +const tap = require('tap') +const proxyquire = require('proxyquire').noCallThru().noPreserveCache() +const utils = require('@newrelic/test-utilities') +const path = require('node:path') + +tap.test('CJS Edge Cases', (t) => { + t.autoend() + let handler + let helper + let originalEnv + + t.beforeEach(() => { + originalEnv = { ...process.env } + process.env.NEW_RELIC_USE_ESM = 'false' + + helper = utils.TestAgent.makeInstrumented() + + const newrelic = helper.getAgentApi() + + ;({ handler } = proxyquire('../../index', { + 'newrelic': newrelic + })) + }) + + t.afterEach(() => { + process.env = { ...originalEnv } + helper.unload() + }) + + t.test('should delete serverless mode env var if defined', (t) => { + process.env.LAMBDA_TASK_ROOT = './' + process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED = 'true' + + // redundant, but needed for this test + const newrelic = helper.getAgentApi() + + ;({ handler } = proxyquire('../../index', { + 'newrelic': newrelic + })) + + t.notOk(process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED, + 'NEW_RELIC_SERVERLESS_MODE_ENABLED env var should have been deleted') + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER is missing', (t) => { + t.throws( + () => handler({ key: 'this is a test'}, { functionName: 'testFn'}), + 'No NEW_RELIC_LAMBDA_HANDLER environment variable set.', + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER is malformed', (t) => { + // Missing the .functionName part + process.env.NEW_RELIC_LAMBDA_HANDLER = 'test/unit/fixtures/cjs/handler' + + t.throws( + () => handler({ key: 'this is a test'}, { functionName: 'testFn'}), + 'Improperly formatted handler environment variable: test/unit/fixtures/cjs/handler', + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER module cannot be resolved', (t) => { + const handlerPath = 'test/unit/fixtures/cjs/' + const handlerFile = 'notFound' + const handlerMethod = 'noMethodFound' + const modulePath = path.resolve('./', handlerPath) + const extensions = ['.cjs', '.js'] + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Unable to resolve module file at ${modulePath} with the following extensions: ${extensions.join(',')}` + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER does not export provided function', (t) => { + const handlerPath = 'test/unit/fixtures/cjs/' + const handlerFile = 'errors' + const handlerMethod = 'noMethodFound' + + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Handler '${handlerMethod}' missing on module '${handlerPath}'`, + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER export is not a function', (t) => { + const handlerPath = 'test/unit/fixtures/cjs/' + const handlerFile = 'errors' + const handlerMethod = 'notAfunction' + + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Handler '${handlerMethod}' from 'test/unit/fixtures/cjs/errors' is not a function`, + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER throws on require', (t) => { + const handlerPath = 'test/unit/fixtures/cjs/' + const handlerFile = 'badRequire' + const handlerMethod = 'handler' + + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.throws( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Unable to import module '${handlerPath}${handlerFile}'`, + ) + + t.end() + }) +}) diff --git a/nodejs/test/unit/errorStates.tap.js b/nodejs/test/unit/errorStates.tap.js deleted file mode 100644 index 3d0965e..0000000 --- a/nodejs/test/unit/errorStates.tap.js +++ /dev/null @@ -1,98 +0,0 @@ -'use strict' - -const tap = require('tap') -const proxyquire = require('proxyquire').noCallThru().noPreserveCache() -const utils = require('@newrelic/test-utilities') -const path = require('node:path') - -tap.test('Edge cases (CJS)', (t) => { - t.autoend() - let handler - let helper - let originalEnv - - t.beforeEach(() => { - originalEnv = {...process.env} - process.env.NEW_RELIC_USE_ESM = 'false' - - helper = utils.TestAgent.makeInstrumented() - - const newrelic = helper.getAgentApi() - - ;({ handler } = proxyquire('../../index', { - 'newrelic': newrelic - })) - }) - - t.afterEach(() => { - process.env = {...originalEnv} - helper.unload() - }) - - t.test('should delete serverless mode env var if defined', (t) => { - process.env.LAMBDA_TASK_ROOT = './' - process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED = 'true' - - // redundant, but needed for this test - const newrelic = helper.getAgentApi() - - ;({ handler } = proxyquire('../../index', { - 'newrelic': newrelic - })) - - t.notOk(process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED, - 'NEW_RELIC_SERVERLESS_MODE_ENABLED env var should have been deleted') - t.end() - }) - - t.test('should throw', async(t) => { - t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: 'testFn'}) - }, 'No NEW_RELIC_LAMBDA_HANDLER environment variable set.', - 'when NEW_RELIC_LAMBDA_HANDLER is unset') - - process.env.NEW_RELIC_LAMBDA_HANDLER = 'test/unit/fixtures/cjs/handler' - t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: 'testFn'}) - }, `Improperly formatted handler environment variable: test/unit/fixtures/cjs/handler`, - 'when NEW_RELIC_LAMBDA_HANDLER is not defined as {path}.{function}') - - let handlerPath = 'test/unit/fixtures/' - let handlerFile = 'notFound' - let handlerMethod = 'noMethodFound' - let modulePath = path.resolve('./', handlerPath) - const extensions = ['.cjs', '.js'] - process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` - - t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: handlerMethod }) - }, `Unable to resolve module file at ${modulePath} with the following extensions: ${extensions.join(',')}`, - `when module file can't be found`) - - handlerFile = 'errors' - process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` - - t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: handlerMethod }) - }, `Handler '${handlerMethod}' missing on module '${handlerPath}'`, - `when file is found, but handler function is not found`) - - handlerMethod = 'notAfunction' - process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` - - t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: handlerMethod }) - }, `Handler '${handlerMethod}' from 'test/unit/fixtures/errors' is not a function`, - `when NEW_RELIC_LAMBDA_HANDLER is not a function`) - - handlerFile = 'badRequire' - handlerMethod = 'handler' - process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` - t.throws(() => { - return handler({ key: 'this is a test'}, { functionName: handlerMethod }) - }, `Unable to import module '${handlerPath}${handlerFile}'`, - `when handler file can't load dependencies`) - - t.end() - }) -}) diff --git a/nodejs/test/unit/esmErrorStates.tap.js b/nodejs/test/unit/esmErrorStates.tap.js new file mode 100644 index 0000000..b1d4082 --- /dev/null +++ b/nodejs/test/unit/esmErrorStates.tap.js @@ -0,0 +1,129 @@ +'use strict' + +const tap = require('tap') +const proxyquire = require('proxyquire').noCallThru().noPreserveCache() +const utils = require('@newrelic/test-utilities') +const path = require('node:path') + +tap.test('ESM Edge Cases', (t) => { + t.autoend() + let handler + let helper + let originalEnv + + t.beforeEach(() => { + originalEnv = { ...process.env } + process.env.NEW_RELIC_USE_ESM = 'true' + + helper = utils.TestAgent.makeInstrumented() + + const newrelic = helper.getAgentApi() + + ;({ handler } = proxyquire('../../index', { + 'newrelic': newrelic + })) + }) + + t.afterEach(() => { + process.env = { ...originalEnv } + helper.unload() + }) + + t.test('should delete serverless mode env var if defined', async(t) => { + process.env.LAMBDA_TASK_ROOT = './' + process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED = 'true' + + // redundant, but needed for this test + const newrelic = helper.getAgentApi() + + ;({ handler } = proxyquire('../../index', { + 'newrelic': newrelic + })) + + t.notOk(process.env.NEW_RELIC_SERVERLESS_MODE_ENABLED, + 'NEW_RELIC_SERVERLESS_MODE_ENABLED env var should have been deleted') + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER is missing', (t) => { + t.rejects( + () => handler({ key: 'this is a test'}, { functionName: 'testFn'}), + 'No NEW_RELIC_LAMBDA_HANDLER environment variable set.', + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER is malformed', async(t) => { + // Missing the .functionName part + process.env.NEW_RELIC_LAMBDA_HANDLER = 'test/unit/fixtures/esm/handler' + + t.rejects( + () => handler({ key: 'this is a test'}, { functionName: 'testFn'}), + 'Improperly formatted handler environment variable: test/unit/fixtures/esm/handler', + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER module cannot be resolved', async(t) => { + const handlerPath = 'test/unit/fixtures/esm/' + const handlerFile = 'notFound' + const handlerMethod = 'noMethodFound' + const modulePath = path.resolve('./', handlerPath) + const extensions = ['.mjs', '.js'] + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.rejects( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Unable to resolve module file at ${modulePath} with the following extensions: ${extensions.join(',')}` + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER does not export provided function', async(t) => { + const handlerPath = 'test/unit/fixtures/esm/' + const handlerFile = 'errors' + const handlerMethod = 'noMethodFound' + + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.rejects( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Handler '${handlerMethod}' missing on module '${handlerPath}'`, + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER export is not a function', async(t) => { + const handlerPath = 'test/unit/fixtures/esm/' + const handlerFile = 'errors' + const handlerMethod = 'notAfunction' + + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.rejects( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Handler '${handlerMethod}' from 'test/unit/fixtures/esm/errors' is not a function`, + ) + + t.end() + }) + + t.test('should throw when NEW_RELIC_LAMBDA_HANDLER throws on import', async(t) => { + const handlerPath = 'test/unit/fixtures/esm/' + const handlerFile = 'badImport' + const handlerMethod = 'handler' + + process.env.NEW_RELIC_LAMBDA_HANDLER = `${handlerPath}${handlerFile}.${handlerMethod}` + + t.rejects( + () => handler({ key: 'this is a test'}, { functionName: handlerMethod }), + `Unable to import module '${handlerPath}${handlerFile}'`, + ) + + t.end() + }) +}) diff --git a/nodejs/test/unit/fixtures/badRequire.js b/nodejs/test/unit/fixtures/cjs/badRequire.js similarity index 100% rename from nodejs/test/unit/fixtures/badRequire.js rename to nodejs/test/unit/fixtures/cjs/badRequire.js diff --git a/nodejs/test/unit/fixtures/errors.js b/nodejs/test/unit/fixtures/cjs/errors.js similarity index 100% rename from nodejs/test/unit/fixtures/errors.js rename to nodejs/test/unit/fixtures/cjs/errors.js diff --git a/nodejs/test/unit/fixtures/esm/badImport.js b/nodejs/test/unit/fixtures/esm/badImport.js new file mode 100644 index 0000000..6e9c214 --- /dev/null +++ b/nodejs/test/unit/fixtures/esm/badImport.js @@ -0,0 +1,9 @@ +// eslint-disable-next-line no-unused-vars +import notFoundDependency from 'path/not/found' + +export function handler(event) { + return { + statusCode: 200, + body: JSON.stringify(event) + } +} diff --git a/nodejs/test/unit/fixtures/esm/errors.js b/nodejs/test/unit/fixtures/esm/errors.js new file mode 100644 index 0000000..d4a8fe2 --- /dev/null +++ b/nodejs/test/unit/fixtures/esm/errors.js @@ -0,0 +1 @@ +export const notAfunction = `This is a string.`