Skip to content

Commit 0991021

Browse files
authored
chore: standardized env setup for tests (#1255)
1 parent 897cda6 commit 0991021

File tree

11 files changed

+37
-27
lines changed

11 files changed

+37
-27
lines changed

packages/commons/jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
2+
displayName: {
3+
name: 'AWS Lambda Powertools utility: COMMONS',
4+
color: 'red',
5+
},
26
'preset': 'ts-jest',
37
'transform': {
48
'^.+\\.ts?$': 'ts-jest',

packages/idempotency/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
displayName: {
33
name: 'AWS Lambda Powertools utility: IDEMPOTENCY',
4-
color: 'blue',
4+
color: 'yellow',
55
},
66
'runner': 'groups',
77
'preset': 'ts-jest',

packages/idempotency/tests/helpers/populateEnvironmentVariables.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12';
33
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
44
process.env.AWS_EXECUTION_ENV = 'nodejs16.x';
55
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
6-
process.env.AWS_REGION = 'eu-west-1';
6+
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
7+
process.env.AWS_REGION = 'eu-west-1';
8+
}
79
process.env._HANDLER = 'index.handler';

packages/logger/tests/helpers/populateEnvironmentVariables.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
33
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
44
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
5-
process.env.AWS_REGION = 'eu-west-1';
5+
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
6+
process.env.AWS_REGION = 'eu-west-1';
7+
}
68

79
// Powertools variables
810
process.env.LOG_LEVEL = 'DEBUG';

packages/metrics/jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
2+
displayName: {
3+
name: 'AWS Lambda Powertools utility: METRICS',
4+
color: 'green',
5+
},
26
'runner': 'groups',
37
'preset': 'ts-jest',
48
'transform': {
@@ -33,4 +37,7 @@ module.exports = {
3337
'json-summary',
3438
'text',
3539
'lcov' ],
40+
'setupFiles': [
41+
'<rootDir>/tests/helpers/populateEnvironmentVariables.ts'
42+
]
3643
};

packages/metrics/tests/helpers/populate-environment-variables.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Reserved variables
2+
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
3+
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
4+
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
5+
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
6+
process.env.AWS_REGION = 'eu-west-1';
7+
}
8+
9+
// Powertools variables
10+
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface } from '@aws-lambda-powertools/commons';
88
import { Context, Callback } from 'aws-lambda';
99
import { Metrics, MetricUnits } from '../../src/';
10-
import { populateEnvironmentVariables } from '../helpers';
1110

1211
const MAX_METRICS_SIZE = 100;
1312
const MAX_DIMENSION_COUNT = 29;
@@ -20,7 +19,7 @@ interface LooseObject {
2019
}
2120

2221
describe('Class: Metrics', () => {
23-
const originalEnvironmentVariables = process.env;
22+
const ENVIRONMENT_VARIABLES = process.env;
2423
const context = dummyContext.helloworldContext;
2524
const event = dummyEvent.Custom.CustomEvent;
2625

@@ -29,12 +28,7 @@ describe('Class: Metrics', () => {
2928
});
3029

3130
beforeAll(() => {
32-
populateEnvironmentVariables();
33-
});
34-
35-
afterEach(() => {
36-
process.env = originalEnvironmentVariables;
37-
delete process.env.POWERTOOLS_SERVICE_NAME;
31+
process.env = { ...ENVIRONMENT_VARIABLES };
3832
});
3933

4034
describe('Feature: Dimensions logging', () => {

packages/parameters/tests/helpers/populateEnvironmentVariables.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
33
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
44
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
5-
process.env.AWS_REGION = 'eu-west-1';
6-
7-
// Powertools variables
8-
process.env.LOG_LEVEL = 'DEBUG';
9-
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
5+
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
6+
process.env.AWS_REGION = 'eu-west-1';
7+
}

packages/tracer/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
displayName: {
33
name: 'AWS Lambda Powertools utility: TRACER',
4-
color: 'cyan',
4+
color: 'white',
55
},
66
'runner': 'groups',
77
'preset': 'ts-jest',

packages/tracer/tests/helpers/populateEnvironmentVariables.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12';
33
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
44
process.env.AWS_EXECUTION_ENV = 'nodejs16.x';
55
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
6-
process.env.AWS_REGION = 'eu-west-1';
6+
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
7+
process.env.AWS_REGION = 'eu-west-1';
8+
}
79
process.env._HANDLER = 'index.handler';
810

911
// Powertools variables

0 commit comments

Comments
 (0)