Colocating Tests and Styles with Components
You'll learn how grouping related files together makes your components easier to find, update, and maintain.
The Core Idea
Colocation means keeping files that belong together physically close in your folder structure. Instead of separating tests into a tests/ folder and styles into a styles/ folder, you keep everything related to a component right next to the component itself.
For a Button component, you'd have:
Button/
Button.jsx
Button.test.jsx
Button.module.css
Or in a flat structure:
components/
Button.jsx
Button.test.jsx
Button.module.css
Why Colocation Matters
Think of it like organizing a toolbox. Would you rather have all screwdrivers in one drawer across the garage, all wrenches in another, and all hammers upstairs? Or would you keep your plumbing tools together, electrical tools together, and carpentry tools together?
When you need to update the Button component, you don't want to hunt through separate tests/, styles/, and components/ folders. Having Button.jsx, Button.test.jsx, and Button.module.css in the same location means:
- Faster navigation: Everything's right there
- Easier updates: Change the component, update its test, adjust its styles—all in one place
- Clearer ownership: You instantly see what belongs to what
- Better deletions: Delete the folder, and all related code goes with it
The Pattern in Practice
When you open your editor's file tree and navigate to Button/, you see the complete picture of that component. No jumping between distant folders. No forgotten orphaned test files.
Key Takeaway: Colocate related files—component code, tests, and styles—in the same folder to make your codebase easier to navigate and maintain.