Skip to content

Instantly share code, notes, and snippets.

@nickytonline
Last active July 20, 2019 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickytonline/4ecaa049283feb2ca7c2b3ee993df7c3 to your computer and use it in GitHub Desktop.
Save nickytonline/4ecaa049283feb2ca7c2b3ee993df7c3 to your computer and use it in GitHub Desktop.
Recreated a portion of the expect library
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