Back to home

Writing testsMain js fileTesting
JavaScript FizzBuzz main image

JavaScript FizzBuzz

This blog assumes a global install of mocha, although that can also be installed locally. chai is also required as the assertion library - install as a dev dependency.

Writing tests

Create fizzbuzz.mocha.js.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 const lib = require("./index"); const chai = require("chai"); const { expect } = chai; describe("check basic function", function () { it("should return when not part of fizz buzz", function () { const res = lib.run(2); expect(res).to.equal(2); }); it("should return fizz", function () { const res = lib.run(3); expect(res).to.equal("Fizz"); }); it("should return buzz", function () { const res = lib.run(5); expect(res).to.equal("Buzz"); }); it("should return fizzbuzz", function () { const res = lib.run(15); expect(res).to.equal("FizzBuzz"); }); });

Main js file

Create index.js:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const run = (arg) => { switch (true) { case arg % 15 === 0: return "FizzBuzz"; case arg % 3 === 0: return "Fizz"; case arg % 5 === 0: return "Buzz"; default: return arg; } }; module.exports = { run, };

Testing

Change into directory and run mocha fizzbuzz.mocha.js.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.