Overview

Let's talk about React.js!

In the first post of this series, I created the base for the utils layer.

At that time, I created src/utils/formatDate.ts, but I never wrote unit tests for it!
In 2025, unit tests are a must-have for any program. So, let's add them.

Adding the Unit Test Base

For unit testing, I chose Vitest.
Why? Because when I set up this starter kit, I used the following command:

yarn create vite my-react -- -template react-swc-ts

Since the project is based on Vite, it makes sense to use Vitest.

yarn add -D vitest
# Install Vitest.
# Create src/utils/formatDate.test.ts
echo 'import { describe, it, expect } from "vitest"
import formatDate from "./formatDate"

// Vitest doc: https://vitest.dev/api/

describe("formatDate", () => {
  it("should format date as `Month day, year`", () => {
    const date = new Date("2023-03-15")
    const result = formatDate(date)
    expect(result).toBe("March 15, 2023")
  })
})' > src/utils/formatDate.test.ts
# Try running the tests.
# Without "run": watch mode
# With "run": standard execution
yarn run vitest run
# Output ↓
# $ ...my-react/node_modules/.bin/vitest run
# ...omitted
#  ✓ src/utils/formatDate.test.ts (1 test) 9ms
#    ✓ formatDate > should format date as `Month day, year`
#
#  Test Files  1 passed (1)
#       Tests  1 passed (1)
# ...omitted
# Done in 0.71s.

Conclusion

Yep, it's perfect.

But... Python already has a built-in unittest module, so you don't even need to install anything.
(Though you still need to import unittest... just a Pythonista's remark.)