Skip to content

[Feature]: Add Decorator-Based API Testing Support for Enhanced #241

@json-choi

Description

@json-choi

Current Situation/Problem

Currently, itdoc provides excellent test-driven API documentation generation through its function-based approach using describeAPI() and itDoc(). However, this approach requires developers to write verbose boilerplate code and manually integrate with test frameworks

Proposed Feature

Introduce decorator-based API documentation support that allows developers to add documentation decorators to existing E2E tests in their preferred test frameworks, providing a seamless way to generate API documentation from existing test suites:

// Proposed decorator-based approach - enhancing existing E2E tests
import { ApiDoc, DocumentEndpoint } from 'itdoc/decorators'
import request from 'supertest'
import { app } from '../src/app'

describe('User Authentication API', () => {
    
    @ApiDoc({
        method: "POST",
        path: "/signup",
        summary: "User Registration API",
        description: "Register a new user with username and password",
        tag: "Auth"
    })
    it('should register user successfully', async () => {
        const response = await request(app)
            .post('/signup')
            .send({
                username: 'testuser',
                password: 'password123'
            })
            .expect(201)
        
        expect(response.body).toEqual({
            message: 'User registered successfully',
            userId: expect.any(String)
        })
    })

    @ApiDoc({
        method: "POST",
        path: "/signup",
        summary: "User Registration API - Validation Error",
        description: "Returns error when username is missing",
        tag: "Auth"
    })
    it('should fail when username is missing', async () => {
        const response = await request(app)
            .post('/signup')
            .send({
                password: 'password123'
            })
            .expect(400)
        
        expect(response.body).toEqual({
            error: 'Username is required'
        })
    })
})

// Works with any test framework
describe('Product API', () => {
    
    @DocumentEndpoint({
        method: "GET",
        path: "/products/:id",
        summary: "Get Product by ID",
        tag: "Products"
    })
    test('should return product details', async () => {
        // Existing Jest/Mocha/Vitest test code
        const response = await request(app)
            .get('/products/123')
            .expect(200)
        
        expect(response.body.id).toBe(123)
        expect(response.body.name).toBeDefined()
    })
})

Benefits of decorator-based approach:

  1. Non-intrusive integration: Add documentation to existing E2E tests without changing test logic
  2. Framework agnostic: Works seamlessly with any test framework (Jest, Mocha, Vitest, etc.)
  3. Minimal learning curve: Developers can keep using their familiar testing patterns
  4. Better IDE support: Enhanced autocomplete, IntelliSense, and refactoring capabilities
  5. Gradual adoption: Teams can incrementally add documentation decorators to existing tests
  6. Automatic documentation generation: Extract API specs directly from real, working tests
  7. Test-driven documentation: Documentation is always in sync with actual test behavior

Alternatives (Optional)

No response

Additional Information

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions