2019-11-14 21:18:52 +01:00
/ *
* This code is a mainly a copy - and - paste of a snippet provided by Sam Selikoff
* at https : //gist.github.com/samselikoff/ad5e3695383b91599ee428bf9a2d22ca
* /
import Ember from 'ember' ;
let originalTestAdapterException ;
let originalEmberOnError ;
let originalWindowOnError ;
function intercept ( f = ( ) => { } ) {
originalTestAdapterException = Ember . Test . adapter . exception ;
originalWindowOnError = window . onerror ;
originalEmberOnError = Ember . onerror ;
Ember . Test . adapter . exception = ( ) => { } ;
Ember . onerror = f ;
window . onerror = ( ) => { } ;
}
function restore ( ) {
Ember . Test . adapter . exception = originalTestAdapterException ;
Ember . onerror = originalEmberOnError ;
window . onerror = originalWindowOnError ;
}
export default async function asyncThrows ( f , expectedErrorMessage ) {
let done = this . async ( ) ;
let loggedErrorArgs ;
intercept ( ( ... args ) => {
loggedErrorArgs = args ;
} ) ;
await f ( ) ;
let errorText = ( loggedErrorArgs || [ ] ) . join ( ' ' ) ;
if ( expectedErrorMessage ) {
2023-10-28 19:15:06 +02:00
let result = errorText . includes ( expectedErrorMessage ) ;
2019-11-14 21:18:52 +01:00
this . pushResult ( {
result ,
expected : expectedErrorMessage ,
actual : errorText ,
2023-10-15 20:37:03 +02:00
message : ` Expected to see error ' ${ expectedErrorMessage } ' ` ,
2019-11-14 21:18:52 +01:00
} ) ;
} else {
this . pushResult ( {
result : false ,
expected : '' ,
actual : errorText ,
2023-10-15 20:37:03 +02:00
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. ` ,
2019-11-14 21:18:52 +01:00
} ) ;
}
await done ( ) ;
return restore ( ) ;
}