lightsoutAlpha

component-test-rendering

Where a component test renders and queries.

Agent checkAdvises by default

The rule

Agents read this text as written when they write and review code.

Render the component inside the setup factory, and query and assert in the test. Rendering is the act, yet it sits in the setup factory: this is the one exception to the act living in the test. Query from screen; never destructure queries from render().

In a test of an interaction, the query that finds the element to act on groups with the act, the userEvent or fireEvent call, not with arrange.

import { expect, describe, test, jest } from '@jest/globals';
import { render, screen } from '@testing-library/preact';
import { NotificationBanner } from './NotificationBanner';

const mockUseAppStore = jest.fn<(selector: (state: unknown) => unknown) => unknown>();

jest.mock('@store/appStore', () => ({
	useAppStore: (selector: (state: unknown) => unknown) => mockUseAppStore(selector),
}));

const setupNotificationBanner = ({ isVisible = true }: { isVisible?: boolean } = {}) => {
	const onDismiss = jest.fn<() => void>();
	mockUseAppStore.mockReturnValue(isVisible);
	render(<NotificationBanner onDismiss={onDismiss} />);

	return { onDismiss };
};

describe('NotificationBanner', () => {
	test('does not render the banner when not visible', () => {
		setupNotificationBanner({ isVisible: false });

		const banner = screen.queryByRole('alert');

		expect(banner).not.toBeInTheDocument();
	});

	test('renders the notification message when visible', () => {
		setupNotificationBanner({ isVisible: true });

		const message = screen.getByText('Action required');

		expect(message).toBeInTheDocument();
	});
});

Examples

The agent flags code like the incorrect example and accepts code like the correct one.

Incorrect

src/notifications/NotificationBanner.unit.test.tsx
import { expect, describe, test } from '@jest/globals';
import { render } from '@testing-library/react';
import { NotificationBanner } from './NotificationBanner';
describe('NotificationBanner', () => {
test('renders the notification message when visible', () => {
// render in the test body, and queries destructured from its result
const { getByText } = render(<NotificationBanner isVisible message="Action required" />);
expect(getByText('Action required')).toBeInTheDocument();
});
});

Correct

src/notifications/NotificationBanner.unit.test.tsx
import { expect, describe, test } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import { NotificationBanner } from './NotificationBanner';
const setupNotificationBanner = ({ isVisible = true }: { isVisible?: boolean } = {}) => {
render(<NotificationBanner isVisible={isVisible} message="Action required" />);
};
describe('NotificationBanner', () => {
test('renders the notification message when visible', () => {
setupNotificationBanner({ isVisible: true });
const message = screen.getByText('Action required');
expect(message).toBeInTheDocument();
});
});

Configure

  • Block"blocking"Stops a run when a file the run changed breaks the rule.
  • Advise"advisory"Reports it and hands it to the refactor agent. Never stops a run.
    Default
  • Off"off"Not checked. Use it when your own linter already enforces the rule.

Add this to your lightsout.config.json, then change the value.

lightsout.config.json
{
"standards-rule-settings": {
"component-test-rendering": "advisory"
}
}

Packs that select it