From a21fcb9f3690d8e15eb0c08e3912d5907e3818eb Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 16 Aug 2024 13:00:47 +0000 Subject: [PATCH 1/2] tests(maintenance): migrate testing utils to vitest --- packages/testing/jest.config.cjs | 31 -------- packages/testing/package.json | 72 ++++++++++--------- .../helpers/populateEnvironmentVariables.ts | 16 ----- .../tests/unit/TestInvocationLogs.test.ts | 29 ++++---- packages/testing/vitest.config.ts | 10 +++ 5 files changed, 61 insertions(+), 97 deletions(-) delete mode 100644 packages/testing/jest.config.cjs delete mode 100644 packages/testing/tests/helpers/populateEnvironmentVariables.ts create mode 100644 packages/testing/vitest.config.ts diff --git a/packages/testing/jest.config.cjs b/packages/testing/jest.config.cjs deleted file mode 100644 index f13ab92dfd..0000000000 --- a/packages/testing/jest.config.cjs +++ /dev/null @@ -1,31 +0,0 @@ -module.exports = { - displayName: { - name: 'Powertools for AWS Lambda (TypeScript) utility: TESTING', - color: 'blue', - }, - runner: 'groups', - preset: 'ts-jest', - moduleNameMapper: { - '^(\\.{1,2}/.*)\\.js$': '$1', - }, - transform: { - '^.+\\.ts?$': 'ts-jest', - }, - moduleFileExtensions: ['js', 'ts'], - collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'], - testMatch: ['**/?(*.)+(spec|test).ts'], - roots: ['/src', '/tests'], - testPathIgnorePatterns: ['/node_modules/'], - testEnvironment: 'node', - coveragePathIgnorePatterns: ['/node_modules/', '/types/'], - coverageThreshold: { - global: { - statements: 100, - branches: 100, - functions: 100, - lines: 100, - }, - }, - coverageReporters: ['json-summary', 'text', 'lcov'], - setupFiles: ['/tests/helpers/populateEnvironmentVariables.ts'], -}; diff --git a/packages/testing/package.json b/packages/testing/package.json index d21635a13c..a82ee67b4b 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -8,9 +8,8 @@ }, "private": true, "scripts": { - "test": "npm run test:unit", - "test:unit": "jest --group=unit --detectOpenHandles --verbose", - "jest": "jest --detectOpenHandles --verbose", + "test": "vitest --run", + "test:unit": "vitest --run", "test:e2e": "echo 'Not implemented'", "watch": "jest --watch", "build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json", @@ -31,49 +30,56 @@ "exports": { ".": { "require": { - "types": "./lib/cjs/index.d.ts", - "default": "./lib/cjs/index.js" + "source": "./src/index.ts", + "default": "./lib/cjs/index.js", + "types": "./lib/cjs/index.d.ts" }, "import": { - "types": "./lib/esm/index.d.ts", - "default": "./lib/esm/index.js" + "source": "./src/index.ts", + "default": "./lib/esm/index.js", + "types": "./lib/esm/index.d.ts" } }, "./resources/lambda": { - "import": "./lib/esm/resources/TestNodejsFunction.js", - "require": "./lib/cjs/resources/TestNodejsFunction.js" + "import": { + "source": "./src/resources/TestNodejsFunction.ts", + "default": "./lib/esm/resources/TestNodejsFunction.js", + "types": "./lib/esm/resources/TestNodejsFunction.d.ts" + }, + "require": { + "source": "./src/resources/TestNodejsFunction.ts", + "default": "./lib/cjs/resources/TestNodejsFunction.js", + "types": "./lib/cjs/resources/TestNodejsFunction.d.ts" + } }, "./resources/dynamodb": { - "import": "./lib/esm/resources/TestDynamodbTable.js", - "require": "./lib/cjs/resources/TestDynamodbTable.js" + "import": { + "source": "./src/resources/TestDynamodbTable.ts", + "default": "./lib/esm/resources/TestDynamodbTable.js", + "types": "./lib/esm/resources/TestDynamodbTable.d.ts" + }, + "require": { + "source": "./src/resources/TestDynamodbTable.ts", + "default": "./lib/cjs/resources/TestDynamodbTable.js", + "types": "./lib/cjs/resources/TestDynamodbTable.d.ts" + } }, "./context": { + "source": "./src/context.ts", "import": "./lib/esm/context.js", "require": "./lib/cjs/context.js" }, "./types": { - "import": "./lib/esm/types.js", - "require": "./lib/cjs/types.js" - } - }, - "typesVersions": { - "*": { - "resources/lambda": [ - "lib/cjs/resources/TestNodejsFunction.d.ts", - "lib/esm/resources/TestNodejsFunction.d.ts" - ], - "resources/dynamodb": [ - "lib/cjs/resources/TestDynamodbTable.d.ts", - "lib/esm/resources/TestDynamodbTable.d.ts" - ], - "types": [ - "lib/cjs/types.d.ts", - "lib/esm/types.d.ts" - ], - "context": [ - "lib/cjs/context.d.ts", - "lib/esm/context.d.ts" - ] + "import": { + "source": "./src/types.ts", + "default": "./lib/esm/types.js", + "types": "./lib/esm/types.d.ts" + }, + "require": { + "source": "./src/types.ts", + "default": "./lib/cjs/types.js", + "types": "./lib/esm/types.d.ts" + } } }, "types": "./lib/cjs/index.d.ts", diff --git a/packages/testing/tests/helpers/populateEnvironmentVariables.ts b/packages/testing/tests/helpers/populateEnvironmentVariables.ts deleted file mode 100644 index ed8dde8cd5..0000000000 --- a/packages/testing/tests/helpers/populateEnvironmentVariables.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Reserved variables -process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12'; -process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function'; -process.env.AWS_EXECUTION_ENV = 'nodejs20.x'; -process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128'; -if ( - process.env.AWS_REGION === undefined && - process.env.CDK_DEFAULT_REGION === undefined -) { - process.env.AWS_REGION = 'eu-west-1'; -} -process.env._HANDLER = 'index.handler'; - -// Powertools for AWS Lambda (TypeScript) variables -process.env.POWERTOOLS_SERVICE_NAME = 'hello-world'; -process.env.AWS_XRAY_LOGGING_LEVEL = 'silent'; diff --git a/packages/testing/tests/unit/TestInvocationLogs.test.ts b/packages/testing/tests/unit/TestInvocationLogs.test.ts index 74c84730ce..de13cfae72 100644 --- a/packages/testing/tests/unit/TestInvocationLogs.test.ts +++ b/packages/testing/tests/unit/TestInvocationLogs.test.ts @@ -1,9 +1,4 @@ -/** - * Test InvocationLogs class - * - * @group unit/commons/invocationLogs - * - */ +import { beforeEach, describe, expect, it } from 'vitest'; import { TestInvocationLogs } from '../../src/TestInvocationLogs.js'; const exampleLogs = `START RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678 Version: $LATEST @@ -15,7 +10,7 @@ END RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678 REPORT RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678\tDuration: 2.16 ms\tBilled Duration: 3 ms\tMemory Size: 128 MB\tMax Memory Used: 57 MB\t`; describe('Constructor', () => { - test('it should parse base64 text correctly', () => { + it('parses base64 text correctly', () => { const invocationLogs = new TestInvocationLogs( Buffer.from(exampleLogs).toString('base64') ); @@ -33,29 +28,29 @@ describe('doesAnyFunctionLogsContains()', () => { Buffer.from(exampleLogs).toString('base64') ); }); - test('it should return true if the text appear in any logs', () => { + it('returns true if the text appear in any of the logs', () => { const phraseInMessage = 'This is'; expect(invocationLogs.doesAnyFunctionLogsContains(phraseInMessage)).toBe( true ); }); - test('it should return false if the text does not appear in any logs', () => { + it('returns false if the text does not appear anywhere', () => { const phraseNotInMessage = 'A quick brown fox jumps over the lazy dog'; expect(invocationLogs.doesAnyFunctionLogsContains(phraseNotInMessage)).toBe( false ); }); - test('it should return true for key in the log', () => { + it('returns true if the provided key appears in any of the logs', () => { const keyInLog = 'error'; expect(invocationLogs.doesAnyFunctionLogsContains(keyInLog)).toBe(true); }); - test('it should return true for a text in an error key', () => { + it('returns true it the provided text appears in an error key within the logs', () => { const textInError = '/var/task/index.js:2778'; expect(invocationLogs.doesAnyFunctionLogsContains(textInError)).toBe(true); }); - test('it should return false for the text that appears only on the ', () => { + it('excludes the report logs from the search', () => { const textInStartLine = 'Version: $LATEST'; const textInEndLine = 'END RequestId'; const textInReportLine = 'Billed Duration'; @@ -70,7 +65,7 @@ describe('doesAnyFunctionLogsContains()', () => { ); }); - test('it should apply filter log based on the given level', () => { + it('filters log based on the given level', () => { const debugLogHasWordINFO = invocationLogs.doesAnyFunctionLogsContains( 'INFO', 'DEBUG' @@ -100,7 +95,7 @@ describe('getFunctionLogs()', () => { ); }); - test('it should retrive logs of the given level only', () => { + it('retrives logs of the given level only', () => { const infoLogs = invocationLogs.getFunctionLogs('INFO'); expect(infoLogs.length).toBe(2); expect(infoLogs[0].includes('INFO')).toBe(true); @@ -114,7 +109,7 @@ describe('getFunctionLogs()', () => { expect(errorLogs[0].includes('ERROR')).toBe(true); }); - test('it should NOT return logs generated by Lambda service (e.g. START, END, and REPORT)', () => { + it("doesn't return logs generated by Lambda service (e.g. START, END, and REPORT)", () => { const errorLogs = invocationLogs.getFunctionLogs('ERROR'); expect(errorLogs.length).toBe(1); expect(errorLogs[0].includes('START')).toBe(false); @@ -124,7 +119,7 @@ describe('getFunctionLogs()', () => { }); describe('parseFunctionLog()', () => { - test('it should return object with the correct values based on the given log', () => { + it('returns an object with the correct values based on the given log', () => { const rawLogStr = '{"cold_start":true,"function_arn":"arn:aws:lambda:eu-west-1:561912387782:function:loggerMiddyStandardFeatures-c555a2ec-1121-4586-9c04-185ab36ea34c","function_memory_size":128,"function_name":"loggerMiddyStandardFeatures-c555a2ec-1121-4586-9c04-185ab36ea34c","function_request_id":"7f586697-238a-4c3b-9250-a5f057c1119c","level":"DEBUG","message":"This is a DEBUG log but contains the word INFO some context and persistent key","service":"logger-e2e-testing","timestamp":"2022-01-27T16:04:39.323Z","persistentKey":"works"}'; @@ -146,7 +141,7 @@ describe('parseFunctionLog()', () => { }); }); - test('it should throw an error if receive incorrect formatted raw log string', () => { + it('throws an error if receive incorrect formatted raw log string', () => { const notJSONstring = 'not-json-string'; expect(() => { TestInvocationLogs.parseFunctionLog(notJSONstring); diff --git a/packages/testing/vitest.config.ts b/packages/testing/vitest.config.ts new file mode 100644 index 0000000000..da7de22714 --- /dev/null +++ b/packages/testing/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineProject } from 'vitest/config'; + +export default defineProject({ + resolve: { + conditions: ['source'], + }, + test: { + environment: 'node', + }, +}); From ad78cfe835c97f9bebb6db81383985e0977e1203 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 20 Aug 2024 14:26:34 +0000 Subject: [PATCH 2/2] revert exports changes --- packages/testing/package.json | 69 ++++++++++++++++------------------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/packages/testing/package.json b/packages/testing/package.json index a82ee67b4b..5185781880 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -30,56 +30,49 @@ "exports": { ".": { "require": { - "source": "./src/index.ts", - "default": "./lib/cjs/index.js", - "types": "./lib/cjs/index.d.ts" + "types": "./lib/cjs/index.d.ts", + "default": "./lib/cjs/index.js" }, "import": { - "source": "./src/index.ts", - "default": "./lib/esm/index.js", - "types": "./lib/esm/index.d.ts" + "types": "./lib/esm/index.d.ts", + "default": "./lib/esm/index.js" } }, "./resources/lambda": { - "import": { - "source": "./src/resources/TestNodejsFunction.ts", - "default": "./lib/esm/resources/TestNodejsFunction.js", - "types": "./lib/esm/resources/TestNodejsFunction.d.ts" - }, - "require": { - "source": "./src/resources/TestNodejsFunction.ts", - "default": "./lib/cjs/resources/TestNodejsFunction.js", - "types": "./lib/cjs/resources/TestNodejsFunction.d.ts" - } + "import": "./lib/esm/resources/TestNodejsFunction.js", + "require": "./lib/cjs/resources/TestNodejsFunction.js" }, "./resources/dynamodb": { - "import": { - "source": "./src/resources/TestDynamodbTable.ts", - "default": "./lib/esm/resources/TestDynamodbTable.js", - "types": "./lib/esm/resources/TestDynamodbTable.d.ts" - }, - "require": { - "source": "./src/resources/TestDynamodbTable.ts", - "default": "./lib/cjs/resources/TestDynamodbTable.js", - "types": "./lib/cjs/resources/TestDynamodbTable.d.ts" - } + "import": "./lib/esm/resources/TestDynamodbTable.js", + "require": "./lib/cjs/resources/TestDynamodbTable.js" }, "./context": { - "source": "./src/context.ts", "import": "./lib/esm/context.js", "require": "./lib/cjs/context.js" }, "./types": { - "import": { - "source": "./src/types.ts", - "default": "./lib/esm/types.js", - "types": "./lib/esm/types.d.ts" - }, - "require": { - "source": "./src/types.ts", - "default": "./lib/cjs/types.js", - "types": "./lib/esm/types.d.ts" - } + "import": "./lib/esm/types.js", + "require": "./lib/cjs/types.js" + } + }, + "typesVersions": { + "*": { + "resources/lambda": [ + "lib/cjs/resources/TestNodejsFunction.d.ts", + "lib/esm/resources/TestNodejsFunction.d.ts" + ], + "resources/dynamodb": [ + "lib/cjs/resources/TestDynamodbTable.d.ts", + "lib/esm/resources/TestDynamodbTable.d.ts" + ], + "types": [ + "lib/cjs/types.d.ts", + "lib/esm/types.d.ts" + ], + "context": [ + "lib/cjs/context.d.ts", + "lib/esm/context.d.ts" + ] } }, "types": "./lib/cjs/index.d.ts", @@ -103,4 +96,4 @@ "aws-cdk-lib": "^2.152.0", "esbuild": "^0.23.0" } -} +} \ No newline at end of file