Skip to content

Instantly share code, notes, and snippets.

@markodayan
Created July 18, 2020 12:01
Show Gist options
  • Save markodayan/1aa9e0e6f6aaeb33bdb9471db495fbe6 to your computer and use it in GitHub Desktop.
Save markodayan/1aa9e0e6f6aaeb33bdb9471db495fbe6 to your computer and use it in GitHub Desktop.
Assertion Libary Basic Example (JS Testing)
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