Skip to content

Instantly share code, notes, and snippets.

@debonx
Created January 26, 2020 10:04
Show Gist options
  • Save debonx/a1e8de5dd12eff99e6bfd14874d35124 to your computer and use it in GitHub Desktop.
Save debonx/a1e8de5dd12eff99e6bfd14874d35124 to your computer and use it in GitHub Desktop.
Node, Mocha: Simple factorial test suite with Mocha and Node assertion methods.
const Calculate = {
factorial(number) {
if(number === 0) {
return 1;
}
for(let i = number - 1; i >= 1; i--) {
number *= i;
}
return number;
}
}
module.exports = Calculate;
var assert = require("assert");
var Calculate = require('../factorial.js')
describe('Calculate', () => {
describe('.factorial', () => {
it('test factorial 5! is 120', () => {
// Setup
const expected = 120;
const number = 5;
// Exercise
const actual = Calculate.factorial(number);
// Verify
assert.equal(actual, expected);
});
it('test factorial 3! is 6', () => {
// Setup
const expected = 6;
const number = 3;
// Exercise
const actual = Calculate.factorial(number);
// Verify
assert.equal(actual, expected);
});
it('return 1 when 0!', () => {
// Setup
const expected = 1;
const number = 0;
// Exercise
const actual = Calculate.factorial(number);
// Verify
assert.equal(actual, expected);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment