Fizz Buzz starter code for ACS 3310
The goal of this lab is to write unit tests for Fizz Buzz using Jest. You’ll practice writing test cases, analyzing coverage, and using AI as a coding companion.
Here are some useful prompts to help you work through this lab:
- "Help me write a unit test for a function that returns Fizz, Buzz, or FizzBuzz."
- "What edge cases should I test in this fizzBuzz implementation?"
- "What’s the difference between
.toBe()
and.toEqual()
in Jest?" - "What are edge cases?"
- ". These are the edge cases I'm testing, give me your opinion on these choices. Did i miss any edge cases?"
- "How can I improve my test coverage for this file?"
- What is a unit test, and when does it pass?
- What happens when a test fails in Jest?
- Why do we use constants like
FIZZ
,BUZZ
, andFIZZBUZZ
? - How can I use coverage data to improve my test suite?
Discuss your answers with a partner or ask AI to help you reflect.
The contents of this repo contain an implementation of the classic Fizz Buzz program. The program counts up to a given value. Along the way:
- Multiples of 3 are counted as
fizz
- Multiples of 5 as
buzz
- Multiples of both as
fizzbuzz
Calling fizzBuzz(count)
returns an object with the counts:
{ count: 100, fizz: 27, buzz: 14, fizzBuzz: 6 }
The program has been broken down into small, testable functions and constants.
- Clone or download this repo
- Navigate the terminal to the project directory
- Run:
npm init -y npm install --save-dev jest mkdir tests touch tests/test.js
- Edit
package.json
to add:"scripts": { "test": "jest" }
You can now run your tests with:
npm run test
A basic test looks like this:
test('Sanity check', () => {
expect(2 + 2).toBe(4);
});
To test the fizzbuzz.js
module, import it like so:
const fb = require('../fizzbuzz');
Access methods like this:
fb.fizzBuzz(16);
fb.isFizzy(4);
The fizzbuzz.js
module includes:
- Constants:
FIZZ
BUZZ
FIZZBUZZ
- Functions:
isFizzy(n)
– true if divisible by 3isBuzzy(n)
– true if divisible by 5fizzyBuzzy(n)
– returns correct fizz/buzz stringfizzBuzz(count)
– returns count breakdown object
Test each one of these!
📌 Tip: Use the constants in your tests to avoid typos.
Ensure each constant equals its expected string value:
expect(fb.FIZZ).toBe("fizz");
Write tests that prove this function behaves as expected with inputs like:
- 3, 6 (should return true)
- 2, 5 (should return false)
- Edge cases: 0, negative numbers
Same approach as isFizzy
. Try inputs:
- 5, 10 (true)
- 3, 6 (false)
Should return:
"fizz"
for 3"buzz"
for 5"fizzbuzz"
for 15""
for 2
Compare using:
expect(fb.fizzyBuzzy(3)).toBe(fb.FIZZ);
Use input 15
and test that the output is:
{
count: 15,
fizz: 4, // 3, 6, 9, 12
buzz: 2, // 5, 10
fizzBuzz: 1 // 15
}
Use .toEqual()
to compare objects.
- "Review my code: "
Ask your AI to code review your unit tests.
Can you simplify or optimize the fizzBuzz logic? Run your tests to ensure the refactor doesn't break functionality.
Allow users to specify the fizz and buzz divisors:
fizzBuzz(count, fizzOn = 3, buzzOn = 5);
Update your tests accordingly.
Group related tests using describe() blocks to make your test suite easier to read and maintain.
describe('fizzyBuzzy()', () => {
test('returns fizz for 3', () => {
expect(fb.fizzyBuzzy(3)).toBe(fb.FIZZ);
});
test('returns buzz for 5', () => {
expect(fb.fizzyBuzzy(5)).toBe(fb.BUZZ);
});
test('returns fizzbuzz for 15', () => {
expect(fb.fizzyBuzzy(15)).toBe(fb.FIZZBUZZ);
});
});
Want to know how much of the code your tests actually verify? Run:
npx jest --coverage
Look for:
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
Focus on reaching 100% on statements, branches, functions, and lines where possible.
Happy Testing! 🧪
Use Jest, explore edge cases, and get in the habit of checking your coverage. Let AI help with strategy and syntax, but make sure you understand what your tests are doing and why!