UN

unit-tests

Automated support for writing, maintaining, and fixing unit tests for Shared Reference Data with 90% coverage mandates.

Install

mkdir -p .claude/skills/unit-tests && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/14913" && unzip -o skill.zip -d .claude/skills/unit-tests && rm skill.zip

Installs to .claude/skills/unit-tests

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Expert unit test engineer for MMO Shared Reference Data. Use when: writing unit tests, updating tests for code changes, fixing failing tests, improving code coverage, fixing SonarQube issues.
191 chars✓ has a “when” trigger
Intermediate

Key capabilities

  • Write unit tests for new or modified code
  • Fix failing unit tests after code changes
  • Maintain 90% code coverage for statements, branches, functions, and lines
  • Fix SonarQube issues and code smells
  • Mock external API dependencies (e.g., Boomi/CEFAS API)
  • Mock Azure Service Bus client interactions

How it works

The skill guides the creation and maintenance of unit tests using Jest and Sinon, providing specific mocking patterns for external dependencies and enforcing a 90% code coverage target.

Inputs & outputs

You give it
New or modified code, failing tests, or SonarQube reports
You get back
Passing unit tests, 90% code coverage, and resolved SonarQube issues

When to use unit-tests

  • Writing unit tests for new code
  • Fixing failing tests
  • Improving test coverage to 90%
  • Resolving SonarQube code smells

About this skill

Shared Reference Data — Unit Tests Skill

Expert in writing and maintaining unit tests for the MMO Shared Reference Data library. This shared library enforces tiered coverage targets.

When to Use

  • Writing unit tests for new or modified code
  • Fixing failing tests after code changes
  • Maintaining the tiered coverage targets below
  • Fixing SonarQube issues or code smells

Coverage Requirements

Tiered targets (aligned with copilot-instructions quality gates) — never drop below the SonarCloud baseline:

  • ≥90% global — Statements, Functions, Lines, Branches
  • ≥95% — core business logic (query functions, transformations, risk scoring, weight/overuse validation)
  • 100% — error-handling and security-critical paths (OAuth2/API failures, Service Bus publish failures, input/date validation)
  • Run tests: npm test (single run with coverage report)
  • Watch mode: npm run test:watch

Test Framework & Tools

  • Jest as test runner with ts-jest
  • sinon for stubs and spies
  • Test files in test/ directory mirroring src/ structure

Mocking Patterns

Boomi/CEFAS API (OAuth2)

// Mock both success and dual error handling
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;

// Success
mockedAxios.post.mockResolvedValueOnce({ data: { access_token: 'token' } });
mockedAxios.get.mockResolvedValueOnce({ data: mockApiData });

// Error with response (HTTP error)
mockedAxios.get.mockRejectedValueOnce({
  response: { status: 500, data: 'Server Error' },
});

// Error without response (network error)
mockedAxios.get.mockRejectedValueOnce(new Error('ECONNREFUSED'));

Azure Service Bus (Class-Based Mock)

// Class-based mocks for Service Bus client
const mockSender = {
  sendMessages: jest.fn().mockResolvedValue(undefined),
  close: jest.fn().mockResolvedValue(undefined),
};
const mockSbClient = {
  createSender: jest.fn().mockReturnValue(mockSender),
  close: jest.fn().mockResolvedValue(undefined),
};
jest.mock('@azure/service-bus', () => ({
  ServiceBusClient: jest.fn().mockImplementation(() => mockSbClient),
}));

Filesystem Fallback (Queue Dev Mode)

jest.mock('fs/promises', () => ({
  writeFile: jest.fn().mockResolvedValue(undefined),
  mkdir: jest.fn().mockResolvedValue(undefined),
}));

Date Mocking for CEFAS

// CEFAS requires UTC dates in YYYY-MM-DD format
jest.spyOn(Date, 'now').mockImplementation(() => 1693751375000);
// Verify: moment.utc().format('YYYY-MM-DD') === '2023-09-03'

What to Test

  1. Barrel exports — verify all public API is exported from src/index.ts
  2. Boomi OAuth2 — token fetch, API call with bearer, dual error handling (e.response + direct)
  3. Service Bus — send with sessionId, sender/client cleanup in finally, filesystem fallback
  4. Weight validation — overuse with 10% tolerance (estimated) and fixed buffer (actual), boundary values
  5. AJV validation — valid/invalid payloads, schema error reporting
  6. Type interfaces — ensure type guards match interface definitions
  7. Legacy SSLSSL_OP_LEGACY_SERVER_CONNECT option set on HTTPS agent
  8. Date formattingmoment.utc() produces correct YYYY-MM-DD for CEFAS
  9. Logging — bracketed format with Bunyan

SonarQube Issue Resolution

When fixing SonarQube issues, NEVER modify functionality. If existing tests fail after a fix, revert it. Only structural refactoring is allowed.

Workflow

  1. Identify the source file(s) that need tests
  2. Find existing test file or create new one mirroring src/test/ path
  3. Read the source code to understand ALL branches — cover every path to meet the tiered targets
  4. Write tests for every code path including error branches (error-handling/security paths need 100%)
  5. Run npm test and verify coverage meets the tiered targets across all metrics
  6. If any metric is below target, identify uncovered lines and add tests
  7. Check problems tab for SonarQube issues

When not to use it

  • When the code is not part of the MMO Shared Reference Data library
  • When the task is not related to unit testing or code quality
  • When modifying functionality to fix SonarQube issues

Limitations

  • Specific to the MMO Shared Reference Data library
  • Requires Jest as test runner and Sinon for stubs/spies
  • Does not allow modifying functionality to fix SonarQube issues

How it compares

This approach enforces a high standard of code quality and reliability by mandating 90% test coverage and providing specific mocking strategies for common external services, unlike less rigorous testing methodologies.

Compared to similar skills

unit-tests side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
unit-tests (this skill)05moReviewIntermediate
dependency-upgrade265moReviewIntermediate
lint-and-validate66moReviewBeginner
customaize-agent:create-hook05moReviewIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry