π₯οΈ Running Early CLI Locally
You can run the Early CLI manually on your machine to evaluate test generation before integrating it into your CI/CD. This is useful for:
- Testing how Early handles a specific folder or branch
- Validating your API key and token permissions
- Previewing generated test files in your IDE
- Running offline or experimental workflows
1οΈβ£ Getting Readyβ
Before running the CLI locally, make sure you're set up with the following.
Youβll need these mandatory parameters to run the CLI successfully:
early generate-for-project \
--api-key <your_api_key> \
--token <your_github_token> \
--target-directory <path_to_file_or_directory> \
--ref-name <branch_name>
The --target-directory flag accepts either a directory or a single file. This allows you to generate tests for a specific file instead of scanning an entire folder.
Now, letβs make sure you have everything ready to execute this command.
β 1. Install the CLIβ
Install the CLI globally via npm:
npm install -g @earlyai/cli
π 2. API Key from Earlyβ
You should have received an API key from the Early team.
If not, contact support@startearly.ai to get one.
Store it securely β it will be used as --api-key.
π 3. GitHub Personal Access Token (PAT)β
Create a classic or fine-grained GitHub token to use with --token.
Classic PAT requirements:
- Scope:
repo,read:packages
Fine-grained PAT requirements:
- Access to your repo
- Permissions:
- Contents: Read & Write
- Pull Requests: Read & Write
- Metadata: Read
Create one here:
https://github.com/settings/tokens
Store it securely β it will be passed as --token.
πΏ 4. Create a Dedicated Branchβ
To avoid unintentionally committing generated files to your default branch (e.g., main),
itβs recommended to create a separate branch first:
git checkout -b early-cli-local-run
Then use that branch name with the --ref-name flag.
βΆοΈ Step 2: Run the CLIβ
This command runs test generation on a folder named src/user-usage/ using an API key and token:
early generate-for-project \
--api-key xxxxx \
--token github_pat_123456 \
--target-directory src/user-usage/ \
--ref-name cli-repo-run-1125
Run on a single file
early generate-for-project \
--api-key xxxxx \
--token github_pat_123456 \
--target-directory src/utils/math.ts \
--ref-name cli-repo-single-file
π Step 3: What Logs Will I See?β
Here's a sample log from a successful run:
π§ͺ **Early Pull Request Test Generation Agent**
- Changed files: 11
- Files with testable code: 4
- Total methods detected: 18
- Public methods below coverage threshold: 3
**Action Taken**
β
Unit tests automatically generated for 3 public functions below threshold.
π¦ Test files have been auto-committed to this branch.
π PR coverage before: 71%. PR coverage after: 91%.
π¦ Where Are My Test Files?β
If you leave --auto-commit at its default (true), test files are committed directly to your current branch. You can see them in your Git history or IDE.
To find them manually:
- Check the
tests/folder (if you're usingrootFolderstructure) - Or look beside the original source files (for
siblingFolderstructure)
π Want to See Files in the IDE Before Committing?β
Use the same command with --auto-commit false:
early generate-for-project \
--api-key xxxxx \
--token github_pat_123456 \
--target-directory src/user-usage/ \
--ref-name cli-repo-run-1125 \
--auto-commit false
This will leave the generated test files unstaged so you can explore or edit them in your IDE before committing manually.
β Run a Quick Test Firstβ
To begin, try pointing the CLI at one file in your project β for example:
early generate-for-project \
--api-key <your_api_key> \
--token <your_token> \
--target-directory src/utils/math.ts \
--ref-name my-local-run
This helps validate that everything is configured correctly before scanning larger areas.
β See an error?β
Check out the Troubleshooting Guide in this chapter for help resolving:
- CLI not installing
- Missing API key or token
- Memory issues
- Push conflicts
- And more...
π΅οΈ No tests were generated?β
If the CLI runs but you donβt see any tests created, it could be due to a number of reasons β
see Why No Tests Were Generated β for a step-by-step checklist.