Skip to content

Commit 132d215

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.
1 parent a7f3494 commit 132d215

6 files changed

Lines changed: 57 additions & 34 deletions

File tree

src/Components/CreateImageWizard/steps/Registration/components/ManualActivationKey.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import React, { MutableRefObject, useEffect, useRef } from 'react';
1+
import React, { useEffect } from 'react';
22

33
import {
4+
Button,
45
Content,
56
ContentVariants,
67
FormGroup,
7-
FormGroupLabelHelp,
88
Popover,
99
} from '@patternfly/react-core';
10+
import { HelpIcon } from '@patternfly/react-icons';
1011

1112
import { ValidatedInput } from '@/Components/CreateImageWizard/ValidatedInput';
1213
import { CDN_PROD_URL } from '@/constants';
@@ -20,14 +21,9 @@ import {
2021
selectOrgId,
2122
} from '@/store/slices/wizard';
2223

23-
const ManualRegistrationPopover = ({
24-
ref,
25-
}: {
26-
ref: MutableRefObject<null>;
27-
}) => {
24+
const ManualRegistrationPopover = () => {
2825
return (
2926
<Popover
30-
triggerRef={ref}
3127
headerContent='About Activation Keys & Organization ID'
3228
position='right'
3329
minWidth='30rem'
@@ -46,9 +42,12 @@ const ManualRegistrationPopover = ({
4642
</Content>
4743
}
4844
>
49-
<FormGroupLabelHelp
50-
ref={ref}
51-
aria-label='About Activation Keys & Organization ID'
45+
<Button
46+
icon={<HelpIcon />}
47+
variant='plain'
48+
size='sm'
49+
aria-label='Credentials path info'
50+
hasNoPadding
5251
/>
5352
</Popover>
5453
);
@@ -58,8 +57,6 @@ export const ManualActivationKey = () => {
5857
const dispatch = useAppDispatch();
5958
const orgId = useAppSelector(selectOrgId);
6059
const activationKey = useAppSelector(selectActivationKey);
61-
const orgIdRef = useRef(null);
62-
const activationKeyRef = useRef(null);
6360

6461
useEffect(() => {
6562
dispatch(changeServerUrl('subscription.rhsm.redhat.com'));
@@ -71,7 +68,7 @@ export const ManualActivationKey = () => {
7168
<FormGroup
7269
className='pf-v6-u-mb-md'
7370
label={'Activation key'}
74-
labelHelp={<ManualRegistrationPopover ref={activationKeyRef} />}
71+
labelHelp={<ManualRegistrationPopover />}
7572
isRequired
7673
>
7774
<ValidatedInput
@@ -88,7 +85,7 @@ export const ManualActivationKey = () => {
8885
</FormGroup>
8986
<FormGroup
9087
label={'Organization ID'}
91-
labelHelp={<ManualRegistrationPopover ref={orgIdRef} />}
88+
labelHelp={<ManualRegistrationPopover />}
9289
isRequired
9390
>
9491
<ValidatedInput

src/Components/CreateImageWizard/steps/Repositories/components/Repositories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ const Repositories = () => {
364364
),
365365
);
366366
}
367-
}, [templateUuid, reposInTemplate]);
367+
}, [dispatch, isTemplateSelected, reposInTemplate]);
368368

369369
if (
370370
isError ||

src/Components/CreateImageWizard/steps/Repositories/tests/Repositories.test.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { screen, waitFor } from '@testing-library/react';
33
import { mapRequestFromState } from '@/Components/CreateImageWizard/utilities/requestMapper';
44
import { CreateBlueprintRequest } from '@/store/api/backend';
55
import { server } from '@/test/mocks/server';
6-
import { createTestStore, createUser, typeWithWait } from '@/test/testUtils';
6+
import {
7+
clickWithWait,
8+
createTestStore,
9+
createUser,
10+
typeWithWait,
11+
} from '@/test/testUtils';
712

813
import {
914
removeRepo,
@@ -101,7 +106,7 @@ describe('Repositories Component', () => {
101106
const toggle = await screen.findByRole('button', {
102107
name: /menu toggle/i,
103108
});
104-
await user.click(toggle);
109+
await clickWithWait(user, toggle);
105110

106111
// Should show repositories without typing
107112
expect(
@@ -123,7 +128,7 @@ describe('Repositories Component', () => {
123128
const toggle = await screen.findByRole('button', {
124129
name: /menu toggle/i,
125130
});
126-
await user.click(toggle);
131+
await clickWithWait(user, toggle);
127132

128133
expect(
129134
await screen.findByRole('option', {
@@ -135,7 +140,7 @@ describe('Repositories Component', () => {
135140
const searchInput = await screen.findByRole('textbox', {
136141
name: /filter repositories/i,
137142
});
138-
await user.type(searchInput, 'nonexistent');
143+
await typeWithWait(user, searchInput, 'nonexistent');
139144

140145
expect(
141146
await screen.findByRole('option', {
@@ -166,15 +171,15 @@ describe('Repositories Component', () => {
166171
const toggle = await screen.findByRole('button', {
167172
name: /menu toggle/i,
168173
});
169-
await user.click(toggle);
174+
await clickWithWait(user, toggle);
170175

171176
await screen.findByRole('option', { name: /no repositories available/i });
172177

173178
// Type to search - should use limit=50
174179
const searchInput = await screen.findByRole('textbox', {
175180
name: /filter repositories/i,
176181
});
177-
await user.type(searchInput, 'test');
182+
await typeWithWait(user, searchInput, 'test');
178183

179184
await screen.findByRole('option', {
180185
name: /no repositories found for "test"/i,
@@ -295,14 +300,16 @@ describe('Repositories Component', () => {
295300
await selectRepo(user, '01-test-valid-repo');
296301
await selectRepo(user, '04-test-another-valid-repo');
297302

298-
expect(
299-
await screen.findByRole('cell', { name: /01-test-valid-repo/i }),
300-
).toBeInTheDocument();
301-
expect(
302-
await screen.findByRole('cell', {
303-
name: /04-test-another-valid-repo/i,
304-
}),
305-
).toBeInTheDocument();
303+
await waitFor(() => {
304+
expect(
305+
screen.getByRole('cell', { name: /01-test-valid-repo/i }),
306+
).toBeInTheDocument();
307+
expect(
308+
screen.getByRole('cell', {
309+
name: /04-test-another-valid-repo/i,
310+
}),
311+
).toBeInTheDocument();
312+
});
306313
});
307314
});
308315

src/test/Components/ImagesTable/ImagesTable.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Images Table', () => {
1010
});
1111

1212
const user = userEvent.setup();
13-
test('render ImagesTable', async () => {
13+
test.skipIf(process.env.IS_ON_PREMISE)('render ImagesTable', async () => {
1414
renderCustomRoutesWithReduxRouter();
1515

1616
const table = await screen.findByTestId('images-table');

src/test/fixtures/repositories.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { v4 as uuidv4 } from 'uuid';
2+
13
import {
24
ApiLinks,
35
ApiRepositoryResponse,
@@ -609,8 +611,9 @@ const generateLinks = (
609611

610612
const generateFillerRepos = (num: number): ApiRepositoryResponse[] => {
611613
const repos = new Array(num).fill(undefined).map((_, i) => {
614+
const uuid = uuidv4();
612615
return {
613-
uuid: '9cf1d45d-aa06-46fe-87ea-121845cc6bbb',
616+
uuid,
614617
name: `z-filler repo ${i}`,
615618
url: `http://fillerRepos.org/9/x86_64/packages/${i}`,
616619
distribution_versions: ['any'],

src/test/setup.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ configure({
8080
},
8181
});
8282

83-
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
84-
afterAll(() => server.close());
83+
// Fail tests on console warnings and errors
84+
beforeAll(() => {
85+
server.listen({ onUnhandledRequest: 'error' });
86+
87+
vi.spyOn(console, 'error').mockImplementation((...args) => {
88+
throw new Error(`Console error:\n${args.join(' ')}`);
89+
});
90+
91+
vi.spyOn(console, 'warn').mockImplementation((...args) => {
92+
throw new Error(`Console warning:\n${args.join(' ')}`);
93+
});
94+
});
95+
96+
afterAll(() => {
97+
server.close();
98+
vi.restoreAllMocks();
99+
});
100+
85101
afterEach(() => server.resetHandlers());

0 commit comments

Comments
 (0)