From 9cababe51e0b126ac92b91022797627ec74115ee Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 1 Oct 2024 11:13:28 +0200 Subject: [PATCH 1/5] test(idempotency): switch e2e tests to vitest --- packages/idempotency/jest.config.cjs | 31 ------------------- packages/idempotency/package.json | 11 ++++--- .../tests/e2e/idempotentDecorator.test.ts | 30 ++++++++---------- .../tests/e2e/makeHandlerIdempotent.test.ts | 22 ++++++------- .../tests/e2e/makeIdempotent.test.ts | 14 +++------ 5 files changed, 33 insertions(+), 75 deletions(-) delete mode 100644 packages/idempotency/jest.config.cjs diff --git a/packages/idempotency/jest.config.cjs b/packages/idempotency/jest.config.cjs deleted file mode 100644 index 758a5e9f23..0000000000 --- a/packages/idempotency/jest.config.cjs +++ /dev/null @@ -1,31 +0,0 @@ -module.exports = { - displayName: { - name: 'Powertools for AWS Lambda (TypeScript) utility: IDEMPOTENCY', - color: 'yellow', - }, - 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/idempotency/package.json b/packages/idempotency/package.json index d350f22cb6..b497763579 100644 --- a/packages/idempotency/package.json +++ b/packages/idempotency/package.json @@ -10,13 +10,14 @@ "access": "public" }, "scripts": { - "test": "vitest --run", - "test:unit": "vitest --run", + "test": "vitest --run tests/unit", + "test:unit": "vitest --run tests/unit", "test:unit:coverage": "vitest --run tests/unit --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'", "test:unit:types": "echo 'Not Implemented'", - "test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e", - "test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e", - "test:e2e": "jest --group=e2e", + "test:unit:watch": "vitest tests/unit", + "test:e2e:nodejs18x": "RUNTIME=nodejs18x vitest --run tests/e2e", + "test:e2e:nodejs20x": "RUNTIME=nodejs20x vitest --run tests/e2e", + "test:e2e": "vitest --run tests/e2e", "build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json", "build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json", "build": "npm run build:esm & npm run build:cjs", diff --git a/packages/idempotency/tests/e2e/idempotentDecorator.test.ts b/packages/idempotency/tests/e2e/idempotentDecorator.test.ts index ca22922e83..00f0aa236d 100644 --- a/packages/idempotency/tests/e2e/idempotentDecorator.test.ts +++ b/packages/idempotency/tests/e2e/idempotentDecorator.test.ts @@ -1,8 +1,3 @@ -/** - * Test idempotency decorator - * - * @group e2e/idempotency/decorator - */ import { createHash } from 'node:crypto'; import { join } from 'node:path'; import { @@ -14,6 +9,7 @@ import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { ScanCommand } from '@aws-sdk/lib-dynamodb'; import { Duration } from 'aws-cdk-lib'; import { AttributeType } from 'aws-cdk-lib/aws-dynamodb'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, @@ -166,8 +162,8 @@ describe('Idempotency e2e test decorator, default settings', () => { tableNameDataIndex = testStack.findAndGetStackOutputValue('dataIndexTable'); }, SETUP_TIMEOUT); - test( - 'when called twice with the same payload, it returns the same result and runs the handler once', + it( + 'returns the same result and runs the handler once when called multiple times', async () => { const payload = { foo: 'bar' }; @@ -208,8 +204,8 @@ describe('Idempotency e2e test decorator, default settings', () => { TEST_CASE_TIMEOUT ); - test( - 'when called twice in parallel, the handler is called only once', + it( + 'handles parallel invocations correctly', async () => { const payload = { foo: 'bar' }; const payloadHash = createHash('md5') @@ -256,8 +252,8 @@ describe('Idempotency e2e test decorator, default settings', () => { TEST_CASE_TIMEOUT ); - test( - 'when the function times out, the second request is processed correctly by the handler', + it( + 'recovers from a timed out request and processes the next one', async () => { const payload = { foo: 'bar' }; const payloadHash = createHash('md5') @@ -311,8 +307,8 @@ describe('Idempotency e2e test decorator, default settings', () => { TEST_CASE_TIMEOUT ); - test( - 'when the idempotency record is expired, the second request is processed correctly by the handler', + it( + 'recovers from an expired idempotency record and processes the next request', async () => { const payload = { foo: 'baz', @@ -385,8 +381,8 @@ describe('Idempotency e2e test decorator, default settings', () => { TEST_CASE_TIMEOUT ); - test( - 'when called with customized function wrapper, it creates ddb entry with custom attributes', + it( + 'uses the provided custom idempotency record attributes', async () => { const payload = { foo: 'bar' }; const payloadHash = createHash('md5') @@ -425,8 +421,8 @@ describe('Idempotency e2e test decorator, default settings', () => { TEST_CASE_TIMEOUT ); - test( - 'when called twice for with different payload using data index arugment, it returns the same result and runs the handler once', + it( + 'takes the data index argument into account when making the function idempotent', async () => { const payload = [{ id: '1234' }, { id: '5678' }]; const payloadHash = createHash('md5') diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index f8f5339c91..fbd841e647 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -1,8 +1,3 @@ -/** - * Test makeHandlerIdempotent middleware - * - * @group e2e/idempotency/makeHandlerIdempotent - */ import { createHash } from 'node:crypto'; import { join } from 'node:path'; import { @@ -13,6 +8,7 @@ import { import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { ScanCommand } from '@aws-sdk/lib-dynamodb'; import { Duration } from 'aws-cdk-lib'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, @@ -116,8 +112,8 @@ describe('Idempotency E2E tests, middy middleware usage', () => { tableNameExpired = testStack.findAndGetStackOutputValue('expiredTable'); }, SETUP_TIMEOUT); - test( - 'when called twice with the same payload, it returns the same result and runs the handler once', + it( + 'returns the same result and runs the handler once', async () => { // Prepare const payload = { @@ -166,8 +162,8 @@ describe('Idempotency E2E tests, middy middleware usage', () => { TEST_CASE_TIMEOUT ); - test( - 'when two identical requests are sent in parallel, the handler is called only once', + it( + 'handles idempotent requests sent in parallel', async () => { // Prepare const payload = { @@ -224,8 +220,8 @@ describe('Idempotency E2E tests, middy middleware usage', () => { TEST_CASE_TIMEOUT ); - test( - 'when the function times out, the second request is processed correctly by the handler', + it( + 'recovers from a timed out request and processes the second request correctly', async () => { // Prepare const payload = { @@ -287,8 +283,8 @@ describe('Idempotency E2E tests, middy middleware usage', () => { TEST_CASE_TIMEOUT ); - test( - 'when the idempotency record is expired, the second request is processed correctly by the handler', + it( + 'recovers from an expired idempotency record and processes the subsequent request correctly', async () => { // Prepare const payload = { diff --git a/packages/idempotency/tests/e2e/makeIdempotent.test.ts b/packages/idempotency/tests/e2e/makeIdempotent.test.ts index 9ae7609198..d510a0c893 100644 --- a/packages/idempotency/tests/e2e/makeIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeIdempotent.test.ts @@ -1,8 +1,3 @@ -/** - * Test makeIdempotent function - * - * @group e2e/idempotency/makeIdempotent - */ import { createHash } from 'node:crypto'; import { join } from 'node:path'; import { @@ -13,6 +8,7 @@ import { import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { ScanCommand } from '@aws-sdk/lib-dynamodb'; import { AttributeType } from 'aws-cdk-lib/aws-dynamodb'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, @@ -167,8 +163,8 @@ describe('Idempotency E2E tests, wrapper function usage', () => { TEST_CASE_TIMEOUT ); - test( - 'when called with customized function wrapper, it creates ddb entry with custom attributes', + it( + 'creates a DynamoDB item with the correct attributes', async () => { // Prepare const payload = { @@ -281,8 +277,8 @@ describe('Idempotency E2E tests, wrapper function usage', () => { TEST_CASE_TIMEOUT ); - test( - 'when called twice with the same payload, it returns the same result and runs the handler once', + it( + 'calls the wrapped function once and always returns the same result when called multiple times', async () => { // Prepare const payload = { From 95174e90365bb253092095c2200b5abfcd917088 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 3 Oct 2024 02:03:25 +0200 Subject: [PATCH 2/5] Update makeHandlerIdempotent.test.ts Co-authored-by: Leandro Damascena --- packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index fbd841e647..08bc352f86 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -113,7 +113,7 @@ describe('Idempotency E2E tests, middy middleware usage', () => { }, SETUP_TIMEOUT); it( - 'returns the same result and runs the handler once', + 'returns the same result and runs the handler once when called multiple times', async () => { // Prepare const payload = { From dacf48010e88fe1fadefd90d4ca2f62e8c3e096b Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 3 Oct 2024 02:03:32 +0200 Subject: [PATCH 3/5] Update makeHandlerIdempotent.test.ts Co-authored-by: Leandro Damascena --- packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index 08bc352f86..cdfa12cf7c 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -163,7 +163,7 @@ describe('Idempotency E2E tests, middy middleware usage', () => { ); it( - 'handles idempotent requests sent in parallel', + 'handles parallel invocations correctly', async () => { // Prepare const payload = { From 69156aca91be7ec9f3d5a71fc6c7448e8ecc70dd Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 3 Oct 2024 02:03:38 +0200 Subject: [PATCH 4/5] Update makeHandlerIdempotent.test.ts Co-authored-by: Leandro Damascena --- packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index cdfa12cf7c..f41c8a7aad 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -221,7 +221,7 @@ describe('Idempotency E2E tests, middy middleware usage', () => { ); it( - 'recovers from a timed out request and processes the second request correctly', + 'recovers from a timed out request and processes the next one', async () => { // Prepare const payload = { From a817f353f3bd622d96a6b6df3140ff7c353d3257 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 3 Oct 2024 02:03:50 +0200 Subject: [PATCH 5/5] Update makeHandlerIdempotent.test.ts Co-authored-by: Leandro Damascena --- packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index f41c8a7aad..f6a2cfbf63 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -284,7 +284,7 @@ describe('Idempotency E2E tests, middy middleware usage', () => { ); it( - 'recovers from an expired idempotency record and processes the subsequent request correctly', + 'recovers from an expired idempotency record and processes the next request', async () => { // Prepare const payload = {