Skip to content

Commit b1a1182

Browse files
committed
test(logger): expect lambda handler to throw in unit tests for clear state functionality
1 parent 03cd539 commit b1a1182

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

Diff for: packages/logger/tests/unit/Logger.test.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ describe('Class: Logger', () => {
982982

983983
});
984984

985-
test('when used as decorator with the clear state flag enabled and the handler throws, the persistent log attributes added in the handler are removed after the handler\'s code is executed', async () => {
985+
test('when used as decorator with the clear state flag enabled and the handler throws an error, the persistent log attributes added in the handler are removed after the handler\'s code is executed', async () => {
986986

987987
// Prepare
988988
const logger = new Logger({
@@ -1014,15 +1014,12 @@ describe('Class: Logger', () => {
10141014

10151015
const persistentAttribs = { ...logger.getPersistentLogAttributes() };
10161016

1017-
// Act
1018-
try {
1017+
// Act & Assess
1018+
const executeLambdaHandler = async (): Promise<void> => {
10191019
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
1020-
} catch (error) {
1021-
// Do nothing
1022-
}
1020+
};
1021+
await expect(executeLambdaHandler()).rejects.toThrow('Unexpected error occurred!');
10231022
const persistentAttribsAfterInvocation = { ...logger.getPersistentLogAttributes() };
1024-
1025-
// Assess
10261023
expect(persistentAttribs).toEqual({
10271024
foo: 'bar',
10281025
biz: 'baz'

Diff for: packages/logger/tests/unit/middleware/middy.test.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Logger } from './../../../src';
1010
import middy from '@middy/core';
1111
import { PowertoolLogFormatter } from '../../../src/formatter';
1212
import { Console } from 'console';
13+
import { context as dummyContext } from '../../../../../tests/resources/contexts/hello-world';
1314

1415
const mockDate = new Date(1466424490000);
1516
const dateSpy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate as unknown as string);
@@ -207,7 +208,7 @@ describe('Middy middleware', () => {
207208

208209
});
209210

210-
test('when enabled and the handler throws, the persistent log attributes added within the handler scope are removed after the invocation ends', async () => {
211+
test('when enabled and the handler throws an error, the persistent log attributes added within the handler scope are removed after the invocation ends', async () => {
211212

212213
// Prepare
213214
const logger = new Logger({
@@ -243,18 +244,15 @@ describe('Middy middleware', () => {
243244
throw new Error('Unexpected error occurred!');
244245
};
245246

246-
const handler = middy(lambdaHandler).use(injectLambdaContext(logger, { clearState: true }));
247247
const persistentAttribs = { ...logger.getPersistentLogAttributes() };
248+
const handler = middy(lambdaHandler).use(injectLambdaContext(logger, { clearState: true }));
248249

249-
// Act
250-
try {
250+
// Act & Assess
251+
const executeLambdaHandler = async (): Promise<void> => {
251252
await handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
252-
} catch (error) {
253-
// Do nothing
254-
}
253+
};
254+
await expect(executeLambdaHandler()).rejects.toThrow('Unexpected error occurred!');
255255
const persistentAttribsAfterInvocation = { ...logger.getPersistentLogAttributes() };
256-
257-
// Assess
258256
expect(persistentAttribs).toEqual({
259257
foo: 'bar',
260258
biz: 'baz'

0 commit comments

Comments
 (0)