Skip to content

Commit 3de19c2

Browse files
authored
Merge pull request #886 from govuk-one-login/bau-vitest-stub-env
BAU: Switch to vitest stubEnv
2 parents d5f62d3 + f54853e commit 3de19c2

3 files changed

Lines changed: 20 additions & 29 deletions

File tree

src/handlers/tests/txma-handler.test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ beforeEach(() => {
4646
});
4747

4848
describe('TxMA Handler', () => {
49-
const OLD_ENV = process.env;
5049
let mockEvent: SQSEvent;
5150
let mockRecord: SQSRecord;
5251
const mockContext = {
@@ -70,12 +69,8 @@ describe('TxMA Handler', () => {
7069
},
7170
};
7271

73-
beforeEach(() => {
74-
process.env = { ...OLD_ENV };
75-
});
76-
7772
afterEach(() => {
78-
process.env = OLD_ENV;
73+
vi.unstubAllEnvs();
7974
vi.clearAllMocks();
8075
});
8176

@@ -88,8 +83,8 @@ describe('TxMA Handler', () => {
8883
mockRecord = createMockRecord(deleteEvent);
8984
mockEvent = { Records: [mockRecord] };
9085

91-
process.env['ACCOUNT_DELETION_SQS_QUEUE'] = 'delete_queue';
92-
process.env['ACCOUNT_INTERVENTION_SQS_QUEUE'] = 'intervention_queue';
86+
vi.stubEnv('ACCOUNT_DELETION_SQS_QUEUE', 'delete_queue');
87+
vi.stubEnv('ACCOUNT_INTERVENTION_SQS_QUEUE', 'intervention_queue');
9388
await handler(mockEvent, mockContext);
9489
expect(mockSendBatchSqsMessage).toHaveBeenCalledWith(
9590
[
@@ -109,8 +104,8 @@ describe('TxMA Handler', () => {
109104
};
110105
mockRecord = createMockRecord(otherInterventionEvent);
111106
mockEvent = { Records: [mockRecord] };
112-
process.env['ACCOUNT_DELETION_SQS_QUEUE'] = 'delete_queue';
113-
process.env['ACCOUNT_INTERVENTION_SQS_QUEUE'] = 'intervention_queue';
107+
vi.stubEnv('ACCOUNT_DELETION_SQS_QUEUE', 'delete_queue');
108+
vi.stubEnv('ACCOUNT_INTERVENTION_SQS_QUEUE', 'intervention_queue');
114109
await handler(mockEvent, mockContext);
115110
expect(mockSendBatchSqsMessage).toHaveBeenCalledWith(
116111
[
@@ -124,7 +119,7 @@ describe('TxMA Handler', () => {
124119
});
125120

126121
it('Sends throw an error if delete queue not configured', async () => {
127-
process.env['ACCOUNT_INTERVENTION_SQS_QUEUE'] = 'intervention_queue';
122+
vi.stubEnv('ACCOUNT_INTERVENTION_SQS_QUEUE', 'intervention_queue');
128123
try {
129124
await handler(mockEvent, mockContext);
130125
} catch (error) {
@@ -134,7 +129,7 @@ describe('TxMA Handler', () => {
134129
});
135130

136131
it('Sends throw an error if intervention queue not configured', async () => {
137-
process.env['ACCOUNT_DELETION_SQS_QUEUE'] = 'queue';
132+
vi.stubEnv('ACCOUNT_DELETION_SQS_QUEUE', 'queue');
138133
try {
139134
await handler(mockEvent, mockContext);
140135
} catch (error) {

src/services/test/app-config-service.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ import { LOGS_PREFIX_INVALID_CONFIG } from '../../data-types/constants';
66
vi.mock('@aws-lambda-powertools/logger');
77

88
describe('AppConfigService', () => {
9-
const OLD_ENV = process.env;
10-
119
beforeEach(() => {
1210
vi.clearAllMocks();
1311
vi.resetModules();
14-
process.env = { ...OLD_ENV };
15-
delete process.env['NODE_ENV'];
1612
});
1713

1814
afterEach(() => {
19-
process.env = OLD_ENV;
15+
vi.unstubAllEnvs();
2016
});
2117

2218
it('should throw an error if the environment variable AWS_REGION is undefined', () => {
23-
process.env['AWS_REGION'] = undefined;
19+
vi.stubEnv('AWS_REGION', '');
2420
const appConfig = AppConfigService.getInstance();
2521
const expectedMessage = 'Invalid configuration - Environment variable AWS_REGION is not defined.';
2622
expect(() => appConfig.awsRegion).toThrow(new InvalidEnvironmentVariableError(expectedMessage));
@@ -31,7 +27,7 @@ describe('AppConfigService', () => {
3127
});
3228

3329
it('should throw an error if the environment variable TABLE_NAME is equal to an empty string', () => {
34-
process.env['TABLE_NAME'] = '';
30+
vi.stubEnv('TABLE_NAME', '');
3531
const appConfig = AppConfigService.getInstance();
3632
const expectedMessage = 'Invalid configuration - Environment variable TABLE_NAME is not defined.';
3733
expect(() => appConfig.tableName).toThrow(new InvalidEnvironmentVariableError(expectedMessage));
@@ -42,7 +38,7 @@ describe('AppConfigService', () => {
4238
});
4339

4440
it('should throw an error if the environment variable DELETED_ACCOUNT_RETENTION_SECONDS is equal to an empty string', () => {
45-
process.env['DELETED_ACCOUNT_RETENTION_SECONDS'] = '';
41+
vi.stubEnv('DELETED_ACCOUNT_RETENTION_SECONDS', '');
4642
const appConfig = AppConfigService.getInstance();
4743
const expectedMessage =
4844
'Invalid configuration - Environment variable DELETED_ACCOUNT_RETENTION_SECONDS is not defined.';
@@ -54,7 +50,7 @@ describe('AppConfigService', () => {
5450
});
5551

5652
it('should throw an error if the environment variable CLOUDWATCH_METRICS_NAMESPACE is equal to an empty string', () => {
57-
process.env['CLOUDWATCH_METRICS_NAMESPACE'] = '';
53+
vi.stubEnv('CLOUDWATCH_METRICS_NAMESPACE', '');
5854
const appConfig = AppConfigService.getInstance();
5955
const expectedMessage = 'Invalid configuration - Environment variable CLOUDWATCH_METRICS_NAMESPACE is not defined.';
6056
expect(() => appConfig.cloudWatchMetricsWorkSpace).toThrow(new InvalidEnvironmentVariableError(expectedMessage));
@@ -65,7 +61,7 @@ describe('AppConfigService', () => {
6561
});
6662

6763
it('should throw an error if the environment variable TXMA_QUEUE_URL is not a url', () => {
68-
process.env['TXMA_QUEUE_URL'] = 'notAURL';
64+
vi.stubEnv('TXMA_QUEUE_URL', 'notAURL');
6965
const appConfig = AppConfigService.getInstance();
7066
const expectedMessage = `${LOGS_PREFIX_INVALID_CONFIG} Environment variable TXMA_QUEUE_URL is not a valid HTTPS URL (notAURL).`;
7167
expect(() => appConfig.txmaEgressQueueUrl).toThrow(new InvalidEnvironmentVariableError(expectedMessage));
@@ -87,7 +83,7 @@ describe('AppConfigService', () => {
8783
});
8884

8985
it('should throw an error if the environmental variable is not a number', () => {
90-
process.env['DELETED_ACCOUNT_RETENTION_SECONDS'] = 'string';
86+
vi.stubEnv('DELETED_ACCOUNT_RETENTION_SECONDS', 'string');
9187
const expectedMessage =
9288
'Invalid configuration - Environment variable DELETED_ACCOUNT_RETENTION_SECONDS is not a number.';
9389
const appConfig = AppConfigService.getInstance();

src/services/test/send-sqs-message.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('sendSqsMessage', () => {
2929
});
3030

3131
it('throws an error if AWS_REGION environment variable is not set', async () => {
32-
delete process.env['AWS_REGION'];
32+
vi.stubEnv('AWS_REGION', '');
3333
await expect(sendSqsMessage(messageBody, queueUrl)).rejects.toThrow('AWS_REGION environment variable not set');
3434
// eslint-disable-next-line @typescript-eslint/unbound-method
3535
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('AWS_REGION environment variable is not set');
3636
});
3737

3838
it('sends a message successfully when AWS_REGION and parameters are set', async () => {
39-
process.env['AWS_REGION'] = 'eu-west-2';
39+
vi.stubEnv('AWS_REGION', 'eu-west-2');
4040

4141
await sendSqsMessage(messageBody, queueUrl);
4242

@@ -50,7 +50,7 @@ describe('sendSqsMessage', () => {
5050
});
5151

5252
it('throws an error if sending the message fails', async () => {
53-
process.env['AWS_REGION'] = 'eu-west-2';
53+
vi.stubEnv('AWS_REGION', 'eu-west-2');
5454
sendFn.mockImplementation(() => {
5555
throw new Error('Failed to send message');
5656
});
@@ -72,14 +72,14 @@ describe('sendBatchSqsMessage', () => {
7272
});
7373

7474
it('throws an error if AWS_REGION environment variable is not set', async () => {
75-
delete process.env['AWS_REGION'];
75+
vi.stubEnv('AWS_REGION', '');
7676
await expect(sendBatchSqsMessage(entries, queueUrl)).rejects.toThrow('AWS_REGION environment variable not set');
7777
// eslint-disable-next-line @typescript-eslint/unbound-method
7878
expect(vi.mocked(logger.error)).toHaveBeenCalledWith('AWS_REGION environment variable is not set');
7979
});
8080

8181
it('sends a message successfully when AWS_REGION and parameters are set', async () => {
82-
process.env['AWS_REGION'] = 'eu-west-2';
82+
vi.stubEnv('AWS_REGION', 'eu-west-2');
8383

8484
await sendBatchSqsMessage(entries, queueUrl);
8585

@@ -93,7 +93,7 @@ describe('sendBatchSqsMessage', () => {
9393
});
9494

9595
it('throws an error if sending the message fails', async () => {
96-
process.env['AWS_REGION'] = 'eu-west-2';
96+
vi.stubEnv('AWS_REGION', 'eu-west-2');
9797
sendFn.mockImplementation(() => {
9898
throw new Error('Failed to send message');
9999
});

0 commit comments

Comments
 (0)