DEV Community

Cover image for How I Used an AI Agent to "Enforce" 70% Unit Test Coverage for 3,000 Users
Pau Dang
Pau Dang

Posted on

How I Used an AI Agent to "Enforce" 70% Unit Test Coverage for 3,000 Users

Hello everyone, I'm back!

Following my previous post on Zero-Downtime, my CLI tool nodejs-quickstart-structure has fortunately reached nearly 3,000 downloads. This milestone is both a joy and a pressure. The pressure lies in: How do I ensure that every project initialized from the tool is truly "Production-Ready"?

The answer doesn't lie in boilerplate code, but in Unit Testing. However, instead of just manually writing tests, I tried a more "trendy" approach: Building an AI Testing Agent to establish a Quality Gate for the entire project.


1. The Pain: "I'll Write Tests Later!" ... and the Reality

We all know Unit Testing is important. But in reality, when starting a new Node.js project:

  • Setting up Jest and ts-jest takes time.
  • Mocking Mongoose, Redis, and Express Request/Response is incredibly cumbersome.
  • Defining a tests/ folder structure that follows Clean Architecture is a hassle.

The result? We often tell ourselves: "Let's finish the logic first, then write tests later." And that "later" usually never comes. With 3,000 users relying on my tool, I couldn't let that happen.


2. The Solution: From AI Chatbot to AI Agent (Thinking in Loops)

Instead of just using AI to "ghostwrite" a few isolated test snippets, I built a Testing Agent Skill. Unlike a simple Chatbot, this Agent operates based on a Standard Operating Procedure (SOP):

The "Skill" Framework I established for the Agent:

  • Role: Senior QA & Testing Architect.
  • Standards: Utilize the AAA Pattern (Arrange - Act - Assert), 100% Isolation (never touch the real database).
  • Goal: Enforce a minimum coverage threshold of 70% Lines and 70% Functions.
  • Protocol: Must list Scenarios (Happy/Sad/Edge cases) before writing any code.

Git file: https://github.com/paudang/nodejs-quickstart-structure/blob/main/docs/testing-agent-standards.md


3. Implementation: Turning "Senior Mindset" into Automation

I integrated this logic directly into the project through a system of Templates (.ejs) and smart path handling.

The Automatic "Quality Gate" Mechanism:

When you use the command to initialize a project:

  1. The tool automatically scans the src/ structure (Controllers, Services, Resolvers).
  2. The AI Agent suggests standard Mocks for Mongoose (e.g., admin.ping failure) or GraphQL Context.
  3. All .spec.ts files are generated into the tests/ directory with the corresponding structure.

Fun fact: During development, I encountered several "missing file extension" bugs due to manual path handling. I eventually refactored everything to use path.parse() so the tool can run flawlessly across Windows, MacOS, and Linux.


4. Results: Numbers Don't Lie

Here is the Coverage report after I verified all scenarios for version v1.14.0:

Category Coverage Requirement Status
Lines 78.5% > 70% ✅ PASS
Functions 82.1% > 70% ✅ PASS
Branches 70.2% > 65% ✅ PASS

Now, every user downloading nodejs-quickstart-structure has no excuse to skip testing. The hardest "skeleton" has already been prepared by the AI.


5. Lessons Learned: AI Doesn't Replace Us, It Levels Us Up

Using an AI Agent to build Testing standards taught me one thing: AI is most powerful when placed within a standard Workflow.

If you're struggling to set up a professional Node.js project, give my tool a try:

npx nodejs-quickstart-structure init
Enter fullscreen mode Exit fullscreen mode

And remember: "Quality is not an act, it is a habit."

Conclusion

My next goal is the 5,000 downloads milestone. If you find this approach useful, please leave a Star on GitHub or comment with your thoughts on how you handle Unit Testing!

GitHub Repo: https://github.com/paudang/nodejs-quickstart-structure


Thanks for reading! I hope this sharing gives you a little more motivation in your "struggle" with Unit Testing.

Top comments (0)