Writing unit tests is a best practice, but for many developers, it’s also repetitive, time-consuming, and easy to deprioritize as projects grow. In TypeScript backend projects, keeping Jest test coverage high often means spending significant time writing boilerplate rather than focusing on actual business logic.
To address this, I built ts-genai-test package, an AI-powered Jest test case generator for TypeScript (Node.js) that automatically generates meaningful unit tests using configurable Generative AI providers.
🔗 GitHub: testcase-gen-ai-ts
📦 npm: ts-genai-test
🧠 The Problem
Most developers face the same challenges when writing unit tests:
- Writing repetitive Jest boilerplate
- Missing edge cases due to time pressure
- Tests becoming outdated after refactors
- Spending more time testing than coding
Traditional test generators can scaffold files, but they don’t understand the intent of a function. Generative AI, on the other hand, can reason about function signatures and expected behavior, making it a natural fit for unit test generation. Let's get curious about it!
🤖 What is ts-genai-test?
ts-genai-test is a developer tool that:
- Automatically generates Jest unit test cases
- Works with TypeScript (Node.js) projects
- Supports multiple AI providers
- Allows configuration of model name and API key
- Produces ready-to-run .test.ts files
- Is designed to integrate cleanly into CI/CD workflows
The project is written in TypeScript, packaged using pnpm, and focuses on being simple, extensible, and developer-friendly.
✨ Key Features
✅ AI-Generated Jest Test Cases
Point the tool to a TypeScript file or function, and it generates Jest test cases automatically.
import path from "path";
import { generateTests, functionalTypes } from "ts-genai-test";
const inputPrompt: functionalTypes.PromptInput[] = [
{
outputTestDir: path.resolve(__dirname, "../__tests__"), // optional test suite directory, defaults to 'tests' folder
folderPath: path.resolve(__dirname, "../src"), // source folder
filePath: path.resolve(__dirname, "../src/index"), // source file
functionName: "add", // function to generate tests for
testFileName: "" // optional custom test file name
rootPath: "" // optional if outputTestDir is provided else its mandatory to form tests folder default path
}
];
await generateTests(inputPrompt);
The generated output is clean, readable, and ready to execute—no manual cleanup required.
🧩 Override Existing Test Cases (Flag-Based Control)
By default, ts-genai-test is designed to preserve existing test files to avoid accidental overwrites. However, there are scenarios—such as refactoring or regenerating tests—where developers may want to rewrite existing test cases.
To support this, the tool provides an override test cases flag. When this flag is enabled:
- Existing .test.ts files are explicitly overwritten
- Previously generated or manually written tests can be replaced with newly generated ones
- Developers retain full control over when regeneration is allowed
This flag-based approach ensures that test overwriting is intentional, explicit, and developer-controlled, reducing the risk of unintended changes while still enabling regeneration workflows.
This design reinforces the tool’s philosophy: AI assists test creation, but developers remain in control of the final output.
⚙️ Wide & Flexible AI Configuration
One of the core design goals of this project is configurability. You can configure model details (recommended via environment variables):
- AI provider (OpenAI, Gemini, Groq, etc. in lowercase)[AI_MODEL=gemini]
- Model code (or name) [AI_MODEL_NAME=gemini-2.5-flash]
- API key [AI_API_KEY=*******]
- Retry behavior for failed AI calls
This allows teams to switch providers easily, control costs, and future-proof their workflows as AI ecosystems evolve.
🔄 Built-In Retry Mechanism
AI calls can occasionally fail due to network issues or incomplete responses. To improve reliability, ts-genai-test includes a one-time retry mechanism that attempts regeneration before failing—making it safer to use in automated environments like CI pipelines.
🗂 Automatic Test Directory Handling
If the provided target test directory does not exist, the tool creates it automatically. This reduces setup friction and keeps the developer experience smooth.
🧪 Example
Given a simple utility function:
export function add(a: number, b: number): number {
return a + b;
}
The tool generates a Jest test like:
test("adds two numbers", () => {
expect(add(2, 3)).toBe(5);
});
Simple, readable, and immediately executable.
⚠️ Limitations
While ts-genai-test helps reduce the effort required to write unit tests, it is important to understand its current limitations:
- Generated test cases should be reviewed before production use. AI-generated output is intended to assist developers, not to be used blindly.
- Functions with complex business logic may require manual adjustments to ensure correctness and adequate coverage.
- This tool assists developers; it does not replace human-written tests. Developer judgment remains essential for validating intent and edge cases.
- Currently, only functional (unit-level) API test cases are supported. REST API testing, Swagger/OpenAPI-based test generation, and end-to-end API scenarios are not supported at this stage.
These limitations define the current scope of the project and also highlight clear opportunities for future enhancements.
🤝 Open Source & Collaboration
ts-genai-test is fully open source and welcomes collaboration. Contributions are especially welcome in areas like:
- Supporting additional AI providers
- Improving prompt quality
- Adding support for other test frameworks
- Exploring REST or Swagger-based test generation
- Handle unsupported languages and malformed code
- Support for non-TypeScript files
Documentation and examples
🔗 GitHub: testcase-gen-ai-ts GitHub Repo
📦 npm: ts-genai-test
🧰 Tech Stack
- TypeScript
- Node.js
- pnpm (v10.24.0)
- Jest
- Generative AI (LLM-based test generation)
📦 Installation
Using pnpm (recommended):
pnpm install ts-genai-test
or using npm
npm install ts-genai-test
📌 Final Thoughts
This project explores how Generative AI can assist developers in writing better unit tests with less effort. While it does not replace thoughtful test design, it significantly reduces boilerplate and accelerates development.
If you’re working on TypeScript backends and spending too much time writing Jest tests, this tool might help and contributions are always welcome.
Top comments (0)