How many times have you opened a React component at work and found a chaotic soup of a dozen useState calls, loading flags, permission checks, and miles of JSX? Even in React 19, components still turn into unwieldy monsters that are painful to maintain and nearly impossible to test cleanly.
When you test a component like that using standard render from React Testing Library, the test dives deep into every child node. One tiny tweak to styles or internal markup in a small card breaks parent panel tests ten screens up.
Software engineer Evgeny Plotnikov (@bad4iz) proposed an elegant way out: the useSetup[Component] architectural pattern, inspired by Vue 3's setup section, paired with Shallowly, a lightweight shallow-rendering library for Vitest.
Separation of Concerns: Hook for Logic, Component for JSX
The idea is straightforward: the component should stay a pure visual function. All data preparation, loading states, and model mapping move into a dedicated hook prefixed with useSetup:
// src/useSetupProfilePanel.ts
import { useProfile } from './useProfile';
export function useSetupProfilePanel() {
const state = useProfile();
if (state.status === 'loading') {
return {
isLoading: true as const,
userCardProps: null,
};
}
return {
isLoading: false as const,
userCardProps: {
name: state.profile.name,
},
};
}
Now look how clean and readable the component itself becomes:
// src/ProfilePanel.tsx
import { UserCard } from './UserCard';
import { useSetupProfilePanel } from './useSetupProfilePanel';
export function ProfilePanel() {
const { isLoading, userCardProps } = useSetupProfilePanel();
if (isLoading) {
return <p role="status">Loading profile...</p>;
}
return (
<section aria-label="Profile">
<h1>Your profile</h1>
<UserCard {...userCardProps} />
</section>
);
}
The component no longer knows anything about data sources. It just takes ready-made props and lays them out across the element tree.
A Three-Tier Testing Strategy
With this structure, testing splits into three clear layers with zero redundant checks:
- Hook logic test: test
useSetupProfilePanelviarenderHookwith mocked network responses. - Shallow markup test: verify that the parent passes the right props to
UserCardand renders accessibility attributes (role,aria-label). - DOM integration test: run only where you need to verify an end-to-end user flow.
For that second layer, Shallowly steps in as a modern replacement for the old Enzyme, built for React 18/19 and Vitest:
// src/ProfilePanel.shallow.test.tsx
import { shallow } from 'shallowly';
import { describe, expect, it, vi } from 'vitest';
import { ProfilePanel } from './ProfilePanel';
import { UserCard } from './UserCard';
import { useSetupProfilePanel } from './useSetupProfilePanel';
vi.mock('./useSetupProfilePanel');
describe('ProfilePanel shallow tests', () => {
it('passes prepared props to the child card', () => {
const userCardProps = { name: 'Алексей' };
useSetupProfilePanel.mockReturnValue({ isLoading: false, userCardProps });
const wrapper = shallow(<ProfilePanel />);
expect(wrapper.find(UserCard).props()).toEqual(userCardProps);
});
it('renders loading status without rendering the card', () => {
useSetupProfilePanel.mockReturnValue({ isLoading: true, userCardProps: null });
const wrapper = shallow(<ProfilePanel />);
expect(wrapper.find('p').props()).toEqual({
role: 'status',
children: 'Loading profile...',
});
expect(wrapper.find(UserCard).exists()).toBe(false);
});
});
In a Shallowly test, UserCard isn't even initialized: its hooks don't run, no network requests fire, and the whole test finishes in milliseconds.
How to Run the Demo Project Locally
To try this approach on Node.js 22 or 24, clone the official repository at the article's tag and run the test suite:
git clone --branch profile-panel-article-v3 --single-branch https://github.com/bad4iz/unit-test-article.git
cd unit-test-article/examples/profile-panel
npm ci && npm test && npm run typecheck
All 6 tests (covering shallow, hooks, and DOM) pass in fractions of a second with zero type errors.
This pattern takes a bit of discipline when creating files, but it pays off right away: your tests stop being brittle, and logic and markup evolve independently.

![Node.JS [ru]](/api/digests/it_development/daily/20261020/assets/sources/we-use-js.jpg?v=7d0cf00ec27629c4c254ad643d9de4a5694fea85ef589c9c4a7b8b22590a5681)