Back to Repositories

Testing API Generation Command Implementation in iptv-org/iptv

This test suite validates the API generation functionality in the IPTV project, specifically focusing on the streams.json file creation process. It ensures proper file generation and content matching through automated testing.

Test Coverage Overview

The test suite provides focused coverage of the API generation command, specifically targeting the streams.json output.

Key areas covered include:
  • File system operations and directory preparation
  • Command execution with environment variables
  • JSON file generation and content validation
  • Output matching against expected results

Implementation Analysis

The testing approach utilizes Jest’s beforeEach hooks for consistent test environment setup and cleanup.

Key implementation patterns include:
  • Direct command execution using child_process
  • File system manipulation with fs-extra
  • Environment variable configuration for test isolation
  • JSON content comparison using Jest matchers

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • fs-extra for enhanced file operations
  • child_process for command execution
  • TypeScript for type safety
  • Custom helper functions for file content reading
  • Environment variable configuration for test paths

Best Practices Demonstrated

The test implementation showcases several testing best practices for API generation validation.

Notable practices include:
  • Isolated test environment through directory cleanup
  • Consistent test data management
  • Environment variable isolation
  • Reusable utility functions
  • Clear test case organization
  • Type-safe implementation with TypeScript

iptv-org/iptv

tests/commands/api/generate.test.ts

            
import { execSync } from 'child_process'
import fs from 'fs-extra'

beforeEach(() => {
  fs.emptyDirSync('tests/__data__/output')

  execSync(
    'STREAMS_DIR=tests/__data__/input/streams_generate API_DIR=tests/__data__/output/.api npm run api:generate',
    { encoding: 'utf8' }
  )
})

it('can create streams.json', () => {
  expect(content('output/.api/streams.json')).toMatchObject(content('expected/.api/streams.json'))
})

function content(filepath: string) {
  return JSON.parse(
    fs.readFileSync(`tests/__data__/${filepath}`, {
      encoding: 'utf8'
    })
  )
}