| name | gen-rtl-test |
| description | Generate React Testing Library tests following OCP Console best practices |
| argument-hint | [path/to/Component.tsx] or use @file for autocomplete |
OCP Console React Component Unit Testing Best Practices
Usage:
/gen-rtl-test - Default: Automatically checks git diff for component changes and generates tests
/gen-rtl-test path/to/Component.tsx - Generate tests for a specific component
/gen-rtl-test @Component.tsx - Use @ for file autocomplete, then select the file
Smart Component Detection Workflow
When invoked without arguments, the slash command follows this intelligent workflow:
- Check git diff: Automatically run
git diff --name-only to find modified files
- Filter for components: Identify
.tsx and .jsx component files (exclude test files, type files, utils)
- Validate components: Ensure files contain React components (not just types or utilities)
- Present options: Show user the detected components and ask which to generate tests for
- Fallback: If no valid components found, prompt user for component path
This workflow ensures you automatically generate tests for components you're actively working on.
You are helping generate comprehensive React Testing Library (RTL) test cases following the established OCP Console unit testing standards.
Before writing imports: Inspect the component under test (and its hooks). Use renderWithProviders only if it depends on the Redux store and/or React Router. Otherwise use render from @testing-library/react. (See Rule 0 for the full table, including PluginStore when relevant.)
Introduction & Objectives
This guide establishes a consistent, project-wide standard for all React component tests in the OCP Console.
Core Philosophy: Test component behavior from a user's perspective, not internal implementation details.
Objectives
- Establish consistent project-wide testing standards
- Promote user-centric testing that focuses on behavior over implementation
- Provide practical, rules-based guidance for common scenarios
- Improve test quality, resilience, and maintainability
Rule 0: Use renderWithProviders Only When the Component Needs Redux and/or Router
Pick the render helper from what the component under test actually uses:
| Use | When the component (or non-mocked hooks it calls) … |
|---|
renderWithProviders from @console/shared/src/test-utils/unit-test-utils | Needs the Redux store (e.g. useSelector, useDispatch, k8s/resource hooks backed by the console store) and/or React Router (e.g. useNavigate, useParams, useLocation, Link, NavLink). The helper also wraps PluginStore — use it when the tree touches dynamic plugin APIs that expect that context. |
render from @testing-library/react | Has no Redux or Router dependency (pure presentational UI, local useState only, or all store/router hooks are mocked so the real provider is unnecessary). |
import { render, screen } from '@testing-library/react';
import { BadgeLabel } from './BadgeLabel';
it('renders the label', () => {
render(<BadgeLabel text="Ready" />);
expect(screen.getByText('Ready')).toBeVisible();
});
import { screen } from '@testing-library/react';
import { renderWithProviders } from '@console/shared/src/test-utils/unit-test-utils';
import { DeploymentListRow } from './DeploymentListRow';
it('shows the deployment name', () => {
renderWithProviders(<DeploymentListRow deployment={mockDeployment} />);
expect(screen.getByRole('cell', { name: /nginx/i })).toBeVisible();
});
Why renderWithProviders exists: it supplies Redux Provider, MemoryRouter, and PluginStore so typical console components do not throw when mounting.
Optional clarity for reviewers: If the file uses render (not renderWithProviders), a one-line comment at the top of the file or above the first test can help reviewers, e.g. // Unit tests: component has no Redux or Router dependencies.
Do not use renderWithProviders “by default” for every console file — that hides missing providers in tests that should be asserting integration with real store/router behavior, and it adds cost where render is enough.
Rule 0.1: Use userEvent (Not fireEvent)
ALWAYS use userEvent from @testing-library/user-event for user interactions.
import { fireEvent } from '@testing-library/react';
fireEvent.click(button);
fireEvent.change(input, { target: { value: 'test' } });
import userEvent from '@testing-library/user-event';
const user = userEvent.setup();
await user.click(button);
await user.type(input, 'test');
Why:
userEvent simulates real user behavior (focus, blur, keyboard events)
fireEvent dispatches raw DOM events (not realistic)
userEvent catches more bugs related to event handling
- Better async handling with
await
Rule 0.2: Use screen Queries (Not Destructured)
ALWAYS use screen from RTL instead of destructuring queries from render.
const { getByRole, getByText } = render(<MyComponent />);
const button = getByRole('button');
import { render, screen } from '@testing-library/react';
render(<MyComponent />);
const button = screen.getByRole('button');
Why:
- Consistent query access across all tests
- Better debugging with
screen.debug()
- Cleaner test code
- ESLint rule
testing-library/prefer-screen-queries enforces this
Section 1: React Testing Library Overview
The RTL Approach
RTL emphasizes testing components as users interact with them. Users find buttons by visible text (e.g., "Submit"), not by CSS classes, IDs, or test IDs. Therefore, test selectors should prioritize what users see and interact with.
Core Principles
-
User-Centric Testing - Test what users see and interact with. DO NOT test:
- Internal component state
- Private component methods
- Props passed to child components
- CSS class names or styles
- Component structure (e.g.,
expect(container.firstChild).toBe...)
-
Accessibility-First - Queries match how screen readers and users interact with the UI
-
Semantic Over Generic - Always prefer role-based queries (e.g., getByRole) over generic selectors
-
DRY Helpers - Use reusable function in frontend/packages/console-shared/src/test-utils directoty and sub-directory if exists else extract repetitive setup into reusable functions
-
Async-Aware - Handle asynchronous updates with findBy* and waitFor
-
TypeScript Safety - Use proper types for props, state, and mock data
-
Arrange-Act-Assert (AAA) Pattern - Structure tests logically:
- Arrange: Render component with mocks
- Act: Perform user actions
- Assert: Verify expected state
Section 2: Console RTL Rules
⚠️ CRITICAL RULE - READ FIRST
ALWAYS Use ES6 Imports - NEVER Use require()
This is the #1 most critical rule for test generation.
🚫 ZERO TOLERANCE: NO require() ANYWHERE
NEVER use require() in test files. NO EXCEPTIONS.
❌ FORBIDDEN - In test bodies:
it('should work', () => {
const { k8sCreate } = require('@console/internal/module/k8s');
});
❌ FORBIDDEN - In mock factories:
jest.mock('../Component', () => {
const React = require('react');
return () => React.createElement('div', null, 'Mock');
});
✅ REQUIRED - ES6 imports only:
import { k8sCreate } from '@console/internal/module/k8s';
jest.mock('../Component', () => () => null);
jest.mock('../LoadingSpinner', () => () => 'Loading...');
it('should work', () => {
(k8sCreate as jest.Mock).mockResolvedValue({});
});
Why ZERO tolerance:
require() breaks Jest's mock hoisting mechanism
- Causes test isolation failures and flaky tests
- Violates OCP Console testing standards
- NO exceptions - even in mock factories
Rule 1: Test File Co-location and Naming Convention
File Structure:
MyComponentDirectory/
├── __tests__/
│ └── MyComponent.spec.tsx
└── MyComponent.tsx
- Test file must be in
__tests__/ directory within component directory
- Test file must have same name as implementation file
- Use
.spec.tsx extension
Rule 2: Mocking Strategies
Check for Global Mocks First
Before manually mocking, check __mocks__/ directory for existing global mocks (e.g., react-i18next, localStorage, k8sResourcesMocks). These are applied automatically.
Keep Component Mocks Simple (No JSX)
Mock functions must NOT return JSX to avoid Jest hoisting errors:
jest.mock('../MyComponent', () => () => null);
jest.mock('../LoadingSpinner', () => () => 'Loading...');
jest.mock('../utils/firehose', () => ({
Firehose: (props) => props.children,
}));
jest.mock('../utils/firehose', () => ({
Firehose: jest.fn((props) => props.children),
}));
jest.mock('../MyComponent', () => () => <div>My Mock</div>);
Mock Custom Hooks with jest.fn()
jest.mock('../useCustomHook', () => ({
useCustomHook: jest.fn(() => []),
}));
Use Static Partial Mocking for Module-Wide Control
jest.mock('@console/internal/module/k8s', () => ({
...jest.requireActual('@console/internal/module/k8s'),
k8sCreate: jest.fn(),
k8sPatch: jest.fn(),
}));
Use jest.spyOn for Granular, Test-Level Control (Preferred)
import * as k8sModule from '@console/internal/module/k8s';
it('should do something when k8sGet succeeds', () => {
jest.spyOn(k8sModule, 'k8sGet').mockResolvedValue(data);
});
Controlling Redux State
DO NOT mock the useReduxStore hook. Instead, pass initialState to renderWithProviders:
import { renderWithProviders } from '@console/shared/src/test-utils/unit-test-utils';
it('should render with mock Redux data', () => {
const mockK8sState = { };
renderWithProviders(
<MyComponent />,
{
initialState: {
k8s: mockK8sState
}
}
);
expect(screen.getByText('My Mock Data')).toBeVisible();
});
⚠️ CRITICAL: Always Use ES6 Import (Never require())
STRICTLY ENFORCED - ZERO EXCEPTIONS
🚫 NO require() ANYWHERE IN TEST FILES
Always use ES6 import/export syntax in test files. NEVER use require() - not in test bodies, not in mock factories, NOWHERE.
✅ CORRECT - ES6 Imports:
import { k8sCreate } from '@console/internal/module/k8s';
import { history } from '@console/internal/components/utils';
import * as pdbModels from '../pdb-models';
jest.mock('../Component', () => () => null);
jest.mock('../ButtonBar', () => ({ children }) => children);
it('should create resource', async () => {
(k8sCreate as jest.Mock).mockResolvedValue({});
jest.spyOn(history, 'push');
jest.spyOn(pdbModels, 'patchPDB').mockResolvedValue({});
});
❌ INCORRECT - require() ANYWHERE:
it('should create resource', async () => {
const { k8sCreate } = require('@console/internal/module/k8s');
});
jest.mock('../Component', () => {
const React = require('react');
return () => React.createElement('div', null, 'Mock');
});
beforeEach(() => {
const utils = require('../utils');
});
How to avoid require() in mocks:
jest.mock('../Component', () => () => null);
jest.mock('../LoadingSpinner', () => () => 'Loading...');
jest.mock('../Wrapper', () => ({ children }) => children);
jest.mock('../ButtonBar', () => jest.fn(({ children }) => children));
Enforcement Checklist:
Rule 3: Use a Clear and Focused Test Structure
import { render, screen } from '@testing-library/react';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
describe('when loading', () => {
it('should show the loading spinner', () => {
jest.spyOn(myHooksModule, 'useCustomHook').mockReturnValue({ isLoading: true });
render(<MyComponent />);
expect(screen.getByRole('progressbar')).toBeVisible();
});
it('should not show the data grid', () => {
jest.spyOn(myHooksModule, 'useCustomHook').mockReturnValue({ isLoading: true });
render(<MyComponent />);
expect(screen.queryByRole('grid')).not.toBeInTheDocument();
});
});
describe(, {
(, {
jest.(myHooksModule, ).({ : , : [...] });
();
(screen.()).();
});
});
});
Requirements:
- All tests wrapped in top-level
describe block named after component
- Use nested
describe blocks for related tests
- Use
it() method (not test())
- Each
it block tests only a single state or interaction
Rule 4: Use the Correct Render Function
Same decision as Rule 0:
render from '@testing-library/react' — when the component under test has no Redux or React Router dependency (see Rule 0 table).
renderWithProviders from '@console/shared/src/test-utils/unit-test-utils' — when it needs Redux and/or React Router (and use it when plugin context is required; see Rule 0).
Rule 5: Always Use screen for Queries
import { render, screen } from '@testing-library/react';
it('should find the heading', () => {
render(<MyComponent />);
const heading = screen.getByRole('heading', { name: /welcome/i });
expect(heading).toBeVisible();
});
it('should find the heading', () => {
const { getByRole } = render(<MyComponent />);
const heading = getByRole('heading', { name: /welcome/i });
expect(heading).toBeVisible();
});
Exception: Use within() for scoped queries or when you need container for specific assertions.
Rule 6: Prioritize Accessible Queries
Query Priority (most to least preferred):
getByRole
getByLabelText
getByPlaceholderText
getByText
getByDisplayValue
getByAltText
getByTitle
getByTestId (last resort only). This might involve adding a data-test attribute to the implementation component element.
Query Variants:
getBy* - Element expected to be present synchronously (throws if not found)
queryBy* - Only for asserting element is NOT present
findBy* - Element will appear asynchronously (returns Promise)
Anti-pattern: Avoid container.querySelector - it tests implementation details.
Helpful Tip: For iframe or markdown content, use screen.getByRole('document').
Rule 7: Text Matching Strategy
- Exact text match - Preferred when text is in a single node
- Regex without
i flag - When text spans multiple wrapper nodes (avoid case-insensitive matching)
Note: Avoid case-insensitive matching based on Console UX text casing convention.
Rule 8: Assertion Guidelines
expect(screen.getByRole('button', { name: 'Submit' })).toBeVisible();
const button = screen.getByRole('button');
expect(button).toBeInTheDocument();
expect(screen.getByText('Submit')).toBeInTheDocument();
When to use:
toBeVisible() - For elements users are expected to see or interact with
toBeInTheDocument() - For structural elements or conditional rendering verification
Anti-pattern: Avoid weak assertions like .toBeTruthy() or .toBeInTheDocument() for visible elements.
Rule 9: Use Shared verifyInputField Utility - MANDATORY for Form Fields
CRITICAL: When testing form input fields, ALWAYS use verifyInputField utility. This is strictly enforced.
When to Use verifyInputField
Use verifyInputField when your test needs to verify:
- ✅ Input field label exists and is associated with the input
- ✅ Input element renders correctly
- ✅ Initial/default value of the input
- ✅ Input can accept user input (onChange behavior)
- ✅ Help text appears below the field
- ✅ Required field indicator (
*) is shown
- ✅ Field ID and accessibility attributes
DO NOT manually write separate assertions for each of these - use the utility instead.
Usage Examples
import { verifyInputField } from '@console/shared/src/test-utils/unit-test-utils';
it('should render the Name field with label, input, and help text', async () => {
render(<MyFormComponent />);
await verifyInputField({
inputLabel: 'Name',
containerId: 'test-name-form',
initialValue: 'test',
testValue: 'test',
helpText: 'Unique name for the resource',
isRequired: true,
});
});
it('should render all form fields correctly', async () => {
render(<MyFormComponent />);
await verifyInputField({
inputLabel: 'Name',
containerId: 'test-name-form',
initialValue: '',
testValue: 'my-resource',
isRequired: true,
});
await verifyInputField({
inputLabel: 'Description',
: ,
: ,
: ,
: ,
: ,
});
});
(, () => {
();
(screen.()).();
(screen.()).();
fireEvent.(screen.(), { : { : } });
(screen.()).();
(screen.()).();
});
When NOT to Use verifyInputField
- ❌ Non-input form controls (Select, Dropdown, Checkbox, Radio)
- ❌ Buttons or action elements
- ❌ Read-only text displays
- ❌ Custom form components that aren't text inputs
For these cases, use standard RTL queries.
Enforcement Checklist
When testing form components:
Rule 10: Test Conditional Rendering by Asserting Both States
import userEvent from '@testing-library/user-event';
it('should show content when expanded', async () => {
render(<Collapsible />);
const user = userEvent.setup();
expect(screen.queryByText('Hidden content')).not.toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Expand' }));
expect(screen.getByText('Hidden content')).toBeVisible();
});
Rule 11: Handle Asynchronous Behavior
const element = await screen.findByText('Loaded content');
expect(element).toBeVisible();
await waitFor(() => {
expect(screen.getByText('Updated')).toBeInTheDocument();
});
Avoid Explicit act(): Rarely needed. render, userEvent, findBy*, and waitFor already wrap operations in act().
Rule 12: Use Lifecycle Hooks for Setup and Cleanup
describe('MyComponent', () => {
beforeEach(() => {
jest.spyOn(myHooksModule, 'useCustomHook').mockReturnValue({ isLoading: false });
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should render the default state', () => {
render(<MyComponent />);
});
it('should render a different state', () => {
jest.spyOn(myHooksModule, 'useCustomHook').mockReturnValue({ isLoading: true });
render(<MyComponent />);
});
});
Rule 13: Scope Queries with within()
import { render, screen, within } from '@testing-library/react';
render(<MyDashboard />);
const userProfileCard = screen.getByTestId('profile-card');
const userName = within(userProfileCard).getByText(/john doe/i);
const editButton = within(userProfileCard).getByRole('button', { name: /edit/i });
expect(userName).toBeVisible();
expect(editButton).toBeVisible();
Rule 14: Simulate User Events with userEvent
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWithProviders } from '@console/shared/src/test-utils/unit-test-utils';
renderWithProviders(<MyForm />);
const user = userEvent.setup();
const input = screen.getByLabelText(/name/i);
const button = screen.getByRole('button', { name: /submit/i });
await user.type(input, 'John Doe');
await user.click(button);
Why userEvent over fireEvent:
userEvent simulates real user behavior (focus, blur, keyboard events)
fireEvent dispatches raw DOM events (not realistic)
userEvent catches more bugs related to event handling
- Better async handling with
await
Rule 15: Test "Unhappy Paths" and Error States
it('should display an error message when the API call fails', async () => {
jest.spyOn(k8sModule, 'k8sGet').mockRejectedValue(new Error('API Error'));
render(<MyComponent />);
const errorMessage = await screen.findByText(/Could not load data/i);
expect(errorMessage).toBeVisible();
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
});
Rule 16: Use screen.debug() for Help
it('should find the element', () => {
render(<MyComponent />);
const button = screen.getByRole('button', { name: /submit/i });
expect(button).toBeVisible();
});
Rule 17: Write Descriptive Test Titles
Format: it('should [expected result] when [condition]')
it('should display an error when the API call fails')
it('works')
it('renders')
Rule 18: Avoid Snapshot Tests
DO NOT use toMatchSnapshot(), toMatchInlineSnapshot(), or error snapshot matchers. Snapshot tests are brittle, give false security, and test implementation details. Prefer toStrictEqual, toMatchObject, or RTL queries on user-visible output.
Enforcement: jest/no-restricted-matchers from eslint-plugin-console errors on these matchers for paths matched by plugin:console/testing-library-tests (the same **/*spec* / **/__tests__** globs used for RTL lint).
Rule 19: Render in Each Test by Default
Default: Call render() inside each it block for test isolation.
May use beforeEach only if ALL tests in the block:
- Are simple, synchronous tests
- Use the exact same props and initial state
- Only test different aspects of a single, unchanged render
Rule 20: Use Centralized Test Data
Store mock data in centralized files (e.g., __mocks__/k8sResourcesMocks.ts). This:
- Mirrors production data structures
- Makes tests more representative
- Easier to maintain
- Catches type-related errors early
Rule 21: Clean Up Unused Imports, Code, and Redundant Mocks
MANDATORY: After generating tests, perform cleanup to ensure code quality and maintainability.
Clean Up Unused Imports
Remove any imports that are not used in the test file:
import { render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { k8sCreate, k8sPatch, k8sUpdate } from '@console/internal/module/k8s';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { k8sCreate } from '@console/internal/module/k8s';
Remove Redundant Mocks
Only mock what's actually used in tests:
jest.mock('../ComponentA', () => () => null);
jest.mock('../ComponentB', () => () => null);
jest.mock('../ComponentC', () => () => null);
jest.mock('../ComponentA', () => () => null);
Remove Duplicate or Redundant Tests
Avoid testing the same behavior multiple times:
it('should render the button', () => {
render(<MyComponent />);
expect(screen.getByRole('button')).toBeInTheDocument();
});
it('should display the button', () => {
render(<MyComponent />);
expect(screen.getByRole('button')).toBeVisible();
});
it('should render the button', () => {
render(<MyComponent />);
expect(screen.getByRole('button', { name: 'Submit' })).toBeVisible();
});
Remove Commented Code
Delete commented-out code, debugging statements, and console.logs:
it('should work', () => {
render(<MyComponent />);
expect(screen.getByRole('button')).toBeVisible();
});
it('should work', () => {
render(<MyComponent />);
expect(screen.getByRole('button')).toBeVisible();
});
Remove Unused Variables and Constants
Clean up any variables that are declared but never used:
import userEvent from '@testing-library/user-event';
it('should submit form', async () => {
const mockData = { foo: 'bar' };
const unusedSpy = jest.spyOn(console, 'log');
const onSubmit = jest.fn();
const user = userEvent.setup();
render(<Form onSubmit={onSubmit} />);
await user.click(screen.getByRole('button'));
expect(onSubmit).toHaveBeenCalled();
});
it('should submit form', async () => {
const onSubmit = jest.fn();
const user = userEvent.setup();
render(<Form onSubmit={onSubmit} />);
await user.click(screen.getByRole('button'));
expect(onSubmit).();
});
Remove Unnecessary Mock Static Methods
Only add static methods to mocks if they're actually called:
jest.mock('../SelectorInput', () => Object.assign(
jest.fn(() => null),
{
objectify: jest.fn(),
arrayify: jest.fn(),
someMethodNeverUsed: jest.fn(),
anotherUnusedMethod: jest.fn(),
}
));
jest.mock('../SelectorInput', () => Object.assign(
jest.fn(() => null),
{
objectify: jest.fn(),
arrayify: jest.fn(),
}
));
Cleanup Checklist:
Rule 22: Generate Between 5-10 Tests Per Component
IMPORTANT: Generate between 5 and 10 focused, high-value tests per component.
Why 5-10 Tests?
- Minimum 5: Ensures adequate coverage of critical functionality
- Maximum 10: Prevents over-testing and maintains quality focus
- Quality over Quantity - Forces focus on most important behaviors
- Maintainability - Easier to read, understand, and maintain
- Faster Test Runs - Reduced execution time
- Better Code Reviews - Reviewers can thoroughly examine each test
- Reduced Redundancy - Prevents testing the same thing multiple ways
How to Choose 5-10 Tests
Priority Order:
-
Critical User Flows (2-3 tests)
- Primary user actions (e.g., form submission, data creation)
- Most important happy path scenarios
-
Error States (2-3 tests)
- API failures, validation errors
- Edge cases that break functionality
- "Unhappy paths" users might encounter
-
Conditional Rendering (2-3 tests)
- Different states/modes of the component
- Loading states, empty states
- Permission-based rendering
-
User Interactions (1-2 tests)
- Click handlers, input changes
- Form validation
- Navigation/routing
-
Accessibility (1 test)
- Key accessible queries work
- ARIA attributes present
- Keyboard navigation (if complex)
What NOT to Test (when limiting to 5-10):
❌ Multiple variations of the same behavior
❌ Testing every prop combination
❌ Minor UI variations (button text, colors)
❌ Component existence tests
❌ Trivial rendering checks
Examples
❌ BAD - Too Few Tests (3 tests):
describe('MyForm', () => {
it('should render the form');
it('should submit when valid');
it('should show error when invalid');
});
❌ BAD - Too Many Tests (15 tests):
describe('MyForm', () => {
it('should render the form');
it('should render the name input');
it('should render the email input');
it('should render the phone input');
it('should render the submit button');
it('should render the cancel button');
it('should enable submit when name is filled');
it('should enable submit when email is filled');
it('should enable submit when all fields filled');
it('should disable submit when name is empty');
it('should disable submit when email is empty');
it('should show error for invalid email');
it('should show error for invalid phone');
it('should submit when form is valid');
it('should call onCancel when cancel clicked');
});
✅ GOOD - Focused 8 Tests (within 5-10 range):
describe('MyForm', () => {
it('should render all form fields and buttons');
it('should submit form with valid data');
it('should show validation errors for invalid email');
it('should display error message when submission fails');
it('should disable submit button when required fields empty');
it('should show loading state during submission');
it('should populate form fields when editing existing data');
it('should have accessible form labels and buttons');
});
✅ ALSO GOOD - Minimal 5 Tests (for simple components):
describe('SimpleButton', () => {
it('should render button with correct label');
it('should call onClick when clicked');
it('should be disabled when disabled prop is true');
it('should show loading spinner when loading');
it('should have accessible button role and label');
});
When a Component Needs More Than 10 Tests
If a component is complex enough to need more than 10 tests, it's a sign the component should be split:
describe('ComplexDashboard', () => {
});
describe('DashboardHeader', () => {
});
describe('DashboardFilters', () => {
});
describe('DashboardDataGrid', () => {
});
describe('DashboardActions', () => {
});
5-10 Tests Rule Enforcement:
Rule 23: Zero act() Warnings - Strictly Enforced
CRITICAL: All tests MUST have ZERO act() warnings. This rule is strictly enforced.
What is an act() Warning?
Warning: An update to ComponentName inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
How to Fix act() Warnings
Strategy 1: Use userEvent with async/await
const user = userEvent.setup();
user.click(button);
expect(screen.getByText('Updated')).toBeInTheDocument();
const user = userEvent.setup();
await user.click(button);
await waitFor(() => {
expect(screen.getByText('Updated')).toBeInTheDocument();
});
Strategy 2: Use findBy queries (preferred for new elements)*
const user = userEvent.setup();
await user.click(button);
expect(screen.getByText('Loaded')).toBeInTheDocument();
const user = userEvent.setup();
await user.click(button);
expect(await screen.findByText('Loaded')).toBeInTheDocument();
Strategy 3: Use waitFor for complex interactions (e.g., dropdowns)
const user = userEvent.setup();
const dropdown = screen.getByText('Select Option');
await user.click(dropdown);
const user = userEvent.setup();
const dropdown = screen.getByText('Select Option');
await user.click(dropdown);
const option = await screen.findByText('Option 1');
await user.click(option);
Note: Do NOT wrap userEvent calls in act(). Since userEvent v14+, all interactions are already wrapped in act() internally. If you see act() warnings, the cause is typically a missing await or async state update that needs waitFor/findBy*.
Strategy 4: Mock timers or async operations
render(<ComponentWithEffect />);
render(<ComponentWithEffect />);
await waitFor(() => {
expect(screen.getByText('Effect completed')).toBeInTheDocument();
});
Common Causes of act() Warnings
-
Dropdown/Select interactions - PatternFly Select/Dropdown components
- Solution: Use
findBy* to wait for dropdown options to appear after click
-
Async state updates - useEffect, setTimeout, promises
- Solution: Use
findBy* or waitFor
-
Form submissions - Forms that trigger async actions
- Solution: Use
waitFor to check for expected outcome
-
Component cleanup - Effects running after test completes
- Solution: Ensure proper cleanup with
waitFor or mock timers
-
Missing await on userEvent calls
- Solution: Always
await userEvent interactions (e.g., await user.click())
Validation Commands
Check for act() warnings:
yarn test -- ComponentName.spec.tsx --no-coverage 2>&1 | grep -i "act()"
Expected result: No output (zero matches)
Enforcement Checklist
Before completing test generation:
If ANY act() warnings exist → IMMEDIATELY FIX before completing
Rule 24: Never Use expect.anything() - Strictly Enforced
CRITICAL: Using expect.anything() defeats the purpose of testing. Always use specific, meaningful assertions.
Why expect.anything() is Forbidden
- ❌ Provides no value - test passes regardless of actual value
- ❌ Masks bugs - incorrect values will pass
- ❌ Reduces confidence - doesn't validate behavior
- ❌ Makes tests meaningless
Examples
expect(StorageClassDropdown).toHaveBeenCalledWith(
expect.objectContaining({
id: 'storageclass-dropdown',
name: 'storageClass',
}),
expect.anything(),
);
expect(StorageClassDropdown).toHaveBeenCalledWith(
expect.objectContaining({
id: 'storageclass-dropdown',
name: 'storageClass',
}),
{},
);
expect(mockFn).toHaveBeenCalledWith({
foo: 'bar',
baz: expect.anything(),
});
expect(mockFn).toHaveBeenCalledWith(
expect.objectContaining({
foo: 'bar',
}),
);
const result = someFunction();
expect(result).toBe(expect.anything());
result = ();
(result).();
(result).();
(result).(, );
When You Think You Need expect.anything()
If you're tempted to use expect.anything(), consider these alternatives:
-
Use expect.objectContaining() without the field
expect(mockFn).toHaveBeenCalledWith(
expect.objectContaining({
importantField: 'value',
}),
);
-
Use specific type matchers
expect(mockFn).toHaveBeenCalledWith(expect.any(String));
expect(mockFn).toHaveBeenCalledWith(expect.any(Function));
expect(mockFn).toHaveBeenCalledWith(expect.any(Object));
-
Use custom matchers
expect(mockFn).toHaveBeenCalledWith(
expect.stringContaining('partial'),
);
expect(mockFn).toHaveBeenCalledWith(
expect.arrayContaining(['item']),
);
-
Don't assert on it at all
expect(mockFn).toHaveBeenCalled();
Enforcement
- ✅ All assertions must be specific and meaningful
- ❌ Zero
expect.anything() in the entire test file
- ✅ Use
expect.any(Type) when you need type checking
- ✅ Use
expect.objectContaining() to test partial objects
Validation Command:
grep -n "expect.anything()" test-file.spec.tsx
Rule 25: Prefer Specific Types Over any
IMPORTANT: While TypeScript is not strictly enforced in test files, prefer specific types when available, or leave untyped.
Why Specific Types Are Preferred
- ✅ Better IDE autocomplete and IntelliSense
- ✅ Catches bugs at development time
- ✅ Makes refactoring safer
- ✅ Documents expected data shapes
- ✅ Self-documenting code
Examples
const input = screen.getByRole('textbox') as HTMLInputElement;
const input = screen.getByRole('textbox') as any;
const mockFn = jest.fn((data: K8sResourceKind) => data);
const mockFn = jest.fn((data: any) => data);
const items: PodDisruptionBudgetKind[] = [];
const items: any[] = [];
const props: CreatePVCFormProps = {};
const props: { [key: string]: any } = {};
Prefer These Approaches When Possible
-
Import actual types from the codebase
import { K8sResourceKind } from '../../module/k8s';
const resource: K8sResourceKind = { ... };
-
Use built-in DOM types
const input = screen.getByRole('textbox') as HTMLInputElement;
const button = screen.getByRole('button') as HTMLButtonElement;
-
Use jest.Mock for type safety
(k8sCreate as jest.Mock).mockResolvedValue(resource);
-
Use generics when appropriate
function mockComponent<T extends object>(props: T) {
return props;
}
-
Use Record for object types
const config: Record<string, boolean> = {};
Guidelines
- ✅ Prefer specific types when they're readily available
- ✅ Use
any for third-party missing types rather than leaving untyped
- ✅ Import types from codebase when possible
- ✅ Use
as jest.Mock for mocked functions
- ✅ Document with comments when using
any for clarity
When to Use any
Acceptable use cases:
- Third-party library types are missing or broken
- Complex mock objects where specific typing is impractical
- Dynamic data structures in test fixtures
- Temporary workarounds for type issues
Best practice: Add a comment explaining why any is used:
const mockProps: any = { ... };
const mockFn = jest.fn() as any;
Rule 26: Add data-test Attributes as Last Resort
IMPORTANT: When high-priority queries (getByRole, getByLabelText, etc.) are impossible or unrealistic, add data-test attributes to the implementation file and use getByTestId in tests.
When to Add data-test Attributes
Use data-test only when:
- ✅ Element has no semantic role
- ✅ Element has no accessible label or text
- ✅ Multiple identical elements exist and cannot be distinguished
- ✅ Element is dynamically generated with no predictable content
- ✅ Using text queries would be too brittle (frequently changing text)
DO NOT use data-test when:
- ❌ Element has a semantic role (button, textbox, checkbox, etc.)
- ❌ Element has accessible label or placeholder text
- ❌ Element has visible text that can be queried
- ❌ You're just being lazy - try harder to find accessible queries first
Implementation Pattern
Step 1: Try all accessible queries first
screen.getByRole('button', { name: 'Submit' });
screen.getByLabelText('Email address');
screen.getByPlaceholderText('Enter your email');
screen.getByText('Welcome back');
Step 2: If truly impossible, add data-test to implementation file
export const MyComponent = () => (
<div>
{/* This element has no role, label, or stable text */}
<div className="custom-widget" data-test="custom-widget">
<svg>...</svg>
</div>
</div>
);
Step 3: Use getByTestId in test file
it('should render the custom widget', () => {
render(<MyComponent />);
const widget = screen.getByTestId('custom-widget');
expect(widget).toBeVisible();
});
Naming Convention for data-test
Use kebab-case and be descriptive:
- ✅
data-test="user-profile-card"
- ✅
data-test="deployment-status-icon"
- ✅
data-test="pod-list-table"
- ❌
data-test="div1" (not descriptive)
- ❌
data-test="UserProfileCard" (not kebab-case)
Examples
❌ BAD - Unnecessary data-test usage:
<button data-test="submit-button">Submit</button>
const button = screen.getByTestId('submit-button');
const button = screen.getByRole('button', { name: 'Submit' });
✅ GOOD - Legitimate data-test usage:
<span className="status-icon" data-test="deployment-status-icon">
<StatusIcon status={status} />
</span>
const icon = screen.getByTestId('deployment-status-icon');
expect(icon).toHaveClass('status-icon');
✅ GOOD - Multiple identical elements:
<div data-test="pod-list">
{pods.map(pod => (
<div key={pod.id} data-test={`pod-item-${pod.id}`}>
<PodIcon />
</div>
))}
</div>
const podItem = screen.getByTestId('pod-item-abc123');
expect(podItem).toBeVisible();
Enforcement Checklist
Before adding data-test:
Remember: Every data-test attribute is a missed opportunity for accessibility. Only use as a last resort.
Instructions for Test Generation
Step 0: Detect Component to Test (When No Argument Provided)
If no component path is provided as an argument, follow this intelligent detection workflow:
-
Run git diff to find changed files:
git diff --name-only HEAD
-
Filter for React components:
- Include files matching:
*.tsx, *.jsx
- Exclude files matching:
*.spec.tsx, *.spec.jsx, *.test.tsx, *.test.jsx (test files)
*.types.ts, *.types.tsx (type definition files)
- Files in
__mocks__/, __tests__/ directories
*utils*.ts, *utils*.tsx, *helpers*.ts (utility files)
*constants*.ts, *types*.ts (non-component files)
-
Validate components:
- Read each filtered file
- Check if file contains React component exports:
export const ComponentName: React.FC
export default function ComponentName
export function ComponentName
class ComponentName extends React.Component
- Exclude files that only export types, interfaces, or constants
-
Present detected components to user:
- If 1 component found: Ask user to confirm
- If multiple components found: Present numbered list and ask user to select
- Format:
[1] packages/console-app/src/components/MyComponent.tsx
-
Fallback if no components detected:
- Inform user: "No React components found in git diff"
- Ask user to provide component path manually
- Suggest using
@ for file autocomplete
Example interaction:
No arguments provided. Checking git diff for component changes...
Found 3 React components modified:
[1] packages/console-app/src/components/forms/CreatePVCForm.tsx
[2] packages/console-shared/src/components/dashboard/UtilizationCard.tsx
[3] public/components/modals/DeleteModal.tsx
Which component would you like to generate tests for? (Enter number or 'all')
Step 1: Analyze the Component
When generating tests for React components:
-
Analyze the component to understand its user-facing behavior
-
Identify test scenarios covering:
- Initial render state
- User interactions (clicks, input, etc.)
- Conditional rendering
- Async operations
- Edge cases and error states
-
Prioritize test scenarios - Generate between 5-10 tests based on component complexity (Rule 22)
- Simple components (5-6 tests): Basic rendering, interactions, and accessibility
- Medium components (7-8 tests): Add conditional rendering and error states
- Complex components (9-10 tests): Full coverage including async operations
Distribution guideline:
- 2-3 Critical User Flows
- 2-3 Error States
- 1-3 Conditional Rendering
- 1-2 User Interactions
- 1 Accessibility
-
Generate tests following all 26 rules above
-
Use appropriate queries based on the priority hierarchy (Rule 26 for data-test)
-
Use verifyInputField for text input fields - strictly enforce Rule 9
-
Write meaningful assertions that validate user experience
-
Include proper TypeScript types for type safety
-
Avoid testing implementation details - focus on behavior
-
Generate 5-10 tests based on component complexity (Rule 22)
- Minimum 5 tests for adequate coverage
- Maximum 10 tests for quality focus
-
Ensure ZERO act() warnings - strictly enforce Rule 23
⚠️ CRITICAL ENFORCEMENT: ES6 Imports Only - ZERO TOLERANCE
🚫 ABSOLUTE RULE: NO require() ANYWHERE
BEFORE GENERATING ANY TEST:
- ✅ Import ALL dependencies at the top using ES6
import statements
- ✅ Import mocked modules (k8s, history, etc.) to use in test bodies
- 🚫 ZERO
require() calls ANYWHERE in the file - NO EXCEPTIONS
- ✅ Mock factories return simple values (null, strings, children) - NO React.createElement
- ✅ Cast mocked imports to
jest.Mock when calling .mockResolvedValue() etc.
- ✅ Import
verifyInputField for form components - Rule 9 strictly enforced
Code Generation Pattern:
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { k8sCreate } from '@console/internal/module/k8s';
import { history } from '@console/internal/components/utils';
import { verifyInputField } from '@console/shared/src/test-utils/unit-test-utils';
jest.mock('../Component', () => () => null);
jest.mock('../ButtonBar', () => ({ children }) => children);
it('should work', () => {
(k8sCreate as jest.Mock).mockResolvedValue({});
jest.spyOn(history, 'push');
});
it('should render form field correctly', async () => {
render(<MyFormComponent />);
await verifyInputField({
: ,
: ,
: ,
: ,
: ,
});
});
(, {
{ k8sCreate } = ();
});
jest.(, {
= ();
.();
});
(, {
();
(screen.()).();
});
Automated Test Generation Workflow
IMPORTANT: Follow this fully automated workflow with mandatory test execution:
1. Auto-detect Test File Name
- DO NOT ask the user for the test file name or any permissions
- Automatically determine test file name based on component file name
- Use pattern:
ComponentName.spec.tsx (or .spec.ts for non-JSX files)
- Place test file in
__tests__/ directory within component directory
- Inform the user which test file will be created (e.g.,
Creating MyComponent.spec.tsx...)
2. Validate ES6 Import Usage (CRITICAL) - ZERO TOLERANCE
🚫 MANDATORY VALIDATION: NO require() ANYWHERE
BEFORE running tests, verify:
- ✅ All dependencies imported at file top using ES6
import
- 🚫 ZERO
require() calls ANYWHERE in the file
- ✅ Mock factories return simple values (null, strings, children)
- ❌ NO React.createElement or React imports needed in mocks
Validation Command:
grep -n "require(" path/to/test.spec.tsx
Expected result:
# ONLY acceptable pattern (if needed for partial mocks):
12: ...jest.requireActual('@console/internal/module/k8s'),
# Everything else is FORBIDDEN
If ANY require() found that is NOT jest.requireActual → IMMEDIATELY FIX before proceeding.
3. Mandatory Test Execution and Validation (Fully Autonomous)
Follow this step-by-step workflow and track progress internally:
Step 1: Run Generated Tests (☐) - MANDATORY FIRST STEP
Step 2: Fix Any Test Failures (☐)
- Analyze ALL test failures from the output
- Fix issues systematically in priority order:
- 🚫 ANY
require() found → Replace with ES6 imports or simple mocks
- Syntax errors and import errors
- Failed assertions (update test logic or fix queries)
- Type errors (add proper types or use
as jest.Mock)
- Missing mocks or incorrect mock implementations
- ⚠️ Missing
verifyInputField for form fields - See Rule 9
- Re-run tests after each significant fix
- DO NOT proceed to Step 3 until all test failures are resolved
Step 3: Verify All Tests Pass (☐)
Step 4: Fix act() Warnings (☐)
- Scan test output for any "not wrapped in act(...)" warnings
- Fix EVERY act() warning using Rule 23 strategies:
- Wrap async interactions in
waitFor
- Use
findBy* queries for async elements
- Add
await waitFor() after dropdown/select interactions
- Ensure async state updates are properly awaited
- Re-run tests after fixing warnings
- DO NOT complete until output has ZERO act() warnings
- Expected: Clean output with no "Warning: An update to" messages
Step 5: Run yarn build Validation (☐)
Iteration Loop
- After each fix, re-run tests and check:
- ✅ All tests pass (Step 3)
- ✅ No warnings in output (Step 4)
- ✅ yarn build passes for test file (Step 5)
- ✅ No console errors or deprecation warnings
- ✅ ZERO act() warnings (strictly enforced)
- ✅ Form fields use
verifyInputField (Rule 9 - strictly enforced)
- 🚫 ZERO
require() anywhere (except jest.requireActual for partial mocks)
- 🚫 NO unused imports (React, etc.)
- Continue iterating until ALL criteria above are met
4. Clean Up Code (MANDATORY)
After all tests pass, perform cleanup following Rule 21:
A. Remove Unused Imports
Examples:
- ✅ Remove
within if not using scoped queries
- ✅ Remove
waitFor if only using findBy*
- ✅ Remove unused mock imports
B. Remove Redundant Mocks
C. Remove Duplicate Tests
- Scan for tests that verify the same behavior
- Consolidate or remove duplicates
- Keep the most comprehensive test
D. Remove Debugging Code
E. Remove Unused Variables
- Remove variables that are declared but never used
- Remove spy mocks that are never asserted
F. Clean Mock Static Methods
- Only keep static methods that are actually called in tests
- Remove unused helper methods from mocks
Cleanup Commands:
grep -n "// screen.debug\|// console\|// TODO" test-file.spec.tsx
Cleanup Verification:
5. Success Criteria - ZERO TOLERANCE
ALL of the following must be true (corresponds to Step 1-5 workflow):
- ✅ Git diff detection for automatic component discovery (when no args provided)
- ✅ Mandatory test execution to validate tests pass
- ✅ Iterative fixing until 100% pass rate achieved
Test Quality (Steps 1-5 Complete)
- ✅ Step 1 Complete: Tests MUST be executed at least once (MANDATORY)
- ✅ Step 2 Complete: All test failures resolved
- ✅ Step 3 Complete: Tests must have 100% pass rate (MANDATORY)
- ✅ Step 4 Complete: Zero act() warnings (Rule 23 - strictly enforced)
- ✅ Step 5 Complete: yarn build passes with no errors for test file
- ✅ Zero warnings in test output
- ✅ Clean console output (no errors, warnings, or deprecation notices)
- ✅ Test count is 5-10 (Rule 22 - minimum 5 for adequate coverage, maximum 10 for quality focus)
Code Quality (ES6 & Mocking)
- 🚫 ZERO
require() in the file (except jest.requireActual for partial mocks)
- ✅ All mocks use simple return values (null, strings, children)
- ✅ NO React.createElement in any mock
- ✅ All imports are ES6
import statements
- ✅ Use
verifyInputField for all text input fields (Rule 9 - strictly enforced)
Code Cleanliness (Rule 21)
- ✅ No unused imports - every import is referenced
- ✅ No unused mocks - every jest.mock() is for components actually used
- ✅ No duplicate tests - each test covers unique behavior
- ✅ No commented code - no debugging code, screen.debug(), console.log()
- ✅ No unused variables - all declared variables are used
- ✅ Clean mock methods - only static methods that are called
Final Validation Commands:
grep "require(" test-file.spec.tsx | grep -v "jest.requireActual"
test -- test-file.spec.tsx --no-coverage 2>&1 | grep -i "act()"
grep -n "expect.anything()" test-file.spec.tsx
grep -q "verifyInputField" test-file.spec.tsx && echo "✅ verifyInputField imported" || echo "⚠️ Check if form fields should use verifyInputField"
grep -n "screen.debug()\|console.log\|console.debug" test-file.spec.tsx
grep -c "^\s*it('.*)" test-file.spec.tsx
yarn build 2>&1 | grep -A 5 "test-file.spec.tsx"
If ANY validation fails → FIX IMMEDIATELY before completing
Note on Test Count:
- If test count < 5: Add more tests to cover critical functionality
- Ensure critical user flows are tested
- Add error state coverage
- Include accessibility tests
- If test count > 10: Review and prioritize the most valuable tests
- Remove redundant or low-value tests
- Keep only the most important 10 tests
- Consider splitting large components into smaller ones
- Ideal range: 5-10 tests based on component complexity
Remember: "The more your tests resemble the way your software is used, the more confidence they can give you."
Generate comprehensive, well-structured test suites that validate the component works correctly from a user's perspective.
Final Checklist Before Completion:
- ✅ Git diff detection implemented (when no args provided)
- ✅ Tests EXECUTED and validated to pass (MANDATORY)
- ✅ Iterative fixing applied until 100% pass rate
Test Quality:
- ✅ All 26 rules followed (especially Rule 9, Rule 22, Rule 23, Rule 24, Rule 26)
- ✅ Tests EXECUTED at least once (MANDATORY - not optional)
- ✅ All tests pass (100% pass rate - MANDATORY)
- ✅ No console warnings or errors
- ✅ ZERO act() warnings (Rule 23)
- ✅ No expect.anything() usage (Rule 24)
- ✅ Prefer specific types over
any (Rule 25)
- ✅ Use data-test only as last resort (Rule 26)
- ✅ Form fields use
verifyInputField utility (Rule 9)
- ✅ Test count is 5-10 (Rule 22 - minimum 5, maximum 10)
Code Quality:
- ✅ Clean code (no unused imports/variables)
- ✅ ES6 imports only (zero require() calls)
- ✅ yarn build passes for test file