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

example-test-gen

v1.0.0-prerelease-2026-04-01

Published

Generate test files from @example snippets in your code

Readme

example-test-gen

Write tests as @example snippets in your source code. Generate and run them with one command.

Quick Start

# Generate Vitest tests from all @example snippets
npx example-test-gen --config=vitest

# Or Jest tests
npx example-test-gen --config=jest

Your tests appear next to your source files, ready to run.


How It Works

1. Write examples in your source code:

// src/math.ts

/**
 * Adds two numbers
 * @example basic addition
 * ```ts
 * import { add } from './math.ts';
 * expect(add(2, 3)).toBe(5);
 * ```
 */
export function add(a: number, b: number): number {
  return a + b;
}

2. Generate tests:

npx example-test-gen --config=vitest

3. Run your tests:

npm test

What Gets Generated

From the @example above, example-test-gen creates src/math.test.ts:

// Auto-generated test file from @example snippets
// Source: src/math.ts
// Generated: 2026-04-01T21:30:00.000Z

import { add } from './math.ts';
import { test, expect } from 'vitest';

test('basic addition', async () => {
  expect(add(2, 3)).toBe(5);
});

Note about imports: Any import statements in your @example snippet get automatically moved to the top of the generated test file. The test body contains only your assertions and method calls—no import clutter.


CLI Options

| Flag | Description | Default | |------|-------------|---------| | --config=<name> | Use built-in config: vitest or jest | Required | | --config=<path> | Path to custom config file | — | | --include=<pattern> | Override which files to scan | From config | | --exclude=<pattern> | Override which files to skip | From config | | --outDir=<dir> | Where to put generated tests | Same as source | | --rootDir=<dir> | Source root for path calculations | ./src | | --overwrite | Replace existing test files | false |

Examples

# Scan only utils directory
npx example-test-gen --config=vitest --include="src/utils/*.ts"

# Output to tests/ directory
npx example-test-gen --config=vitest --outDir=tests

# Custom config file
npx example-test-gen --config=./my-config.mjs

Custom Configuration

Create a .mjs file for full control:

// test-config.mjs
export default {
  include: ['src/**/*.ts'],
  exclude: ['**/*.test.ts'],
  outDir: 'tests',
  mapper: 'vitest'  // or 'jest', or a custom function
};

Then run:

npx example-test-gen --config=./test-config.mjs

Installation

npm install --save-dev example-test-gen

Or use directly via npx (no install needed).


Documentation