npm i -D @n1k1t/unit-generator
The unit-generator wraps the Keploy CLI util to make generation easier and more convenient in NodeJS projects In general, it uses an AI model to generate a unit test that should reach a higher coverage rate than it actually has. While generation AI prompts will be generated by Keploy based on an actual coverage. Using the unit-generator you can choose several modules to work with by provided tools like pattern files searching or ignore files
- Install Keploy
- Get access to API key on the OpenAI platform
- Allow access to model gpt-o4-mini
- Make
.env
file in the root of project that contents:
UNIT_GENERATOR_TEST_COMMAND="npx jest"
UNIT_GENERATOR_API_KEY="...API_KEY"
UNIT_GENERATOR_MODEL="gpt-4o-mini"
- Setup project with Jest
{
// Cobertura reporter is required
"coverageReporters": ["cobertura"],
// Recommend
"collectCoverageFrom": ["<rootDir>/src/**/*.ts"],
// Recommend
"passWithNoTests": true,
}
- Install the @n1k1t/unit-generator
- Run
npx unit calculate
command
$ npx unit calculate
┌─────────┬───────┬────────────────────────┐
│ (index) │ rate │ updated │
├─────────┼───────┼────────────────────────┤
│ 0 │ 0.689 │ '20.03.2025, 19:48:31' │
└─────────┴───────┴────────────────────────┘
- Done!
$ npx unit -h
Usage: cli [options] [command]
It generates unit-tests by Jest coverage
Options:
-h, --help display help for command
Commands:
summary [options] Returns an overall coverage in the project
calculate Calculates a coverage of the whole project and returns summary
analyze [options] [pattern] Returns a table of low covered project files
generate [options] [pattern] Generates unit tests
help [command] display help for command
$ npx unit summary -h
Usage: cli summary [options]
Returns an overall coverage in the project
Options:
-f --format [table|number] Output format (default: "table")
-h, --help display help for command
Examples
$ npx unit summary
┌─────────┬───────┬────────────────────────┐
│ (index) │ rate │ updated │
├─────────┼───────┼────────────────────────┤
│ 0 │ 0.689 │ '20.03.2025, 19:48:31' │
└─────────┴───────┴────────────────────────┘
$ npx unit summary -f number
0.689
$ npx unit calculate -h
Usage: cli calculate [options]
Calculates a coverage of the whole project and returns summary
Options:
-h, --help display help for command
Examples
$ npx unit calculate
┌─────────┬───────┬────────────────────────┐
│ (index) │ rate │ updated │
├─────────┼───────┼────────────────────────┤
│ 0 │ 0.689 │ '20.03.2025, 20:11:16' │
└─────────┴───────┴────────────────────────┘
$ npx unit analyze -h
Usage: cli analyze [options] [pattern]
Returns a table of low covered project files
Options:
-t, --target [value] Desired coverage target of an each file (default: "0.8")
-l --limit [value] Files limit (default: "5")
-a --all Takes all paths provided by pattern (default: false)
-h, --help display help for command
Examples
$ npx unit analyze -l 3 src/utils
┌─────────┬────────────────────────────────────────────────────────┬───────┐
│ (index) │ file │ rate │
├─────────┼────────────────────────────────────────────────────────┼───────┤
│ 0 │ 'src/utils/streams/text-transform.ts' │ 0.107 │
│ 1 │ 'src/utils/streams/sse.ts' │ 0.316 │
│ 2 │ 'src/utils/dto/validators/has-dates-diff.validator.ts' │ 0.421 │
└─────────┴────────────────────────────────────────────────────────┴───────┘
$ npx unit generate -h
Usage: cli generate [options] [pattern]
Generates unit tests
Options:
-t, --target [value] Desired coverage target of an each file (default: "0.8")
-m, --model [value] AI model to use for unit tests generation (default: "gpt-4o-mini")
-i, --iterations [value] Iterations maximum of unit tests generation (default: "5")
-l --limit [value] Files limit (default: "5")
-v --verbose Replaces pretty table with a raw Keploy as output (default: false)
-a --all Takes all paths provided by pattern (default: false)
-h, --help display help for command
Examples
$ npx unit generate src/utils/dto/validators
┌─────────┬────────────────────────────────────────────────────────────────┬───────┬────────┬────────┬─────────┐
│ (index) │ file │ rate │ target │ status │ spent │
├─────────┼────────────────────────────────────────────────────────────────┼───────┼────────┼────────┼─────────┤
│ 0 │ 'src/utils/dto/validators/has-dates-diff.validator.ts' │ 1 │ 0.8 │ 'DONE' │ 166.256 │
│ 1 │ 'src/utils/dto/validators/has-pattern-properties.validator.ts' │ 1 │ 0.8 │ 'DONE' │ 154.54 │
│ 2 │ 'src/utils/dto/validators/has-valid-timestamp.ts' │ 0.636 │ 0.8 │ 'DONE' │ 137.068 │
└─────────┴────────────────────────────────────────────────────────────────┴───────┴────────┴────────┴─────────┘
# Desired coverage target of an each file
export UNIT_GENERATOR_COVERAGE_TARGET = "0.8"
# Path to the cobertura-coverage.xml file generated
export UNIT_GENERATOR_COBERTURA_PATH="coverage/cobertura-coverage.xml"
# Iterations maximum of unit tests generation
export UNIT_GENERATOR_MAX_ITERATIONS="5"
# Command that will be used to generate unit tests
export UNIT_GENERATOR_TEST_COMMAND="npm test"
# AI platform key
export UNIT_GENERATOR_API_KEY="..."
# AI model to use for unit tests generation
export UNIT_GENERATOR_MODEL="gpt-4o-mini"
The unit-generator package supports .unitignore
file that can contain path matches using minimatch expressions
Examples
src/**/*.module.ts
src/**/index.ts
lib/**