Created
April 2, 2022 21:24
-
-
Save elyasha/8973839c82cd5255e53518fed9d9221a to your computer and use it in GitHub Desktop.
LEARN JAVASCRIPT UNIT TESTING Factorial Feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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