Skip to content

Introduce selective mocking utilities for tests #5

@ewilderj

Description

@ewilderj

Description

Create reusable mocking utilities to reduce code duplication in tests and improve test maintainability across the codebase.

Work Required

  1. Create Mock Page Utility: Add test/utils/mockPage.js for Playwright page mocking
  2. Create HTTP Mock Utility: Add test/utils/httpMocks.js for external API mocking
  3. Refactor Existing Tests: Update existing tests to use new utilities
  4. Document Usage: Add documentation for utility usage patterns

Implementation Details

Based on existing test patterns and upcoming test needs:

test/utils/mockPage.js

  • Mock Playwright page object for capture system tests
  • Provide common page methods: evaluate(), waitForTimeout(), screenshot()
  • Mock page properties and events for visual detector tests
  • Support method chaining and Promise-based APIs

test/utils/httpMocks.js

  • Mock axios/fetch for external API calls in stocks/weather modules
  • Provide utilities for mocking HTTP responses, timeouts, errors
  • Support rate limiting simulation for API testing
  • Include common response patterns for external services

Current Test Patterns to Improve

From existing tests:

  • test/templates.validation.api.test.js - Uses supertest for API testing
  • Future capture system tests will need page mocking
  • Future stocks/weather tests will need HTTP mocking

Mock Patterns Needed

Page Mocking

const mockPage = createMockPage({
  evaluate: jest.fn(),
  waitForTimeout: jest.fn(),
  screenshot: jest.fn()
});

HTTP Mocking

const httpMock = createHttpMock()
  .get('/api/external')
  .reply(200, { data: 'test' });

Files to Create

  • test/utils/mockPage.js - Playwright page mocking utility
  • test/utils/httpMocks.js - HTTP request mocking utility
  • test/utils/index.js - Barrel export for utilities

Usage Guidelines

  • Keep utilities focused and composable
  • Provide clear documentation and examples
  • Follow Jest mocking best practices
  • Make utilities easy to extend for specific use cases

Acceptance Criteria

  • Duplicate mock code is reduced across test files
  • New utilities are adopted in new tests
  • Existing tests can be refactored to use utilities where appropriate
  • Utilities are well-documented with usage examples
  • Mock utilities support common testing patterns in the codebase

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions