Getting Started
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.
Installation
bash
# npm
npm install -D test-staged
# pnpm
pnpm add -D test-staged
# yarn
yarn add -D test-staged
# bun
bun add -D test-stagedUsage
You can run it manually, but it's best used as a pre-commit hook.
Manual Run
bash
npx test-stagedSet up with Husky (Recommended)
Install
husky:bashnpm install -D husky npx husky initAdd
test-stagedto your pre-commit hook:bashecho "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
bash
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:
bash
npx test-staged "**/*.ts" "!**/*.test.ts"