Created
January 15, 2019 14:57
-
-
Save samselikoff/91e60ea6c568679bfd34dd23dbb41b1c to your computer and use it in GitHub Desktop.
async-throws
This file contains 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
import Ember from 'ember'; | |
let originalLoggerError; | |
let originalTestAdapterException; | |
let originalOnError; | |
let originalConsoleError; | |
function intercept(f = () => {}) { | |
originalLoggerError = Ember.Logger.error; | |
originalTestAdapterException = Ember.Test.adapter.exception; | |
originalOnError = Ember.onerror; | |
originalConsoleError = console.error; | |
Ember.Logger.error = f; | |
Ember.Test.adapter.exception = f; | |
Ember.onerror = f; | |
console.error = f; | |
} | |
function restore() { | |
Ember.Logger.error = originalLoggerError; | |
Ember.Test.adapter.exception = originalTestAdapterException; | |
Ember.onerror = originalOnError; | |
console.error = originalConsoleError; | |
} | |
export default function asyncThrows(context, f, text) { | |
let done = this.async(); | |
let loggedErrorArgs; | |
intercept((...args) => { | |
loggedErrorArgs = args; | |
}); | |
return f() | |
.then(() => { | |
let errorText = loggedErrorArgs.join(' '); | |
if (text) { | |
let result = errorText.match(text); | |
this.pushResult({ | |
result, | |
expected: text, | |
actual: errorText, | |
message: `Expected to see error '${text}'` | |
}); | |
} else { | |
this.pushResult({ | |
result: false, | |
expected: '', | |
actual: errorText, | |
message: `You're using asyncThrows but you didn't add text to the assertion. Add some text as the second argument so the actual exception being thrown is what you expect it is.` | |
}); | |
} | |
return done(); | |
}) | |
.then(() => { | |
return restore(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment