Tests can be organized either alongside source files (Component.test.js) or in a separate __tests__ directory. Use describe blocks to group related tests, clear test names that describe behavior, and follow the Given-When-Then or AAA pattern for clarity.
File Organization:
Co-located tests:
Component.js + Component.test.jsSeparate directory:
src/ and __tests__/Naming Conventions:
*.test.js or *.spec.jsTest Structure:
describe: Group related teststest or it: Individual test cases// Co-located structure
// src/
// components/
// Button/
// Button.jsx
// Button.test.jsx
// Button.module.css
// utils/
// format.js
// format.test.js
// Separate __tests__ structure
// src/
// components/
// Button.jsx
// utils/
// format.js
// __tests__/
// components/
// Button.test.jsx
// utils/
// format.test.js
// Jest config for finding tests
module.exports = {
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)'
]
};