What you test here
A unit test checks one small piece of code by itself. Usually this is a function that takes input and returns output.
For your hackathon repo, this is the easiest proof to add first. Pick business logic, not a button colour or a database call.
One idea
A good unit test proves one expected behaviour with clear inputs and a clear result. If it needs a server, a database, or a logged-in user, it is probably not a unit test.
Pick a function
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 4 done
Example unit test
Here is a small JavaScript example using Vitest. The function calculates a simple platform fee in rupees.
The test has three parts: arrange the input, act by calling the function, and assert the expected output.
// src/calculateFee.js
export function calculateFee(amount) {
if (amount <= 0) return 0;
return Math.ceil(amount * 0.02);
}
// src/calculateFee.test.js
import { describe, it, expect } from 'vitest';
import { calculateFee } from './calculateFee.js';
describe('calculateFee', () => {
it('returns 2 percent fee rounded up', () => {
// arrange
const amount = 101;
// act
const fee = calculateFee(amount);
// assert
expect(fee).toBe(3);
});
it('returns zero for zero or negative amounts', () => {
expect(calculateFee(0)).toBe(0);
expect(calculateFee(-50)).toBe(0);
});
});Run it locally
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 4 done
npm install -D vitest
// package.json
{
"scripts": {
"test": "vitest run"
}
}
npm testName tests plainly
Write the test name like a sentence: returns zero for negative amounts. A recruiter should understand the behaviour without opening the function.
If you use AI to draft tests, ask it for cases first, not code first. Then check that every case matches your actual product rules.
Prompt you can use:
I have this function. Suggest 5 useful unit test cases. Include normal cases and edge cases. Do not write code yet.
Then paste your function.Common mistake
Do not test the implementation detail. Test the result the user or app depends on.
For example, do not test that the function uses Math.ceil. Test that 101 rupees gives a 3 rupee fee. This keeps the test useful even if you rewrite the internals later.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 4 done
git add src/calculateFee.js src/calculateFee.test.js package.json package-lock.json
git commit -m "Add unit test for fee calculation"