-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathsetupEnv.ts
127 lines (120 loc) · 3.59 KB
/
setupEnv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { toReceiveCommandWith } from 'aws-sdk-client-mock-vitest';
import type { CustomMatcher } from 'aws-sdk-client-mock-vitest';
import { expect, vi } from 'vitest';
expect.extend({ toReceiveCommandWith });
// Mock console methods to prevent output during tests
vi.spyOn(console, 'error').mockReturnValue();
vi.spyOn(console, 'warn').mockReturnValue();
vi.spyOn(console, 'debug').mockReturnValue();
vi.spyOn(console, 'info').mockReturnValue();
vi.spyOn(console, 'log').mockReturnValue();
expect.extend({
toHaveLogged(received, expected) {
const calls = received.mock.calls;
const messages = new Array(calls.length);
for (const [idx, call] of calls.entries()) {
const [rawMessage] = call;
try {
messages[idx] = JSON.parse(rawMessage);
} catch (error) {
messages[idx] = rawMessage;
}
if (this.equals(messages[idx], expected)) {
return {
message: () => '',
pass: true,
};
}
}
return {
message: () => 'Expected function to have logged provided object',
pass: false,
actual: messages,
expected,
};
},
toHaveLoggedNth(received, nth, expected) {
const call = received.mock.calls[nth - 1];
if (!call) {
return {
message: () =>
`Expected function to have logged provided object during ${nth} call`,
pass: false,
actual: 'No log found at index',
expected,
};
}
const [rawMessage] = call;
const message = JSON.parse(rawMessage);
if (this.equals(message, expected)) {
return {
message: () => '',
pass: true,
};
}
return {
message: () => 'Expected function to have logged provided object',
pass: false,
actual: message,
expected,
};
},
});
declare module 'vitest' {
// biome-ignore lint/suspicious/noExplicitAny: vitest typings expect an any type
interface Assertion<T = any> extends CustomMatcher<T> {
/**
* Asserts that the logger function has been called with the expected log message
* during any call.
*
* @example
* ```ts
* vi.spyOn(console, 'info').mockReturnValue();
*
* expect(console.info).toHaveLogged(
* expect.objectContaining({
* message: 'Hello, world!',
* })
* );
* ```
*
* @param expected - The expected log message
*/
toHaveLogged(expected: Record<string, unknown>): void;
/**
* Asserts that the logger function has been called with the expected log message
* during the specific nth call.
*
* @example
* ```ts
* vi.spyOn(console, 'info').mockReturnValue();
*
* expect(console.info).toHaveLoggedNth(
* 1,
* expect.objectContaining({
* message: 'Hello, world!',
* })
* );
* ```
*
* @param nth - The index of the call to check
* @param expected - The expected log message
*/
toHaveLoggedNth(nth: number, expected: Record<string, unknown>): void;
}
interface AsymmetricMatchersContaining extends CustomMatcher {}
}
// Set up environment variables for testing
process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_EXECUTION_ENV = 'nodejs20.x';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
if (
process.env.AWS_REGION === undefined &&
process.env.CDK_DEFAULT_REGION === undefined
) {
process.env.AWS_REGION = 'eu-west-1';
}
process.env._HANDLER = 'index.handler';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
process.env.AWS_XRAY_LOGGING_LEVEL = 'silent';