Skip to content

test(device-profiler): improve JSON formatter integration tests #375

Description

@groupsky

Description

The CLI integration tests currently mock formatJSON, which means they test mock behavior rather than the actual JSON output.

Current Issue

In packages/device-profiler/src/cli.test.ts:

jest.mock('./json-formatter.js')

// In tests
const mockFormatJSON = jest.fn().mockReturnValue('{"test": "json"}')
;(jsonFormatterModule.formatJSON as jest.Mock) = mockFormatJSON

This tests that:

  • The mock was called ✓
  • console.log received the mock output ✓

But does NOT test:

  • Actual JSON structure ✗
  • Real formatJSON behavior ✗
  • Integration between cli and formatter ✗

Proposed Solutions

Option A: Remove Mock (Recommended)

Test the actual integration:

// Remove jest.mock('./json-formatter.js')

it('should output JSON when format is json', async () => {
  // ... setup transport ...
  
  await runProfileScan({
    transport: mockTransport,
    type: RegisterType.Holding,
    startAddress: 0,
    endAddress: 0,
    format: 'json',
    port: '/dev/ttyUSB0',
  })

  const output = mockConsoleLog.mock.calls.find(call => 
    typeof call[0] === 'string' && call[0].includes('"timestamp"')
  )?.[0]
  
  const parsed = JSON.parse(output)
  expect(parsed).toMatchObject({
    timestamp: expect.any(String),
    scan: { type: 'holding', startAddress: 0, endAddress: 0 },
    connection: { port: '/dev/ttyUSB0' },
    results: expect.any(Array),
    summary: expect.any(Object)
  })
})

Option B: Add Snapshot Test

Keep mock but add real integration test:

it('should produce valid JSON output snapshot', async () => {
  // ... real integration test with snapshot
})

Option C: Document Separation

Keep mock and add comment explaining why:

// We mock formatJSON here to isolate CLI orchestration testing.
// The actual JSON structure is thoroughly tested in json-formatter.test.ts
jest.mock('./json-formatter.js')

Impact

  • Option A: Better integration coverage, catches real bugs
  • Option B: Both unit and integration coverage
  • Option C: Current state is acceptable if documented

Recommendation

Option A - Remove mock for true integration testing. The json-formatter.test.ts already provides unit coverage.

Related

Identified in aspected review of #<PR_NUMBER>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttestingTesting improvements and additions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions