test-staged
v1.1.11
Published
Run tests for your staged files. Zero config.
Downloads
1,307
Maintainers
Readme
test-staged 🚫🧪
Run tests for your staged files. Zero config.
Run only the tests that are related to your changes. Stop pushing broken code.
Why?
Running your entire test suite before every commit is slow. test-staged intelligently identifies which tests are related to your staged changes and runs only them. It's like lint-staged, but for tests.
Features
- ⚡️ Fast: Runs only tests related to staged files.
- ⚙️ Zero Config: Automatically detects your package manager and test runner.
- 🧩 Smart Detection:
- Jest / Vitest: Uses native "related tests" mode (dependency graph analysis).
- Mocha / Ava: Automatically maps source files to test files (e.g.,
foo.ts→foo.test.ts,src/foo.ts→src/__tests__/foo.test.ts).
- 📦 Monorepo Support: Works seamlessly in workspaces.
- 🛠 Agnostic: Works with
npm,pnpm,yarn, andbun.
Installation
npm install -D test-stagedor
pnpm add -D test-stagedor
yarn add -D test-stagedor
bun add -D test-stagedUsage
You can run it manually, but it's best used as a pre-commit hook.
Manual Run
npx test-stagedSet up with Husky (Recommended)
Install
husky:npm install -D husky npx husky initAdd
test-stagedto your pre-commit hook:echo "npx test-staged" > .husky/pre-commit
Now, every time you commit, test-staged will run tests related to your changes. If tests fail, the commit is blocked.
Command Line Flags
Usage: test-staged [globs] [options]
Options:
--cwd <cwd> Current working directory
-h, --help Display help
-v, --version Display versionYou can pass glob patterns as arguments to override the configuration:
npx test-staged "**/*.ts" "!**/*.test.ts"Supported Test Runners
test-staged automatically detects which runner you are using based on your package.json dependencies and configuration files.
| Runner | Support Level | Method |
| :--- | :--- | :--- |
| Vitest | ⭐️ Full | Native vitest related |
| Jest | ⭐️ Full | Native jest --findRelatedTests |
| Mocha | ✅ Basic | File pattern matching |
| Ava | ✅ Basic | File pattern matching |
Configuration
test-staged is zero-config by default, but you can customize its behavior using a configuration file.
Configuration Files
test-staged supports the following configuration files (searched in order):
package.json(undertest-stagedkey).test-stagedrc.test-stagedrc.json.test-stagedrc.js.test-stagedrc.cjstest-staged.config.jstest-staged.config.cjs
Options
{
// Force a specific runner (optional)
"runner": "jest",
// 'related': Use native "related tests" feature if available (default).
// 'match': Map staged files to test files (e.g. foo.ts -> foo.test.ts) and run them directly.
"mode": "related",
// Custom test extensions (optional)
// Defaults to [".test", ".spec"]
"testExtensions": [".test", ".spec", ".mytest"]
}Troubleshooting
Runner not detected
If test-staged cannot detect your test runner, you can manually specify it in the configuration:
{
"runner": "jest"
}Tests not found
If your test files are not being picked up, ensure your testExtensions are configured correctly. By default, it looks for .test and .spec extensions.
{
"testExtensions": [".test.ts", ".spec.ts", "Test.ts"]
}"Related" mode not supported
Some runners (like Mocha and Ava) do not support "related" mode (running tests related to changed files via dependency graph). In these cases, test-staged will automatically fall back to "match" mode (mapping source files to test files by name).
Development
Releasing
This project uses Changesets for versioning and publishing.
Create a changeset: When you make changes, run:
pnpm changesetSelect the packages you changed and the bump type (major/minor/patch). This will create a markdown file in
.changeset/.Commit: Commit the changeset file along with your code changes.
Version Packages: When ready to release, run:
pnpm version-packagesThis will consume the changesets, bump versions in
package.json, and updateCHANGELOG.md.Publish: Push the changes and run:
pnpm releaseThis will build the project and publish packages to npm.
