Stop Running Your Entire Test Suite on Every Commit 🚫🧪
Source: Dev.to
The Problem
We’ve all been there: you make a small fix, commit, and watch the entire test suite run for several minutes.
Running the full test suite before every commit is:
- ⏰ Slow – wastes precious development time
- 😤 Frustrating – breaks your flow state
- 🔄 Redundant – tests code you didn’t touch
Most developers either:
- Skip tests entirely (dangerous)
- Wait through long test runs (frustrating)
- Push and pray the CI catches issues (risky)
There has to be a better way.
Introducing test‑staged
test-staged is like lint-staged, but for tests. It intelligently identifies which tests are related to your staged changes and runs only those.
How it works
| Test Runner | Detection Method |
|---|---|
| Vitest ⭐ | Native Vitest related (dependency graph) |
| Jest ⭐ | Native --findRelatedTests (dependency graph) |
| Mocha ✅ | File pattern matching |
| Ava ✅ | File pattern matching |
More runners are coming soon!
Installation
# npm
npm install -D test-staged
# pnpm
pnpm add -D test-staged
# yarn
yarn add -D test-staged
# bun
bun add -D test-staged
Zero‑config by default.
Using with Husky
# Install husky
npm install -D husky
npx husky init
# Add test‑staged to pre‑commit hook
echo "npx test-staged" > .husky/pre-commit
Now every commit will:
- ✅ Run only tests related to your changes
- ⚡ Complete in seconds instead of minutes
- 🛡️ Block the commit if tests fail
Real‑World Examples
Example 1: User authentication feature
# You modified:
git add src/auth/login.ts
test-staged runs only:
src/auth/__tests__/login.test.ts
src/integration/__tests__/auth-flow.test.ts # because it imports login.ts
It skips unrelated suites such as src/payments/__tests__/*.test.ts and src/dashboard/__tests__/*.test.ts.
Result: tests finish in 3 seconds instead of 2 minutes.
Example 2: Refactoring a utility function
# You modified:
git add src/utils/formatDate.ts
test-staged automatically finds all dependent tests:
src/utils/__tests__/formatDate.test.ts
src/components/__tests__/DatePicker.test.ts
src/pages/__tests__/Dashboard.test.ts
Example 3: Monorepo support
# You're in packages/ui
git add Button.tsx
Only runs:
packages/ui/__tests__/Button.test.tsx
It does not run tests from packages/api/ or packages/cli/.
Performance Gains
On a typical project with 500+ tests:
| Before | After |
|---|---|
| 2–3 minutes per commit | 2–5 seconds per commit |
≈ 36× speed improvement in real‑world usage.
Configuration (optional)
package.json
{
"test-staged": {
"runner": "jest",
"mode": "related",
"testExtensions": [".test", ".spec", ".e2e"]
}
}
.test-stagedrc.json
{
"runner": "vitest",
"testExtensions": [".test", ".spec", "Test", "E2E"]
}
Typical Workflow
# Make your changes
vim src/components/Button.tsx
# Stage them
git add src/components/Button.tsx
# Commit (test‑staged runs automatically via pre‑commit hook)
git commit -m "fix: button hover state"
Output example
Running tests for staged files...
✓ src/components/__tests__/Button.test.tsx (2 tests) 0.8s
Tests passed! ✨
[main abc1234] fix: button hover state
No manual test commands, no waiting, no broken commits.
FAQ
Q: What if I want to run all tests?
A: Bypass the hook with git commit --no-verify or run your test suite manually.
Q: Does it work with CI?
A: Yes. CI should still run the full test suite. test-staged is for local development speed.
Q: What about integration/E2E tests?
A: They run if they import the changed files. You can also customize which tests are considered.
Q: Can I use it without Git hooks?
A: Absolutely. Run npx test-staged manually whenever you like.
Getting Started in Under a Minute
npm install -D test-staged husky
npx husky init
echo "npx test-staged" > .husky/pre-commit
Then make a commit and watch the magic happen. ✨
Resources
The Bottom Line
If you’re still running your entire test suite on every commit, you’re wasting time. test-staged gives you:
- ⚡ Faster commits (seconds instead of minutes)
- 🎯 Better focus (test only what changed)
- 🛡️ Safer code (tests actually run)
- 🚀 Improved developer experience (zero config, just works)
Give it a try and let us know what you think! 🙌