Created
July 18, 2020 12:01
-
-
Save markodayan/1aa9e0e6f6aaeb33bdb9471db495fbe6 to your computer and use it in GitHub Desktop.
Assertion Libary Basic Example (JS Testing)
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, subtract } = require('../math'); // const sum = (a, b) => a - b; const subtract = (a, b) => a - b; | |
// sum is intentionally wrong to check if error handling is done according to our assertion function | |
let result, expected; | |
result = sum(3, 7); | |
expected = 10; | |
expect(result).toBe(expected); | |
result = subtract(7, 3); | |
expected = 4; | |
expect(result).toBe(expected); | |
function expect(actual) { | |
return { | |
toBe(expected) { | |
if (actual !== expected) { | |
throw new Error(`${actual} is not equal to ${expected}`); | |
} | |
}, | |
toEqual(expected) {}, | |
toBeGreaterThan(expected) {}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment