Created
November 22, 2023 16:28
-
-
Save suhailgupta03/f97a2cf1f1fbfcacd0e869707069b240 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function sum(a, b) { | |
return a + b; | |
} | |
function divide(a, b) { | |
return a / b; | |
} | |
function calculatePercentage(a, b) { | |
return divide(a, b) * 100; | |
} | |
module.exports = { | |
sum, | |
divide, | |
calculatePercentage | |
} |
This file contains hidden or 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 { | |
sum, | |
divide, | |
calculatePercentage | |
} = require("./calc"); // import the function | |
// This function tests the sum() function | |
// and prints a message to the console | |
// depending on whether the test passed or failed. | |
function testSum() { // This is the unit test | |
// because it tests a single unit of code. | |
const result = sum(7, 3); | |
const expected = 10; | |
if(result !== expected) { | |
console.log("TEST FAILED x_x"); | |
}else { | |
console.log("TEST PASSED :)"); | |
} | |
} | |
function testPercentage() { | |
// This is the integration test | |
// because it tests the integration of | |
// the divide() and calculatePercentage() | |
// functions. | |
const result = calculatePercentage(5, 20); | |
const expected = 25; | |
if(result !== expected) { | |
console.log("TEST FAILED x_x"); | |
}else { | |
console.log("TEST PASSED :)"); | |
} | |
} | |
testSum(); | |
testPercentage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jestjs.io/