Skip to content

Instantly share code, notes, and snippets.

@elyasha
Created April 2, 2022 21:24
Show Gist options
  • Save elyasha/8973839c82cd5255e53518fed9d9221a to your computer and use it in GitHub Desktop.
Save elyasha/8973839c82cd5255e53518fed9d9221a to your computer and use it in GitHub Desktop.
LEARN JAVASCRIPT UNIT TESTING Factorial Feature
const Calculate = {
factorial: (inputValue) => {
if (inputValue === 0) {return 1}
else if (inputValue > 0) {
let result = 1
for(let i = 1; i <= inputValue; i++) {
result *= i
}
return result
}
else {
throw new Error("Error")
}
}
}
module.exports = Calculate;
var assert = require("assert");
var Calculate = require('../index.js')
describe('Calculate', () => {
describe('.factorial', () => {
it("returns 120 for 5!", () => {
const expectedResult = 120
const inputValue = 5
const result = Calculate.factorial(inputValue)
assert.equal(result, expectedResult)
})
it('returns 6 for 3!', () => {
const expectedResult = 6
const inputValue = 3
const result = Calculate.factorial(inputValue)
assert.equal(result, expectedResult)
})
it('returns 1 for 0!', () => {
const expectedResult = 1
const inputValue = 0
const result = Calculate.factorial(inputValue)
assert.equal(result, expectedResult)
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment