Last active
July 20, 2019 04:13
-
-
Save nickytonline/4ecaa049283feb2ca7c2b3ee993df7c3 to your computer and use it in GitHub Desktop.
Recreated a portion of the expect library
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 expect(actual) { | |
function prettyJSON(objectToSerialize) { | |
return JSON.stringify(objectToSerialize, null, '\t') | |
} | |
return { | |
toBe(expected) { | |
if (actual === expected) { | |
console.log('✅ Pass') | |
} else { | |
const message = `References don't match\nReceived: \n${prettyJSON(actual)} \n\nExpected: \n${prettyJSON(expected)}` | |
console.log('❌ Fail') | |
console.assert(false, message) | |
} | |
}, | |
toEqual(expected) { | |
if (JSON.stringify(actual) === JSON.stringify(expected)) { | |
console.log('✅ Pass') | |
} else { | |
const message = `\nReceived: \n${prettyJSON(actual)} \n\nExpected: \n${prettyJSON(expected)}` | |
console.log('❌ Fail') | |
console.assert(false, message) | |
} | |
}, | |
toThrow(message) { | |
try { | |
actual() | |
console.log('❌ Fail') | |
console.assert(false, 'should have thrown an error') | |
} catch(e) { | |
if (message && message !== e.message) { | |
console.log('❌ Fail') | |
console.assert(false, `should have thrown an error with the following message, "${message}" but instead received the following error message, "${e.message}"`) | |
return | |
} | |
console.log('✅ Pass') | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment