lightsoutAlpha

screen-test-under-router

How a screen test is set up.

Agent checkAdvises by default

The rule

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

Render a screen inside a memory-history router whose one route renders it, with a query client seeded with what its loader would have fetched: queryClient.setQueryData(issuesQueryOptions().queryKey, issues). Never mock the router's hooks or the query hooks.

A screen rendered free of its router and query client is not the one the route renders. Under them it is, which is why the route itself gets no test.

Examples

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

Incorrect

src/features/issues/screens/IssuesScreen.unit.test.tsx
import { describe, expect, jest, test } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import { IssuesScreen } from './IssuesScreen.tsx';
const mockUseSuspenseQuery = jest.fn<() => { data: Array<{ id: number; title: string }> }>();
// Incorrect: with the query hook and the router mocked away, the screen under
// test is not the one the route renders — its key, loader and links are gone.
jest.mock('@tanstack/react-query', () => ({ useSuspenseQuery: () => mockUseSuspenseQuery() }));
jest.mock('@tanstack/react-router', () => ({ Link: ({ children }: { children: string }) => children }));
const setupIssuesScreen = () => {
mockUseSuspenseQuery.mockReturnValue({ data: [{ id: 1, title: 'Login fails' }] });
render(<IssuesScreen />);
};
describe('IssuesScreen', () => {
test('lists each issue by title', () => {
setupIssuesScreen();
const row = screen.getByText('Login fails');
expect(row).toBeInTheDocument();
});
});

Correct

src/features/issues/screens/IssuesScreen.unit.test.tsx
import { describe, expect, test } from '@jest/globals';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { RouterProvider, createMemoryHistory, createRootRoute, createRoute, createRouter } from '@tanstack/react-router';
import { render, screen } from '@testing-library/react';
import type { Issue } from '../types/Issue.ts';
import { issuesQueryOptions } from '../queries/issuesQueryOptions.ts';
import { IssuesScreen } from './IssuesScreen.tsx';
// Correct: the screen renders under a router whose one route is its own, with a
// query client holding what the loader would have fetched.
const setupIssuesScreen = ({ issues = [{ id: 1, title: 'Login fails' }] }: { issues?: Issue[] } = {}) => {
const queryClient = new QueryClient();
queryClient.setQueryData(issuesQueryOptions().queryKey, issues);
const rootRoute = createRootRoute();
const issuesRoute = createRoute({ getParentRoute: () => rootRoute, path: '/issues', component: IssuesScreen });
const router = createRouter({ routeTree: rootRoute.addChildren([issuesRoute]), history: createMemoryHistory({ initialEntries: ['/issues'] }) });
render(
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>,
);
};
describe('IssuesScreen', () => {
test('lists each issue by title', async () => {
setupIssuesScreen();
const row = await screen.findByText('Login fails');
expect(row).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": {
"screen-test-under-router": "advisory"
}
}

Packs that select it