Skip to content

Commit 5f06f21

Browse files
dreamorosisvozza
andauthored
fix(logger): improve e2e test resilience by grouping logs by level (#4909)
Co-authored-by: Stefano Vozza <[email protected]>
1 parent 5590766 commit 5f06f21

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

packages/logger/tests/e2e/advancedUses.test.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,55 @@ describe('Logger E2E - Advanced uses', () => {
109109
}
110110
});
111111

112+
// Parse all logs and group by level for more flexible assertions
113+
const parsedLogs = logs.map((log) =>
114+
TestInvocationLogs.parseFunctionLog(log)
115+
);
116+
const debugLogs = parsedLogs.filter((log) => log.level === 'DEBUG');
117+
const infoLogs = parsedLogs.filter((log) => log.level === 'INFO');
118+
const errorLogs = parsedLogs.filter((log) => log.level === 'ERROR');
119+
120+
const correlationId = i + 1;
121+
112122
if (isFirstInvocation) {
113123
// Logs outside of the function handler are only present on the first invocation
114-
expect(TestInvocationLogs.parseFunctionLog(logs[0])).toEqual(
124+
expect(debugLogs).toContainEqual(
115125
expect.objectContaining({
116126
level: 'DEBUG',
117127
message: 'a never buffered debug log',
118128
})
119129
);
130+
// Should have 2 debug logs on first invocation (never buffered + buffered)
131+
expect(debugLogs).toHaveLength(2);
132+
} else {
133+
// Should have 1 debug log on subsequent invocations (only buffered)
134+
expect(debugLogs).toHaveLength(1);
120135
}
121-
// Since we have an extra log (above) on the first invocation, we need to
122-
// adjust the index of the logs we are checking
123-
const logIndexOffset = isFirstInvocation ? 1 : 0;
124-
const correlationId = i + 1;
125-
expect(
126-
TestInvocationLogs.parseFunctionLog(logs[0 + logIndexOffset])
127-
).toEqual(
136+
137+
// Assert INFO log exists with correct content
138+
expect(infoLogs).toHaveLength(1);
139+
expect(infoLogs[0]).toEqual(
128140
expect.objectContaining({
129141
level: 'INFO',
130142
message: 'an info log',
131143
cold_start: false,
132144
correlation_id: correlationId,
133145
})
134146
);
135-
expect(
136-
TestInvocationLogs.parseFunctionLog(logs[1 + logIndexOffset])
137-
).toEqual(
147+
148+
// Assert buffered DEBUG log exists with correct content
149+
expect(debugLogs).toContainEqual(
138150
expect.objectContaining({
139151
level: 'DEBUG',
140152
message: 'a buffered debug log',
141153
cold_start: false,
142154
correlation_id: correlationId,
143155
})
144156
);
145-
expect(
146-
TestInvocationLogs.parseFunctionLog(logs.at(-1) as string)
147-
).toEqual(
157+
158+
// Assert ERROR log exists with correct content
159+
expect(errorLogs).toHaveLength(1);
160+
expect(errorLogs[0]).toEqual(
148161
expect.objectContaining({
149162
level: 'ERROR',
150163
message: 'Uncaught error detected, flushing log buffer before exit',

0 commit comments

Comments
 (0)