npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ai-test-gen-angular

v1.0.2

Published

ai-test-gen-angular is a powerful tool that leverages OpenAI's advanced capabilities to automate the generation of unit tests for your Angular components and services

Downloads

717

Readme

ai-test-gen-angular

ai-test-gen-angular is a powerful tool that leverages OpenAI's advanced capabilities to automate the generation of unit tests for your Angular components and services. This innovative package intelligently analyzes your codebase and produces comprehensive test cases, saving you time and effort in the testing process. Seamlessly integrate it into your Angular projects and watch as it enhances your development workflow by ensuring robust and reliable code.

Key Features

  1. Intelligent Test Generation: ai-test-gen-angular intelligently generates unit tests for Angular components and services. While it provides a solid foundation, manual adjustments may be required to ensure the generated tests are executable and meet your specific needs.

  2. Time and Effort Savings: Automating test creation with ai-test-gen-angular significantly reduces the time and effort spent on writing tests. However, it's important to note that some manual fine-tuning may be necessary to achieve optimal results.

  3. Enhanced Code Quality: The tool offers comprehensive test coverage, helping to improve code reliability. While it's a powerful aid, developers should be aware that the generated tests may require adjustments to be fully effective.

Setup

To get started with ai-test-gen-angular, follow these simple steps:

  1. Install the package via npm:
npm i ai-test-gen-angular
  1. Set Open API key in environment variable.
# if you want to use openai then do this
export OPENAI_API_KEY=somekey
# if you want to use aws bedrock then do this (you should use the flag --aws-bedrock while running the command)
export AWS_ACCESS_KEY=****************
export AWS_SECRET_KEY=****************
export AWS_REGION=*********
  1. Generate Unit Tests (run this command in the root folder of your project)
node ./node_modules/ai-test-gen-angular/index.js "relative/path/to/service/or/somecomponent.component.ts" "relative/path/to/tsconfig.json" [--read-from-index-files] [--aws-bedrock] [-p] [custom prompt (for e.g: write unit test for the function addTasks())]

IMPORTANT: Please note that you need to give relative path from the project root

  • to your component/service file
  • and also to the tsconfig.json file. (if you have multiple tsconfig.json file, then please use that file in which you have mentioned the alias path for your subfolders)

Restrict scanning for selected files

if you want to exclude some files from being scanned then you can add a comment at the top of that file
// EXCLUDE_AI_TEST_GEN_SCAN

Command Usage

node ./node_modules/ai-test-gen-angular/index.js "relative/path/to/some-filename.component.ts" "./tsconfig.json" [--read-from-index-files] [--aws-bedrock] [-p] [custom prompt (for e.g: write unit test for the function addTasks())]

Description

Generates Angular unit tests for the specified TypeScript file using AI-based testing techniques.

Arguments

  • <relative/path/to/component/or/service/ts-file>
    The relative path to the TypeScript file of the component or service for which you want to generate tests.

  • <relative/path/to/tsconfig-file>
    The relative path to the tsconfig.json file that configures your Angular project.

Options

  • --read-from-index-files
    Use this flag to instruct the tool to generate tests by reading models/enums exported via index.ts files.

  • --aws-bedrock If you want to use AWS Bedrock anthropic claude instead of openAI, then use this flag. You need to set 3 environment variables to use this flag

  • -p If you want to ask a custom prompt for generating any specific unit test

export AWS_ACCESS_KEY=****************
export AWS_SECRET_KEY=****************
export AWS_REGION=*********

Example

node ./node_modules/ai-test-gen-angular/index.js src/app/pipes/get-number-suffix.pipe.ts ./tsconfig.base.json
node ./node_modules/ai-test-gen-angular/index.js src/app/pipes/get-number-suffix.pipe.ts ./tsconfig.base.json -p "Write unit test for negative scenarios"

Input file

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: "getNumberSuffix",
  pure: false,
})
export class GetNumberSuffixPipe implements PipeTransform {
  transform(number: number) {
    let suffix;

    if (number >= 11 && number <= 13) {
      suffix = "th";
    } else {
      const lastDigit = number % 10;
      switch (lastDigit) {
        case 1:
          suffix = "st";
          break;
        case 2:
          suffix = "nd";
          break;
        case 3:
          suffix = "rd";
          break;
        default:
          suffix = "th";
          break;
      }
    }

    return suffix;
  }
}

Generated output test case file

File is generated in same folder as of its input file : src/app/pipes/get-number-suffix.pipe.ai-test-gen.spec.ts

import { GetNumberSuffixPipe } from "./get-number-suffix.pipe";

describe("GetNumberSuffixPipe", () => {
  let pipe: GetNumberSuffixPipe;

  beforeEach(() => {
    pipe = new GetNumberSuffixPipe();
  });

  it("should create an instance", () => {
    expect(pipe).toBeTruthy();
  });

  it('should return "th" for numbers between 11 and 13', () => {
    expect(pipe.transform(11)).toBe("th");
    expect(pipe.transform(12)).toBe("th");
    expect(pipe.transform(13)).toBe("th");
  });

  it('should return "st" for numbers ending with 1', () => {
    expect(pipe.transform(1)).toBe("st");
    expect(pipe.transform(21)).toBe("st");
    expect(pipe.transform(31)).toBe("st");
  });

  it('should return "nd" for numbers ending with 2', () => {
    expect(pipe.transform(2)).toBe("nd");
    expect(pipe.transform(22)).toBe("nd");
    expect(pipe.transform(32)).toBe("nd");
  });

  it('should return "rd" for numbers ending with 3', () => {
    expect(pipe.transform(3)).toBe("rd");
    expect(pipe.transform(23)).toBe("rd");
    expect(pipe.transform(33)).toBe("rd");
  });

  it('should return "th" for all other numbers', () => {
    expect(pipe.transform(4)).toBe("th");
    expect(pipe.transform(10)).toBe("th");
    expect(pipe.transform(20)).toBe("th");
    expect(pipe.transform(30)).toBe("th");
    expect(pipe.transform(40)).toBe("th");
    expect(pipe.transform(100)).toBe("th");
  });
});

Example 2

Reading data from index files

node ./node_modules/ai-test-gen-angular/index.js src/app/pipes/get-number-suffix.pipe.ts ./tsconfig.base.json --read-from-index-files

Note

For better result you can store your class models, types, enum, interface in files with an extension below

We scan files with below extensions

  1. .model.ts
  2. .enum.ts
  3. .interface.ts
  4. .type.ts

and folders with naming conventin

  1. models/
  2. enums/
  3. interfaces/
  4. types/

Please be aware that the generated file will not be directly executable but hopefully it generates a code snippet to lessen the effort of the developer. In some cases, the file may contain superfluous or absent characters at its start or end. In such cases, it may be necessary to perform manual adjustments. This is a natural aspect of utilizing generative AI technology.