Skip to content

Bug: Campaign test has type mismatch between mock (string stroops) and assertion (number XLM) #369

@gboigwe

Description

@gboigwe

Description

In backend/src/routes/campaigns.test.ts (lines 14-29), the test mocks return total_spent_stroops: '100000000' as a string, but the test assertion expects response.body.total_spent_xlm to equal 10 (a number):

// Mock returns string stroops
mockPool.query.mockResolvedValueOnce({
  rows: [{ 
    total_campaigns: '5',
    active_campaigns: '3',
    total_spent_stroops: '100000000'  // String
  }]
});

// Test expects numeric XLM
expect(response.body.total_spent_xlm).toBe(10);  // Number

Problem

The test validates that 100000000 stroops = 10 XLM, which is correct math. But:

  1. The mock returns a string ('100000000'), matching how PostgreSQL returns bigint columns
  2. If the route handler doesn't explicitly convert the string to a number before division, '100000000' / 10000000 gives 10 in JS (implicit coercion), but this masks the type issue
  3. If the handler returns the raw string without conversion, the test would fail with '100000000' !== 10

The test doesn't verify the actual transformation logic — it assumes the route handler converts correctly without testing the conversion path.

Impact

  • Test could pass even if the conversion is accidentally removed (if mock data changes)
  • Doesn't test edge cases: very large stroops values, zero, negative
  • String vs number type mismatch may surface in production with real PostgreSQL data

Suggested Fix

Test should explicitly verify the type and value:

expect(typeof response.body.total_spent_xlm).toBe('number');
expect(response.body.total_spent_xlm).toBeCloseTo(10, 7);

File

backend/src/routes/campaigns.test.ts — Lines 14-29

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtestingRelated to test coverage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions