Skip to content

TST-Studio/tst-cli

Repository files navigation

About the Project

tst is a tool from TST-Studio that automatically generates unit tests for JavaScript and TypeScript code, helping developers maintain flow, reduce boilerplate, and improve test coverage. Built with TypeScript, Vitest, OCLIF, ts-morph, and LLMs (like OpenAI, Claude, etc).

screenshot

Example

Math.ts

export function add(a: number, b: number): number {
  return a + b;
}

Generated test (math.test.ts):

import { describe, it, expect } from 'vitest';
import { add } from './math';

describe('add', () => {
  it('adds two numbers', () => {
    expect(add(2, 3)).toBe(5);
  });
});

Table of Contents

Tech Stack

  • Language: TypeScript
  • Test Runner: Vitest
  • CLI Framework: OCLIF
  • LLM Integration: OpenAI
  • AST Parsing: ts-morph

Features

  • Generate unit tests automatically from source files
  • Output structured, runnable Vitest test files
  • CLI interface for smooth developer workflow

Getting Started

You can use this tool in your own repositories.

Installation

$ npm install -D @tst-studio/tst

Configuration

  1. Add an entry into package.json scripts to access the command from the repository
"scripts": {
  "tst": "tst generate"
}
  1. Add a tst.config.json configuration file to configure tst command behavior:
$ tst configure
Wrote tst.config.json
  1. Create an .env file to store the token for accessing the LLM. This example uses OpenAI:
$ cat .env
TST_OPENAI_API_KEY='sk-proj-....'
  1. Ensure you do not check this into your repository by including .env in your .gitignore file

Usage

Generate tests for a file

$ npm run tst ./src/fib.js 

> cli-demo@1.0.0 tst
> tst generate ./src/fib.js

πŸš€ Generated test file: src/fib.test.js πŸš€

This will submit the whole file to the LLM and create a test file in the appropriate location.

IDE Integration

TST-Studio is passionate about making test creation as seamless as possible in any IDE. The tst-cli was created to ensure compatibility across many IDEs.

For example, here is a fairly simple way to integrate the IDE command into VSCode.

Assuming, the package.json commands have the following entry:

"scripts": {
  "tst": "tst generate"
}

One can create a .vscode/tasks.json configuration:

{ 
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run tst command on current file",
      "type": "shell",
      "command": "npm run tst",
      "args": ["${file}"],          // the file you have open/active
      "presentation": { "reveal": "always" },
      "problemMatcher": []
    }
  ]
}

Then, from the Command Pallet, the "Tasks: Run Task" will have a task named "Run tst command on current file".

If one runs tasks often, this will be the top task on the list.

To make this a more streamlined process, one can configure Key Bindings that, when pressed, will run this task. Ensure the task name matches the task above exactly: Run tst command on current file.

.vscode/keybindings.json:

[
  {
    "key": "ctrl+alt+r",
    "mac": "cmd+alt+r",
    "command": "workbench.action.tasks.runTask",
    "args": { "task": "Run tst command on current file" }
  }
]

How to Contribute

Help us build the most seamless automated test generation tool possible.

Issues & PRs welcome!

Before opening a PR:

  • Write tests where appropriate (or use this tool! πŸ˜€ )
  • Run npm run format:check && npm test.

Prerequisites

Ensure you have the following installed:

Tech Stack

  • Language:

  • CLI Framework:

  • Parsing & Utilities:

    • ts-morph – TypeScript AST parsing
    • globby – File globbing
    • fs-extra – Extended file system utilities
    • chalk – Terminal styling
    • zod – Schema validation
    • dotenv – Environment variable management
  • Developer Tooling:

  • OCLIF Support:

    • oclif – CLI scaffolding and packaging
    • @oclif/plugin-legacy – Legacy command support
    • oclif.manifest.json – Generated CLI manifest

Developing

The dev.js command is a development command that represents the tst command but will continue to compile Typescript on the fly while developing.

$ bin/dev.js
LLM-powered unit test generator CLI by TST-Studio

VERSION
  @tst-studio/tst/0.1.0 darwin-arm64 node-v22.17.1

USAGE
  $ tst [COMMAND]

TOPICS
  auth  Write API key to local .env or create one

COMMANDS
  configure  Create or update tst.config.json
  generate   Generate Vitest tests from a source file

To test outside of the tst-cli repo (e.g., to test against a different tsconfig.json file without breaking the tst command), use another repository like this sandbox repository:

https://github.com/TST-Studio/demo-test-script


Publish (maintainers only)

Use this checklist when cutting a new release to npm.

0) Prerequisites

  • You are a maintainer for the @tst-studio org/package.
  • Your npm account has 2FA enabled.
  • You’re logged in: npm whoami (login with npm login if needed)

1) Branch is clean

Ensure:

  • All code is formatted consistently: npm run format
  • README.md is up to date
  • All code is committed

Ensure main is clean:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Next steps may fail if main is not clean.

2) Prep the release

Verify the package contents

  1. Build the tst command:
npm run build
  1. Pack the tst command and inspect the tarball
npm pack
  1. View a publish dry run. Sanity check what is included:
npm publish --dry-run

3) Bump the version (SemVer)

This step updates package.json, creates a git tag (e.g., v0.1.1), and commits.

Determine if this is:

  • A patch (bug fix. No functionality changed)

  • A minor release (functionality was added)

  • A major release (backwards incompatible changes added)

  • Ensure main is clean (see step 1).

NOTE: The following instructions need to have single quotes (not double quotes) as they can include an exclamation mark (used by the "npm version" command). Without the single quotes, the exclamation mark can be interpreted as a history command by the shell.

If Patch

npm version patch -m 'chore(release): Bump version to %s'

If Minor

npm version minor -m 'feat!(release): Bump version to %s'

If Major

npm version major -m 'feat!(release): Bump version to %s'

4) Publish to NPM

npm publish

5) Push tag and verify

git push --follow-tags
npm view @tst-studio/tst version

Common Pitfalls

  • EPUBLISHCONFLICT: That version already exists

  • E403 / not authorized to publish: You’re not a maintainer for @tst-studio/tst under the org; ask an admin to add you or your team.

  • 2FA errors: Provide --otp=(Your code) or re-enable 2FA on your account.

  • Wrong files in the tarball: Use .npmignore or files in package.json; re-run npm pack to confirm.


Documentation

Output Location

You can control where test files are generated using the outFormat option in tst.config.json.

  • sameLocation

    tst generate ./src/queue.js --outFormat=sameLocation

    Produces: ./src/queue.test.js

  • testDir

    tst generate ./src/queue.js --outFormat=testDir --outBaseSrc=./src --outBaseTest=./tests

    Produces: ./tests/queue.test.js


Configuration

tst expects a configuration file in your project root:

tst.config.json

Example:

{
  "provider": "openai",
  "model": "gpt-4o-mini",
  "outFormat": "testDir",
  "outBaseSrc": "./src",
  "outBaseTest": "./tests",
  "astLibrary": "ts-morph",
  "testingFramework": "vitest",
  "moduleType": "module"
}
Fields
  • provider: "openai" (future: "anthropic", "vertex", "azure-openai", "bedrock", etc.)
  • model: "gpt-4o-mini" (future: "gpt-4o", "gpt-4.1", "gpt-4.1-mini")
  • outFormat: "sameLocation" | "testDir"
  • outBaseSrc: Root including src (used when outFormat is testDir)
  • outBaseTest: Root including tests (used when outFormat is testDir)
  • astLibrary: "ts-morph" (future: "babel")
  • testingFramework: "vitest" (future: "jest", etc.)
  • moduleType: "module" (future: "commonjs", etc.)

Commands

Configure

tst configure

Generates tst.config.json. You can also specify fields via flags:

tst configure --provider=openai --model=gpt-4o-mini --outFormat=testDir --outBaseSrc=./src --outBaseTest=./tests --astLibrary=ts-morph --testingFramework=vitest --moduleType=module

Generate

tst generate ./src/utils/math.js                  # Generate unit tests for an entire file
tst generate ./src/utils/math.js --function=add   # Generate unit tests for a specific function

Auth

tst auth set --provider=openai --api-key=$TST_OPENAI_API_KEY
  # Store API key for a specific provider
tst auth status
  # Show current authentication status

Other Commands

tst --help      # Display available commands and usage
tst --version   # Show the current CLI version

Environment Variables

export TST_OPENAI_API_KEY=sk-...

The API key is required to communicate with the LLM.


License

MIT (c) TST-Studio

About

LLM-powered unit test generator CLI by TST-Studio

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •