RSS Amplifier

Pankaj’s Substack · Feb 25, 2025

From Code to Chaos: How My AI Agent Automates Testing

0
Sign in to vote or save

Pankaj · Pankaj’s Substack

I built an AI-powered testing machine using LangChain.js. My goal? To eliminate the boring, repetitive work of writing and running tests. Instead of manually creating test cases, I let my AI agent handle everything—writing Jest tests, saving them, and running them automatically.

And to push things further, I tested it inside a NestJS project within a Turbo Repo. Here’s how I did it.

I started by installing the necessary tools:

npm install @langchain/openai @langchain/core langchain dotenv jest

I stored my OpenAI API key in a .env file and wrote an AI agent that could:

  1. Generate Jest tests from existing code.

  2. Save the generated tests to a file.

  3. Run Jest and report the results.

Here’s the core of my AI agent:

// agent.js
require('dotenv').config();
const { ChatOpenAI } = require("@langchain/openai");
const { initializeAgentExecutorWithOptions } = require("langchain/agents");
const { DynamicTool } = require("@langchain/core/tools");
const fs = require('fs').promises;
const { exec } = require('child_process');
const util = require('util');
const execPromise = util.promisify(exec);
async function createTestAgent() {
  const model = new ChatOpenAI({
    modelName: "gpt-4",
    temperature: 0.5,
    openAIApiKey: process.env.OPENAI_API_KEY,
  });
  const tools = [
    new DynamicTool({
      name: "GenerateTests",
      description: "Generates Jest tests from code or requirements.",
      func: async (input) => {
        const prompt = `Generate Jest tests for: "${input}". Return only the code.`;
        const response = await model.call([{ role: "user", content: prompt }]);
        return response.text;
      },
    }),
    new DynamicTool({
      name: "SaveFile",
      description: "Saves content to a file.",
      func: async ({ content, path }) => {
        await fs.writeFile(path, content);
        return `Saved to ${path}`;
      },
    }),
    new DynamicTool({
      name: "RunJest",
      description: "Runs Jest tests.",
      func: async (dir) => {
        const { stdout } = await execPromise(`npx jest ${dir}`);
        return stdout;
      },
    }),
  ];
  return await initializeAgentExecutorWithOptions(tools, model, {
    agentType: "chat-conversational-react-description",
    verbose: true,
  });
}
module.exports = { createTestAgent };

I tested it on a simple function:

// add.js
function add(a, b) {
  return a + b;
}
module.exports = { add };

I then wrote a script to ask my AI agent to generate and run Jest tests for this function:

// daily-run.js
const { createTestAgent } = require('./agent');
async function runDailyTask() {
  const agent = await createTestAgent();
  const task = `
    Generate Jest tests for a function 'add(a, b)' that adds two numbers.
    Save them to 'add.test.js'.
    Run the tests and tell me the results.
  `;
  const response = await agent.call({ input: task });
  console.log(response.output);
}
runDailyTask().catch(console.error);

The AI generated these Jest tests:

// add.test.js
describe('add function', () => {
  test('adds two positive numbers', () => {
    expect(add(2, 3)).toBe(5);
  });
  test('adds a positive and negative', () => {
    expect(add(2, -3)).toBe(-1);
  });
});

I ran the tests, and they passed! 🎯

Now, I wanted to push my AI agent further. I used it inside a NestJS project within a Turbo Repo.

Here’s a simple NestJS service inside my monorepo’s packages/api folder:

// packages/api/src/math/math.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class MathService {
  add(a: number, b: number): number {
    return a + b;
  }
}

I modified my run script to work with Turbo Repo:

// daily-run-nest.js
const { createTestAgent } = require('./agent');
async function runNestTask() {
  const agent = await createTestAgent();
  const task = `
    Generate Jest tests for a NestJS service method 'add(a: number, b: number): number' that adds two numbers.
    Save them to 'packages/api/src/math/math.service.spec.ts'.
    Run the tests in 'packages/api' using Turbo with 'npx turbo run test --filter=api'.
    Tell me the results.
  `;
  const response = await agent.call({ input: task });
  console.log(response.output);
}
runNestTask().catch(console.error);

I also updated my Jest tool to run tests inside Turbo Repo:

new DynamicTool({
  name: "RunJest",
  description: "Runs Jest tests with Turbo in a monorepo.",
  func: async (dir) => {
    const { stdout } = await execPromise(`npx turbo run test --filter=api`, { cwd: dir });
    return stdout;
  },
});

The AI generated this NestJS test file:

// packages/api/src/math/math.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { MathService } from './math.service';
describe('MathService', () => {
  let service: MathService;
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [MathService],
    }).compile();
    service = module.get<MathService>(MathService);
  });
  it('should add two positive numbers', () => {
    expect(service.add(2, 3)).toBe(5);
  });
  it('should add a positive and negative number', () => {
    expect(service.add(2, -3)).toBe(-1);
  });
});

I ran the test using Turbo, and it passed successfully. ✅

This AI-powered workflow saved me hours by handling test creation and execution automatically. Here’s what I learned:

Fast & automated – No manual test writing needed.
Context-aware – Remembers past interactions to improve testing.
Handles NestJS + Turbo Repo – Works in complex monorepos.

⚠️ More edge cases – Needs a push to test for rare conditions.
⚠️ Better monorepo handling – Can be optimized for bigger projects.

Next, I plan to make my AI agent even smarter:
🔹 Automatically detect and fix failing tests.
🔹 Generate test reports and suggest improvements.
🔹 Integrate with CI/CD pipelines for a fully automated workflow.

Want to try it? Use my code, plug it into your project, and let your AI agent take over testing! 🚀

No posts

Read the original on 10xengineering.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.