Skip to content

Commit 664c4da

Browse files
authored
chore(internal): remove unused test folder from project's root (#1162)
* chore: removed redundant test folder * test: import event & context from commons
1 parent 0a6676a commit 664c4da

File tree

55 files changed

+85
-1615
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+85
-1615
lines changed

packages/commons/src/samples/resources/contexts/hello-world.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const helloworldContext: Context = {
77
memoryLimitInMB: '128',
88
logGroupName: '/aws/lambda/foo-bar-function-123456abcdef',
99
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
10-
invokedFunctionArn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
11-
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
10+
invokedFunctionArn: 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
11+
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
1212
getRemainingTimeInMillis: () => 1234,
1313
done: () => console.log('Done!'),
1414
fail: () => console.log('Failed!'),

packages/logger/tests/unit/Logger.test.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@
44
* @group unit/logger/all
55
*/
66

7-
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
8-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9-
// @ts-ignore
10-
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
7+
import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface } from '@aws-lambda-powertools/commons';
118
import { createLogger, Logger } from '../../src';
129
import { EnvironmentVariablesService } from '../../src/config';
1310
import { PowertoolLogFormatter } from '../../src/formatter';
1411
import { ClassThatLogs, LogJsonIndent } from '../../src/types';
15-
import { Context, Handler } from 'aws-lambda';
12+
import { Context } from 'aws-lambda';
1613
import { Console } from 'console';
1714

18-
interface LambdaInterface {
19-
handler: Handler
20-
}
21-
2215
const mockDate = new Date(1466424490000);
2316
const dateSpy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate as unknown as string);
2417

