Skip to content

HarshitSN/AIUnitTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

309 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js AI Pre-commit Hook

An AI-powered pre-commit hook for Node.js projects that analyzes your code changes, suggests improvements, and helps maintain code quality.

Features

  • 🔍 AI-powered code analysis using Groq AI
  • 🧪 Automatic test generation
  • 📊 Web dashboard for visualizing results
  • 🔄 Git integration
  • ⚙️ Configurable rules and thresholds
  • 🛡️ Security vulnerability detection

Installation

Prerequisites

  • Node.js 14 or higher
  • npm or yarn
  • Git

Install as a dev dependency

npm install --save-dev node-ai-precommit-tester
# or
yarn add --dev node-ai-precommit-tester

Set up with Husky (recommended)

  1. Install Husky if you haven't already:

    npx husky-init && npm install
  2. Add the pre-commit hook:

    npx husky add .husky/pre-commit "npx node-ai-precommit"

Environment Variables

  • GROQ_API_KEY: Your Groq API key (required for AI features)
  • AI_PRECOMMIT_DEBUG: Set to 'true' for debug output

Jenkins Pipeline Integration

This tool integrates seamlessly with Jenkins CI/CD pipelines. The pipeline will:

  1. Run the build process
  2. Generate AI-powered unit tests for committed files
  3. Run linting and formatting
  4. Execute the test suite

Setup in Jenkins

  1. Environment Variables: Set up the GROQ_API_KEY environment variable in your Jenkins pipeline:

    environment {
        GROQ_API_KEY = credentials('groq-api-key-id') // Use Jenkins credentials
    }
  2. Pipeline Configuration: The Jenkinsfile included in this repository demonstrates the integration:

    pipeline {
        stages {
            stage('Build') {
                steps { sh 'npm install' }
            }
            stage('Generate AI Tests') {
                steps {
                    // Automatically generates tests for committed JS/TS files
                    script { /* AI test generation logic */ }
                }
            }
            stage('Lint and Format') {
                steps { sh 'npm run lint && npm run format' }
            }
            stage('Test') {
                steps { sh 'npm test' }
            }
        }
    }
  3. Webhook Setup (Optional): Configure Git webhooks to trigger the pipeline on commits.

Usage

Basic Usage

# Stage your changes
git add .

# Run the pre-commit hook
npx node-ai-precommit

# Or commit as usual (will trigger the hook)
git commit -m "Your commit message"

CLI Options

Usage: node-ai-precommit [options]

Options:
  -V, --version      output the version number
  --no-ai            Skip AI analysis
  --no-tests         Skip test generation
  --no-dashboard     Disable web dashboard
  -h, --help         display help for command

Web Dashboard

The web dashboard provides a visual representation of the analysis results:

  • Risk score visualization
  • Code quality metrics
  • Test coverage
  • Security vulnerabilities
  • Suggested improvements

Access it at http://localhost:3000 when the pre-commit hook runs.

Development

  1. Clone the repository

  2. Install dependencies:

    npm install
  3. Build the project:

    npm run build
  4. Link for local development:

    npm link

Contributing

Contributions are welcome! Please read our contributing guidelines before submitting pull requests.

License

MIT © [Your Name]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors