Skip to content

Commit 6116607

Browse files
dreamorosiam29d
andauthored
test(maintenance): improve assertions on traces & logs (#3012)
Co-authored-by: Alexander Schueren <[email protected]>
1 parent 5d4043b commit 6116607

18 files changed

+957
-1027
lines changed

Diff for: .github/dependabot.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ updates:
4444
- "aws-sdk-client-mock-jest"
4545
aws-cdk:
4646
patterns:
47-
- "@aws-cdk/*"
47+
- "@aws-cdk/cli-lib-alpha"
4848
- "aws-cdk-lib"
4949
- "aws-cdk"
5050
typedoc:

Diff for: package-lock.json

+71-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/idempotency/tests/e2e/idempotentDecorator.test.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
*
44
* @group e2e/idempotency/decorator
55
*/
6-
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
7-
import {
8-
RESOURCE_NAME_PREFIX,
9-
SETUP_TIMEOUT,
10-
TEARDOWN_TIMEOUT,
11-
TEST_CASE_TIMEOUT,
12-
} from './constants.js';
13-
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
146
import { createHash } from 'node:crypto';
7+
import { join } from 'node:path';
158
import {
16-
invokeFunction,
179
TestInvocationLogs,
1810
TestStack,
11+
invokeFunction,
1912
} from '@aws-lambda-powertools/testing-utils';
20-
import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js';
21-
import { join } from 'node:path';
13+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
14+
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
2215
import { Duration } from 'aws-cdk-lib';
2316
import { AttributeType } from 'aws-cdk-lib/aws-dynamodb';
17+
import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js';
18+
import {
19+
RESOURCE_NAME_PREFIX,
20+
SETUP_TIMEOUT,
21+
TEARDOWN_TIMEOUT,
22+
TEST_CASE_TIMEOUT,
23+
} from './constants.js';
2424

2525
const dynamoDBClient = new DynamoDBClient({});
2626

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

292-
// During the first invocation the handler should be called, so the logs should contain 1 log
293-
expect(functionLogs[0]).toHaveLength(2);
294-
expect(functionLogs[0][0]).toContain('Task timed out after');
292+
try {
293+
// During the first invocation the handler should be called, so the logs should contain 1 log
294+
expect(functionLogs[0]).toHaveLength(2);
295+
expect(functionLogs[0][0]).toContain('Task timed out after');
296+
} catch {
297+
// 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
298+
expect(functionLogs[0]).toHaveLength(0);
299+
expect(logs[0].getReportLog()).toMatch(/Status: timeout$/);
300+
}
295301

296302
expect(functionLogs[1]).toHaveLength(1);
297303
expect(TestInvocationLogs.parseFunctionLog(functionLogs[1][0])).toEqual(

Diff for: packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { createHash } from 'node:crypto';
2-
import { join } from 'node:path';
31
/**
42
* Test makeHandlerIdempotent middleware
53
*
64
* @group e2e/idempotency/makeHandlerIdempotent
75
*/
6+
import { createHash } from 'node:crypto';
7+
import { join } from 'node:path';
88
import {
99
TestInvocationLogs,
1010
TestStack,
@@ -263,9 +263,16 @@ describe('Idempotency E2E tests, middy middleware usage', () => {
263263
});
264264
expect(idempotencyRecords.Items?.[0].status).toEqual('COMPLETED');
265265

266-
// During the first invocation the function should timeout so the logs should contain 2 logs
267-
expect(functionLogs[0]).toHaveLength(2);
268-
expect(functionLogs[0][0]).toContain('Task timed out after');
266+
try {
267+
// During the first invocation the handler should be called, so the logs should contain 1 log
268+
expect(functionLogs[0]).toHaveLength(2);
269+
expect(functionLogs[0][0]).toContain('Task timed out after');
270+
} catch {
271+
// 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
272+
expect(functionLogs[0]).toHaveLength(0);
273+
expect(logs[0].getReportLog()).toMatch(/Status: timeout$/);
274+
}
275+
269276
// During the second invocation the handler should be called and complete, so the logs should
270277
// contain 1 log
271278
expect(functionLogs[1]).toHaveLength(1);

Diff for: packages/idempotency/tests/e2e/makeIdempotent.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { createHash } from 'node:crypto';
2-
import { join } from 'node:path';
31
/**
42
* Test makeIdempotent function
53
*
64
* @group e2e/idempotency/makeIdempotent
75
*/
6+
import { createHash } from 'node:crypto';
7+
import { join } from 'node:path';
88
import {
99
TestInvocationLogs,
1010
TestStack,

Diff for: packages/testing/package.json

+15-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
"./types": {
5454
"import": "./lib/esm/types.js",
5555
"require": "./lib/cjs/types.js"
56+
},
57+
"./utils/xray-traces": {
58+
"import": "./lib/esm/xray-traces-utils.js",
59+
"require": "./lib/cjs/xray-traces-utils.js"
5660
}
5761
},
5862
"typesVersions": {
@@ -72,6 +76,10 @@
7276
"context": [
7377
"lib/cjs/context.d.ts",
7478
"lib/esm/context.d.ts"
79+
],
80+
"utils/xray-traces": [
81+
"lib/cjs/xray-traces-utils.d.ts",
82+
"lib/esm/xray-traces-utils.d.ts"
7583
]
7684
}
7785
},
@@ -90,10 +98,14 @@
9098
},
9199
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/testing#readme",
92100
"dependencies": {
93-
"@aws-cdk/cli-lib-alpha": "^2.121.1-alpha.0",
101+
"@aws-cdk/cli-lib-alpha": "^2.155.0-alpha.0",
94102
"@aws-sdk/client-lambda": "^3.637.0",
95103
"@smithy/util-utf8": "^3.0.0",
96104
"aws-cdk-lib": "^2.155.0",
97-
"esbuild": "^0.23.1"
105+
"esbuild": "^0.23.1",
106+
"promise-retry": "^2.0.1"
107+
},
108+
"devDependencies": {
109+
"@types/promise-retry": "^1.1.6"
98110
}
99-
}
111+
}

Diff for: packages/testing/src/TestInvocationLogs.ts

+9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ class TestInvocationLogs {
109109
);
110110
}
111111

112+
/**
113+
* Return the log that contains the report of the function `REPORT RequestId`
114+
*/
115+
public getReportLog(): string {
116+
const endLogIndex = TestInvocationLogs.getReportLogIndex(this.logs);
117+
118+
return this.logs[endLogIndex];
119+
}
120+
112121
/**
113122
* Return the index of the log that contains `REPORT RequestId`
114123
* @param logs

0 commit comments

Comments
 (0)