To create unit tests using Mocha, Chai, and TypeScript for the formatRange function, we first need to understand its intended behavior based on the documentation comment.
Assumptions from the function docs:
- The function takes a
value (number), a unit (plural string like "seconds"), a singular unit (like 'second'), and an optional fuziness value (default 0.25).
- It returns a rounded number or a range:
- If the
value is close enough to an integer (within the fuziness margin), the result will be rounded, e.g., "1 second" or "2 seconds".
- If not, it returns a range, like
"0.8–1.2 seconds".
✅ Step 1: Create the Implementation (Assumed)
We'll need a basic assumption of how formatRange works. Here's a sample implementation based on your JSDoc:
// formatRange.ts
export function formatRange(
value: number,
unit: string,
singular: string,
fuziness: number = 0.25
): string {
const lower = Math.floor(value);
const upper = Math.ceil(value);
const deviation = Math.abs(value - Math.round(value));
if (deviation <= fuziness) {
const rounded = Math.round(value);
const label = rounded === 1 ? singular : unit;
return `${rounded} ${label}`;
} else {
const rangeMin = (value * (1 - fuziness)).toFixed(1);
const rangeMax = (value * (1 + fuziness)).toFixed(1);
return `${rangeMin}–${rangeMax} ${unit}`;
}
}
You can replace this with your actual version if different.
✅ Step 2: Install Test Dependencies
Make sure you have these packages:
npm install --save-dev mocha chai ts-node typescript @types/mocha @types/chai
Create a test script in your package.json:
"scripts": {
"test": "mocha -r ts-node/register tests/**/*.test.ts"
}
✅ Step 3: Create Unit Tests
Create a file like tests/formatRange.test.ts.
// tests/formatRange.test.ts
import { expect } from "chai";
import { formatRange } from "../src/formatRange"; // Adjust path
describe("formatRange", () => {
it("should return singular form when rounded value is 1", () => {
const result = formatRange(1.05, "seconds", "second", 0.1);
expect(result).to.equal("1 second");
});
it("should return plural form for values rounding to > 1", () => {
const result = formatRange(2.1, "seconds", "second");
expect(result).to.equal("2 seconds");
});
it("should return a range when value is outside of fuziness", () => {
const result = formatRange(1.5, "seconds", "second", 0.1);
// 1.5 * 0.9 = 1.35, 1.5 * 1.1 = 1.65
expect(result).to.equal("1.4–1.7 seconds");
});
it("should use default fuziness (0.25) when not provided", () => {
const result = formatRange(1.3, "minutes", "minute");
// 1.3 rounded to 1.0, delta = 0.3 > 0.25 → should be a range
expect(result).to.equal("1.0–1.6 minutes");
});
it("should round down to 0 if value < fuziness", () => {
const result = formatRange(0.1, "hours", "hour", 0.2);
expect(result).to.equal("0 hours");
});
it("should correctly handle exact integer values", () => {
const result = formatRange(5, "seconds", "second");
expect(result).to.equal("5 seconds");
});
});
✅ Step 4: Run the Tests
npm test
✅ Optional: Add Edge Case Coverage
You can also add:
NaN
- Negative numbers
- Very large values
- Very small values (< 1, close to 0)
- Custom units like "days"/"day"
Let me know if you want the tests to cover those extra edge cases.