Skip to content

test(maintenance): improve assertions on traces & logs #3012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 4, 2024
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ updates:
- "aws-sdk-client-mock-jest"
aws-cdk:
patterns:
- "@aws-cdk/*"
- "@aws-cdk/cli-lib-alpha"
- "aws-cdk-lib"
- "aws-cdk"
typedoc:
Expand Down
101 changes: 71 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 20 additions & 14 deletions packages/idempotency/tests/e2e/idempotentDecorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
*
* @group e2e/idempotency/decorator
*/
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import {
RESOURCE_NAME_PREFIX,
SETUP_TIMEOUT,
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT,
} from './constants.js';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
import { createHash } from 'node:crypto';
import { join } from 'node:path';
import {
invokeFunction,
TestInvocationLogs,
TestStack,
invokeFunction,
} from '@aws-lambda-powertools/testing-utils';
import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js';
import { join } from 'node:path';
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 { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js';
import {
RESOURCE_NAME_PREFIX,
SETUP_TIMEOUT,
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT,
} from './constants.js';

const dynamoDBClient = new DynamoDBClient({});

Expand Down Expand Up @@ -289,9 +289,15 @@ describe('Idempotency e2e test decorator, default settings', () => {
});
expect(idempotencyRecord.Items?.[0].status).toEqual('COMPLETED');

// During the first invocation the handler should be called, so the logs should contain 1 log
expect(functionLogs[0]).toHaveLength(2);
expect(functionLogs[0][0]).toContain('Task timed out after');
try {
// During the first invocation the handler should be called, so the logs should contain 1 log
expect(functionLogs[0]).toHaveLength(2);
expect(functionLogs[0][0]).toContain('Task timed out after');
} catch {
// During the first invocation the function should timeout so the logs should not contain any log and the report log should contain a timeout message
expect(functionLogs[0]).toHaveLength(0);
expect(logs[0].getReportLog()).toMatch(/Status: timeout$/);
}

expect(functionLogs[1]).toHaveLength(1);
expect(TestInvocationLogs.parseFunctionLog(functionLogs[1][0])).toEqual(
Expand Down
17 changes: 12 additions & 5 deletions packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createHash } from 'node:crypto';
import { join } from 'node:path';
/**
* Test makeHandlerIdempotent middleware
*
* @group e2e/idempotency/makeHandlerIdempotent
*/
import { createHash } from 'node:crypto';
import { join } from 'node:path';
import {
TestInvocationLogs,
TestStack,
Expand Down Expand Up @@ -263,9 +263,16 @@ describe('Idempotency E2E tests, middy middleware usage', () => {
});
expect(idempotencyRecords.Items?.[0].status).toEqual('COMPLETED');

// During the first invocation the function should timeout so the logs should contain 2 logs
expect(functionLogs[0]).toHaveLength(2);
expect(functionLogs[0][0]).toContain('Task timed out after');
try {
// During the first invocation the handler should be called, so the logs should contain 1 log
expect(functionLogs[0]).toHaveLength(2);
expect(functionLogs[0][0]).toContain('Task timed out after');
} catch {
// During the first invocation the function should timeout so the logs should not contain any log and the report log should contain a timeout message
expect(functionLogs[0]).toHaveLength(0);
expect(logs[0].getReportLog()).toMatch(/Status: timeout$/);
}

// During the second invocation the handler should be called and complete, so the logs should
// contain 1 log
expect(functionLogs[1]).toHaveLength(1);
Expand Down
4 changes: 2 additions & 2 deletions packages/idempotency/tests/e2e/makeIdempotent.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createHash } from 'node:crypto';
import { join } from 'node:path';
/**
* Test makeIdempotent function
*
* @group e2e/idempotency/makeIdempotent
*/
import { createHash } from 'node:crypto';
import { join } from 'node:path';
import {
TestInvocationLogs,
TestStack,
Expand Down
18 changes: 15 additions & 3 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
"./types": {
"import": "./lib/esm/types.js",
"require": "./lib/cjs/types.js"
},
"./utils/xray-traces": {
"import": "./lib/esm/xray-traces-utils.js",
"require": "./lib/cjs/xray-traces-utils.js"
}
},
"typesVersions": {
Expand All @@ -72,6 +76,10 @@
"context": [
"lib/cjs/context.d.ts",
"lib/esm/context.d.ts"
],
"utils/xray-traces": [
"lib/cjs/xray-traces-utils.d.ts",
"lib/esm/xray-traces-utils.d.ts"
]
}
},
Expand All @@ -90,10 +98,14 @@
},
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/testing#readme",
"dependencies": {
"@aws-cdk/cli-lib-alpha": "^2.121.1-alpha.0",
"@aws-cdk/cli-lib-alpha": "^2.155.0-alpha.0",
"@aws-sdk/client-lambda": "^3.637.0",
"@smithy/util-utf8": "^3.0.0",
"aws-cdk-lib": "^2.155.0",
"esbuild": "^0.23.1"
"esbuild": "^0.23.1",
"promise-retry": "^2.0.1"
},
"devDependencies": {
"@types/promise-retry": "^1.1.6"
}
}
}
9 changes: 9 additions & 0 deletions packages/testing/src/TestInvocationLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class TestInvocationLogs {
);
}

/**
* Return the log that contains the report of the function `REPORT RequestId`
*/
public getReportLog(): string {
const endLogIndex = TestInvocationLogs.getReportLogIndex(this.logs);

return this.logs[endLogIndex];
}

/**
* Return the index of the log that contains `REPORT RequestId`
* @param logs
Expand Down
Loading
Loading