npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vitest-test-ratio

v0.2.0

Published

Measure Code to Test LOC ratio in Vitest projects.

Readme

vitest-test-ratio

vitest-test-ratio is a single-purpose CLI that measures Code LOC to Test LOC ratio for Vitest projects, with a Rails stats style summary line.

This repository uses pnpm for development.

Features

  • Reads vitest.config.* when possible and uses test.include / test.exclude
  • Falls back to built-in test patterns when config cannot be resolved
  • Collects ts / tsx / js / jsx source and test files
  • Excludes .d.ts, node_modules, dist, coverage, and detected test files from code files
  • Computes project-level Code LOC, Test LOC, ratio, and unmatched file count
  • Maps source files to tests by basename (supports __tests__ and tests/)
  • Supports text output and JSON output

Install

npm install -g vitest-test-ratio

Or run without installing globally:

npx vitest-test-ratio

With pnpm:

pnpm dlx vitest-test-ratio

Usage

vitest-test-ratio [options]

Options

  • --files Show per-file ratio entries
  • --top <n> Show top N files by code LOC (implies --files)
  • --json Output JSON
  • --cwd <path> Analyze a specific directory (default: current directory)
  • -h, --help Show help

Vitest Integration Examples

vitest-test-ratio is a standalone CLI, not a Vitest plugin.
You can still integrate it into a Vitest workflow with npm scripts or CI.

Run after Vitest locally

{
  "scripts": {
    "test:with-ratio": "vitest run && vitest-test-ratio --files"
  }
}

Run after tests in GitHub Actions

- name: Test
  run: pnpm run test

- name: Test ratio
  run: pnpm vitest-test-ratio --json

Text Output

Summary line format:

Code LOC: 4231     Test LOC: 3120     Code to Test Ratio: 1:0.74     Unmatched files: 12

Per-file unmatched entries show:

No matching test

JSON Output

JSON includes:

  • project.codeLoc
  • project.testLoc
  • project.ratio
  • project.ratioFormatted
  • project.unmatchedFiles
  • files[] entries:
    • source
    • codeLoc
    • testLoc
    • ratio
    • ratioFormatted
    • matchedTests

For unmatched files:

  • testLoc: 0
  • ratio: null
  • matchedTests: []

ratio and ratioFormatted are capped at a maximum of 1:2.00.

How File Matching Works

  • Source and test files are matched by basename.
  • Example: src/user.ts matches tests/user.test.ts, __tests__/user.spec.ts, etc.
  • If multiple tests match one source file, their LOC values are summed.

LOC Counting Policy

This tool uses sloc by default for supported file types (.ts, .tsx, .js, .jsx) and reads source lines from its result.

If sloc cannot be applied for a file (for example, unsupported extension or runtime parse failure), it falls back to a lightweight local counter:

  • remove comment-only content with a simple parser
  • count non-empty lines

This fallback keeps the CLI resilient without requiring additional user setup.

Vitest Config Support

vitest-test-ratio looks for:

  • vitest.config.ts
  • vitest.config.mts
  • vitest.config.cts
  • vitest.config.js
  • vitest.config.mjs
  • vitest.config.cjs

When readable, it extracts test.include and test.exclude string arrays. If extraction fails, built-in test patterns are used.

Non-goals

  • No coverage integration
  • No import graph analysis
  • No Vite config parsing
  • No workspace/deep monorepo features
  • No extra metrics beyond code/test ratio output

Development

pnpm install
pnpm run lint
pnpm run test
pnpm run test:coverage
pnpm run build

Try It Locally

This repository includes a runnable sample project under examples/basic.

pnpm run demo

Quality Gates

  • Unit tests: Vitest (tests/*.test.ts)
  • Coverage: pnpm run test:coverage (output: coverage/, HTML report: coverage/index.html)
  • Lint / format: Biome (pnpm run lint, pnpm run format)
  • CI: GitHub Actions (.github/workflows/ci.yml) runs lint, build, and test

Publish with GitHub Actions

This repository includes a publish workflow:

  • publish.yml
  • Trigger: push tag v* or manual workflow_dispatch
  • Auth: npm Trusted Publishing (OIDC, no NPM_TOKEN secret)

Required setup:

  1. On npm, open package settings and add a Trusted Publisher for this GitHub repository/workflow
  2. Keep the workflow file path as .github/workflows/publish.yml
  3. Bump package.json version
  4. Push a matching tag (example: v0.1.0 for version 0.1.0)

Example release commands:

git tag v0.1.0
git push origin v0.1.0

License

MIT