vitest-bats weekly downloads statistics
By spencerbeggs · npm · Testing
Test bash scripts with kcov coverage directly in Vitest
vitest-bats has 2 recorded weekly downloads on npm, a decrease of 91.3% over the past 30 days, ranking #2182 of 2,256 tracked listings.
About vitest-bats
vitest-bats
A Vitest plugin for testing bash scripts with BATS and collecting kcov coverage, merged into Vitest's standard v8 coverage report.
Features
• Single plugin setup -- BatsPlugin() handles dependency detection, reporter injection, environment configuration, and matcher registration
• Native .sh imports -- import script from "./hello.sh" returns a builder. Configure with env() / flags() / mock(), terminate with run(...args) or exec(shellExpr).
• 23 expect.extend matchers -- toSucceed, toContainOutput, toHaveJsonValue, toMatchSchema, toHaveInvoked, and more. Auto-registered via setup file -- no user setup required.
• Schema validation -- toMatchSchema accepts any Standard Schema validator (Zod, Valibot, Arktype, Effect Schema). toMatchJsonSchema validates raw JSON Schema via Ajv.
• Self-contained command mocking -- .mock("git", { "remote get-url *": "echo https://example.com" }) embeds shim binaries that record calls and emit pre-recorded responses. No bats-mock runtime dependency.
• Unified coverage -- Shell script coverage from kcov is merged into the v8 Istanbul CoverageMap and reported alongside TypeScript coverage.
Installation
vitest-bats requires vitest >= 4.1.0 and BATS >= 1.5 (for the run --separate-stderr form used to capture stderr separately).
System Dependencies
These must be installed on the host (or in your Docker container):
| Dependency | Required | Install (macOS) | Install (Linux) |
| bats (>= 1.5) | Yes | brew install bats-core | apt-get install bats | | bats-support | Yes | brew install bats-support | apt-get install bats-support | | bats-assert | Yes | brew install bats-assert | apt-get install bats-assert | | bats-mock | Yes | brew install bats-mock | apt-get install bats-file | | jq | Yes | brew install jq | apt-get install jq | | kcov | Coverage only | brew install kcov | apt-get install kcov |
bats-assert and bats-mock are still detected at startup for compatibility warnings; the runtime no longer loads them. Mocks are self-contained, and assertions happen in TypeScript via the matchers below.
Quick Start
- Configure Vitest
The plugin auto-injects vitest-bats/setup into test.setupFiles. Do not add it manually -- it registers all matchers and a beforeEach builder reset.
- Add the .sh module type
In tsconfig.json, add vitest-bats to types so TypeScript understands .sh imports:
- Write Tests
run() returns a BatsResult synchronously. await is harmless but unnecessary -- BatsResult is not thenable.
- Run Tests
API Reference
BatsPlugin(options?)
Vitest plugin. Add to the plugins array in vitest.config.ts.
Coverage modes:
| Value | Behavior |
| "auto" (default) | Shell coverage on when kcov is available and platform is not macOS | | true | Require kcov on a non-macOS platform; throw otherwise | | false | Always exclude shell scripts from coverage |
deps:
• "error" (default) -- throw on missing required dependencies • "warn" -- log warnings; pure-TS unit tests still run, integration tests using .sh imports fail individually
Importing Scripts
.sh imports return a ScriptBuilder instance:
The plugin's Vite transform (resolveId + load) intercepts the import and emits a virtual module that constructs a ScriptBuilder bound to the script's absolute path.
ScriptBuilder
Configuration methods accumulate state and return this for chaining. Termination methods (run / exec) consume the state, execute the script, and return a BatsResult. Builder state is reset after every termination (via a finally block) and again before each test (via the auto-injected beforeEach).
Configuration
| Method | Purpose |
| env(vars) | Set environment variables for the next run. Multiple calls accumulate. | | flags(value) | Append a fixed flag string to the script invocation. Replaces prior flags(). | | mock(cmd, responses?) | Register a recorder shim for cmd. Patterns map to bash response expressions. |
mock() patterns are converted to bash globs: literal segments are double-quoted while * ? [ ] stay active. Responses are evaluated by eval so they can include any bash expression.
If no pattern matches at runtime, the shim emits an error to stderr and exits 1. Mocks accumulate across mock() calls; passing an empty responses map records calls without producing output.
Termination
| Method | Generated invocation |
| run(...args) | "$SCRIPT" arg0 arg1 ... (kcov-wrapped when coverage is on) | | exec(shellExpr) | bash -c '' -- $SCRIPT is exported. Not kcov-wrapped. |
exec() is the escape hatch for pipelines and shell expressions:
BatsResult
Returned by run() / exec(). Plain class, not thenable.
result.json() lazily parses output as JSON, caching the value on first call. Throws with a descriptive message if the output is not valid JSON.
Matchers
23 matchers are registered automatically. All operate on a BatsResult.
Status
| Matcher | Asserts |
| toSucceed() | status === 0 | | toFail(code?) | status !== 0 (or status === code if given) |
Output (stdout)
| Matcher | Asserts |
| toHaveOutput(text) | output === text | | toContainOutput(text) | output.includes(text) | | toMatchOutput(pattern) | RegExp test against output | | toHaveEmptyOutput() | output === "" |
Stderr
| Matcher | Asserts |
| toHaveStderr(text) | stderr === text | | toContainStderr(text) | stderr.includes(text) | | toMatchStderr(pattern) | RegExp test against stderr |
Lines
| Matcher | Asserts |
| toHaveLine(index, text) | lines[index] === text | | toHaveLineContaining(text, index?) | Line at index contains text, or any line if index omitted | | toHaveLineMatching(pattern, index?) | Same shape, RegExp | | toHaveLineCount(n) | lines.length === n |
JSON
| Matcher | Asserts |
| toOutputJson() | result.json() doesn't throw | | toEqualJson(expected) | Parsed JSON deep-equals expected | | toMatchJson(partial) | partial is a deep subset of parsed JSON | | toHaveJsonValue(path, expected) | Path exists and deep-equals expected | | toHaveJsonPath(path) | Path exists |…
Description from the npm listing.
- vitest
- bats
- bash
- shell
- testing
- kcov
- coverage
- vitest-plugin
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (121)Similar npm Testing plugins
All 20 vitest-bats alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/vitest-bats. First seen Oct 16, 2026; last successful collection Oct 19, 2026. See the methodology.