Skip to content

Add unit tests for formatCurrency and formatRelativeTime in lib/utils #68

Description

@aruneshwisdm

Summary

The core utility functions in apps/web/lib/utils.tstoNumber, formatCurrency, formatDate, formatRelativeTime, and getBaseUrl — have no unit tests. These are used throughout the entire application for displaying currencies, dates, and handling Prisma Decimal conversions.

Functions to test

  • toNumber(value) — converts Prisma Decimal, null, undefined, or number to a plain number
  • formatCurrency(amount, currency, locale) — formats numbers as currency strings with a space after the symbol
  • formatDate(date, options, locale) — formats dates using Intl.DateTimeFormat
  • formatRelativeTime(date, locale) — returns human-readable relative time (e.g., "2 hours ago")
  • getBaseUrl() — returns the app's base URL from environment variables

Files

  • Source: apps/web/lib/utils.ts
  • Test file to create: apps/web/__tests__/lib/utils.test.ts

Acceptance criteria

  • toNumber tested with: null (returns 0), undefined (returns 0), plain number, Decimal-like object with .toNumber()
  • formatCurrency tested with USD, EUR, and INR — verify space between symbol and number
  • formatRelativeTime tested with dates in the past (e.g., 5 minutes ago, 2 days ago)
  • getBaseUrl tested by mocking process.env with different env var combinations
  • All tests pass with pnpm vitest run

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions