Skip to content

Commit b1d8cfd

Browse files
committed
vitest: Fail on console errors and warnings
Some changes introduce warning and error console logs in the test output. This will make the test run fail when there's any "mess" and the problems can be fixed in place instead of retroactively when the test output becomes unreadable. "Maximum update depth exceeded" warning was silences for now and will be revisited later. There's just new act warning and stuff coming in almost every week.
1 parent 917f914 commit b1d8cfd

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

src/test/setup.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ vi.stubGlobal('ResizeObserver', MockResizeObserver);
3030
vi.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({
3131
default: () => ({
3232
auth: {
33-
getUser: async () => ({
34-
identity: {
35-
internal: {
36-
org_id: 5,
37-
},
38-
},
39-
}),
33+
getUser: () =>
34+
new Promise((resolve) => {
35+
setTimeout(() => {
36+
resolve({
37+
identity: {
38+
internal: {
39+
org_id: 5,
40+
},
41+
},
42+
});
43+
}, 0);
44+
}),
4045
},
4146
isBeta: () => true,
4247
isProd: () => true,
@@ -80,6 +85,26 @@ configure({
8085
},
8186
});
8287

83-
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
84-
afterAll(() => server.close());
88+
// Fail tests on console warnings and errors
89+
beforeAll(() => {
90+
server.listen({ onUnhandledRequest: 'error' });
91+
92+
vi.spyOn(console, 'error').mockImplementation((...args) => {
93+
throw new Error(`Console error:\n${args.join(' ')}`);
94+
});
95+
96+
vi.spyOn(console, 'warn').mockImplementation((...args) => {
97+
const message = args.join(' ');
98+
if (message.includes('Maximum update depth exceeded')) {
99+
return;
100+
}
101+
throw new Error(`Console warning:\n${message}`);
102+
});
103+
});
104+
105+
afterAll(() => {
106+
server.close();
107+
vi.restoreAllMocks();
108+
});
109+
85110
afterEach(() => server.resetHandlers());

0 commit comments

Comments
 (0)