Vitest: the promotion ends at midnight — does the test know?

Bugs often hide at boundaries between dates, amounts and conditions. Test business rules without depending on the current clock.

The same test passes in the morning and fails at night

Imagine a promotion valid until the end of the day. The discount function reads the current date and the test uses the real clock. Its result now depends on execution time and environment configuration. A green report gives no stable evidence about the rule.

Vitest supports replacing dependencies and controlling time in tests. Its documentation also emphasizes clearing or restoring mock state between cases. Vitest mocking guide

Write examples that settle the rules

For a promotion, test just before expiry, exactly at the boundary and afterward. Agree on the time zone and whether eligibility depends on starting checkout or placing the order. These require business decisions before a test can be meaningful.

Separate calculation from clock and network access. Passing time as a dependency makes the rules testable without constructing an entire store. Apply the same care to rounding, minimum spend and combining discount codes.

Do not mock away the point of the test

A test that supplies a finished discount and checks its display does not protect the calculation. Replace external boundaries where appropriate, but execute the actual business rule. Verify database or API integration separately at the relevant level.

Review case names with the person responsible for promotions. They should describe decisions the business understands. Such tests become a record of agreed behavior and reveal when a new campaign requires revisiting earlier assumptions.