Learn unit testing best practices including mocking, async testing, and error handling. Then practice safe library migration using your tests as a safety net.
Group size: 1 person
- Complete Week 3 in-class exercise (TDD with Vitest)
- Bun version 1.3 or later installed
- Select "Use this Template" to create your own repository
- Run
bun install
npm run test- Run test suitenpm run test:coverage- Run tests with coverage report
Start by examining src/dateUtils.ts to understand the functions. Then write comprehensive tests in src/__tests__/dateUtils.test.ts.
Functions to test:
| Function | Description | Testing Notes |
|---|---|---|
getCurrentYear() |
Returns current year | Time-dependent - use techniques from Week 3 to make tests deterministic |
add(date, amount, type) |
Adds time to a date | Has error handling for invalid inputs |
isWithinRange(date, from, to) |
Checks if date is between two dates | Has error handling for invalid range |
isDateBefore(date, compareDate) |
Checks if date is before another | - |
isSameDay(date, compareDate) |
Checks if two dates are the same day | - |
getHolidays(year) |
Async: returns holiday dates for a year | Async function |
isHoliday(date) |
Async: checks if date is a holiday | Async function |
Things to consider:
- How do you test functions that depend on the current time?
- How do you test that functions throw the correct errors?
- How do you test async functions?
- What edge cases should you cover? (boundary dates, negative numbers, different unit types)
Commit your changes with a descriptive message.
Add type annotations to all function parameters and return types in src/dateUtils.ts.
Commit your changes.
Replace the deprecated moment library with date-fns. Find equivalent functions in the date-fns documentation.
Important: Do NOT modify your tests! If your tests are well-written, they should all pass after migration. This demonstrates that tests act as a contract - the implementation can change as long as the behavior stays the same.
Commit your changes.
- Add me (arnif) as a contributor to your GitHub repo
- Ensure you have at least 3 commits (one per task)
- Submit the GitHub repo link to Canvas
- Run
npm run test:coverageto check your test coverage - Use descriptive test names that explain the expected behavior
- Review what you learned in Week 3 about testing async code and mocking