2518
describe('Class: Logger', () => {
2619
const ENVIRONMENT_VARIABLES = process.env;
27-
20+
const context = dummyContext.helloworldContext;
21+
const event = dummyEvent.Custom.CustomEvent;
22+
2823
beforeEach(() => {
2924
dateSpy.mockClear();
3025
process.env = { ...ENVIRONMENT_VARIABLES };
@@ -244,7 +239,7 @@ describe('Class: Logger', () => {
244239
const logger: Logger & { addContext: (context: Context) => void } = createLogger({
245240
logLevel: 'DEBUG',
246241
});
247-
logger.addContext(dummyContext);
242+
logger.addContext(context);
248243
const consoleSpy = jest.spyOn(logger['console'], methodOfLogger).mockImplementation();
249244

250245
// Act
@@ -829,7 +824,7 @@ describe('Class: Logger', () => {
829824
}
830825

831826
// Act
832-
await new LambdaFunction().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
827+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
833828

834829
// Assess
835830
expect(consoleSpy).toBeCalledTimes(1);
@@ -865,7 +860,7 @@ describe('Class: Logger', () => {
865860

866861
// Act
867862
logger.info('An INFO log without context!');
868-
await new LambdaFunction().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
863+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
869864

870865
// Assess
871866

@@ -912,7 +907,7 @@ describe('Class: Logger', () => {
912907

913908
// Act
914909
logger.info('An INFO log without context!');
915-
const actualResult = await new LambdaFunction().handler(dummyEvent, dummyContext);
910+
const actualResult = await new LambdaFunction().handler(event, context);
916911

917912
// Assess
918913

@@ -971,7 +966,7 @@ describe('Class: Logger', () => {
971966
const persistentAttribs = { ...logger.getPersistentLogAttributes() };
972967

973968
// Act
974-
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
969+
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
975970
const persistentAttribsAfterInvocation = { ...logger.getPersistentLogAttributes() };
976971

977972
// Assess
@@ -1017,7 +1012,7 @@ describe('Class: Logger', () => {
10171012

10181013
// Act & Assess
10191014
const executeLambdaHandler = async (): Promise<void> => {
1020-
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
1015+
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
10211016
};
10221017
await expect(executeLambdaHandler()).rejects.toThrow('Unexpected error occurred!');
10231018
const persistentAttribsAfterInvocation = { ...logger.getPersistentLogAttributes() };
@@ -1050,7 +1045,7 @@ describe('Class: Logger', () => {
10501045
}
10511046

10521047
// Act
1053-
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
1048+
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
10541049

10551050
// Assess
10561051
expect(consoleSpy).toBeCalledTimes(1);
@@ -1094,7 +1089,7 @@ describe('Class: Logger', () => {
10941089
}
10951090

10961091
// Act
1097-
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
1092+
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
10981093

10991094
// Assess
11001095
expect(consoleSpy).toBeCalledTimes(1);
@@ -1148,7 +1143,7 @@ describe('Class: Logger', () => {
11481143
// Act
11491144
const lambda = new LambdaFunction('someValue');
11501145
const handler = lambda.handler.bind(lambda);
1151-
await handler({}, dummyContext, () => console.log('Lambda invoked!'));
1146+
await handler({}, context, () => console.log('Lambda invoked!'));
11521147

11531148
// Assess
11541149
expect(consoleSpy).toBeCalledTimes(1);
@@ -1198,7 +1193,7 @@ describe('Class: Logger', () => {
11981193
// Act
11991194
const lambda = new LambdaFunction();
12001195
const handler = lambda.handler.bind(lambda);
1201-
await handler({}, dummyContext, () => console.log('Lambda invoked!'));
1196+
await handler({}, context, () => console.log('Lambda invoked!'));
12021197

12031198
// Assess
12041199
expect(consoleSpy).toBeCalledTimes(1);
@@ -1381,7 +1376,7 @@ describe('Class: Logger', () => {
13811376
const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation();
13821377

13831378
// Act
1384-
logger.logEventIfEnabled(dummyEvent);
1379+
logger.logEventIfEnabled(event);
13851380

13861381
// Assess
13871382

packages/metrics/tests/unit/Metrics.test.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
* @group unit/metrics/class
55
*/
66

7-
import { ContextExamples as dummyContext, LambdaInterface } from '@aws-lambda-powertools/commons';
7+
import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface } from '@aws-lambda-powertools/commons';
88
import { Context, Callback } from 'aws-lambda';
9-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
10-
// @ts-ignore
11-
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
129
import { Metrics, MetricUnits } from '../../src/';
1310
import { populateEnvironmentVariables } from '../helpers';
1411

@@ -22,14 +19,10 @@ interface LooseObject {
2219
[key: string]: string
2320
}
2421

25-
type DummyEvent = {
26-
key1: string
27-
key2: string
28-
key3: string
29-
};
30-
3122
describe('Class: Metrics', () => {
3223
const originalEnvironmentVariables = process.env;
24+
const context = dummyContext.helloworldContext;
25+
const event = dummyEvent.Custom.CustomEvent;
3326

3427
beforeEach(() => {
3528
consoleSpy.mockClear();
@@ -99,7 +92,7 @@ describe('Class: Metrics', () => {
9992
}
10093
}
10194

102-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
95+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
10396
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
10497

10598
expect(console.log).toBeCalledTimes(2);
@@ -193,7 +186,7 @@ describe('Class: Metrics', () => {
193186
}
194187
}
195188

196-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
189+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
197190
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
198191

199192
expect(console.log).toBeCalledTimes(2);
@@ -227,7 +220,7 @@ describe('Class: Metrics', () => {
227220
}
228221
}
229222

230-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
223+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
231224
} catch (e) {
232225
expect((<Error>e).message).toBe('Max dimension count hit');
233226
}
@@ -255,7 +248,7 @@ describe('Class: Metrics', () => {
255248
}
256249
}
257250

258-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
251+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
259252
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);
260253

261254
expect(console.log).toBeCalledTimes(1);
@@ -283,7 +276,7 @@ describe('Class: Metrics', () => {
283276
}
284277
}
285278

286-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
279+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
287280
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);
288281

289282
expect(console.log).toBeCalledTimes(1);
@@ -297,13 +290,13 @@ describe('Class: Metrics', () => {
297290
test('Cold start metric should only be written out once and flushed automatically', async () => {
298291
const metrics = new Metrics({ namespace: 'test' });
299292

300-
const handler = async (_event: DummyEvent, _context: Context): Promise<void> => {
293+
const handler = async (_event: unknown, _context: Context): Promise<void> => {
301294
// Should generate only one log
302295
metrics.captureColdStartMetric();
303296
};
304297

305-
await handler(dummyEvent, dummyContext.helloworldContext);
306-
await handler(dummyEvent, dummyContext.helloworldContext);
298+
await handler(event, context);
299+
await handler(event, context);
307300
const loggedData = [JSON.parse(consoleSpy.mock.calls[0][0])];
308301

309302
expect(console.log).toBeCalledTimes(1);
@@ -329,8 +322,8 @@ describe('Class: Metrics', () => {
329322
}
330323
}
331324

332-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
333-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked again!'));
325+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
326+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked again!'));
334327
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
335328

336329
expect(console.log).toBeCalledTimes(3);
@@ -356,15 +349,15 @@ describe('Class: Metrics', () => {
356349
metrics.addMetric('test_name', MetricUnits.Seconds, 10);
357350
}
358351
}
359-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
352+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
360353
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);
361354

362355
expect(console.log).toBeCalledTimes(2);
363356
expect(loggedData._aws.CloudWatchMetrics[0].Metrics.length).toBe(1);
364357
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Name).toBe('ColdStart');
365358
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Unit).toBe('Count');
366359
expect(loggedData.service).toBe(serviceName);
367-
expect(loggedData.function_name).toBe(dummyContext.helloworldContext.functionName);
360+
expect(loggedData.function_name).toBe(context.functionName);
368361
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('service');
369362
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('function_name');
370363
expect(loggedData.ColdStart).toBe(1);
@@ -373,8 +366,8 @@ describe('Class: Metrics', () => {
373366
test('Cold should still log, without a function name', async () => {
374367
const serviceName = 'test-service';
375368
const metrics = new Metrics({ namespace: 'test', serviceName: serviceName });
376-
const newDummyContext = JSON.parse(JSON.stringify(dummyContext));
377-
delete newDummyContext.functionName;
369+
const newContext = JSON.parse(JSON.stringify(context));
370+
delete newContext.functionName;
378371
class LambdaFunction implements LambdaInterface {
379372
@metrics.logMetrics({ captureColdStartMetric: true })
380373
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -388,7 +381,7 @@ describe('Class: Metrics', () => {
388381
}
389382
}
390383

391-
await new LambdaFunction().handler(dummyEvent, newDummyContext, () => console.log('Lambda invoked!'));
384+
await new LambdaFunction().handler(event, newContext, () => console.log('Lambda invoked!'));
392385
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);
393386

394387
expect(console.log).toBeCalledTimes(2);
@@ -421,7 +414,7 @@ describe('Class: Metrics', () => {
421414
}
422415

423416
try {
424-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
417+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
425418
} catch (e) {
426419
expect((<Error>e).message).toBe('The number of metrics recorded must be higher than zero');
427420
}
@@ -445,7 +438,7 @@ describe('Class: Metrics', () => {
445438
}
446439

447440
try {
448-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
441+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
449442
} catch (e) {
450443
fail(`Should not throw but got the following Error: ${e}`);
451444
}
@@ -457,14 +450,14 @@ describe('Class: Metrics', () => {
457450
expect.assertions(1);
458451

459452
const metrics = new Metrics({ namespace: 'test' });
460-
const handler = async (_event: DummyEvent, _context: Context): Promise<void> => {
453+
const handler = async (_event: unknown, _context: Context): Promise<void> => {
461454
metrics.throwOnEmptyMetrics();
462455
// Logic goes here
463456
metrics.publishStoredMetrics();
464457
};
465458

466459
try {
467-
await handler(dummyEvent, dummyContext.helloworldContext);
460+
await handler(event, context);
468461
} catch (e) {
469462
expect((<Error>e).message).toBe('The number of metrics recorded must be higher than zero');
470463
}
@@ -490,7 +483,7 @@ describe('Class: Metrics', () => {
490483
}
491484
}
492485

493-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
486+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
494487
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
495488

496489
expect(console.log).toBeCalledTimes(2);
@@ -591,7 +584,7 @@ describe('Class: Metrics', () => {
591584
}
592585
}
593586

594-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
587+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
595588
});
596589

597590
test('Publish Stored Metrics should log and clear', async () => {
@@ -610,7 +603,7 @@ describe('Class: Metrics', () => {
610603
}
611604
}
612605

613-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
606+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
614607
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
615608

616609
expect(console.log).toBeCalledTimes(2);
@@ -640,7 +633,7 @@ describe('Class: Metrics', () => {
640633
}
641634

642635
// Act
643-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext);
636+
await new LambdaFunction().handler(event, context);
644637

645638
// Assess
646639
expect(console.log).toBeCalledTimes(1);
@@ -669,7 +662,7 @@ describe('Class: Metrics', () => {
669662
}
670663
}
671664

672-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext);
665+
await new LambdaFunction().handler(event, context);
673666
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);
674667

675668
expect(console.log).toBeCalledTimes(1);
@@ -695,7 +688,7 @@ describe('Class: Metrics', () => {
695688
}
696689

697690
try {
698-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
691+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
699692
} catch (error) {
700693
// DO NOTHING
701694
}
@@ -724,7 +717,7 @@ describe('Class: Metrics', () => {
724717
metrics.addMetric('test_name', MetricUnits.Seconds, 1);
725718
}
726719
}
727-
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
720+
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
728721
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);
729722

730723
// Assess

0 commit comments

Comments
 (0)