You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Alias 'to throw' as 'to throw ... satisfying' #535
Aliases for "to throw" are already supported - see the documentation for the assertion here.
The word "satisfying" is not something defined for "to throw", but if we added it that wouldn't really be an alias but an entirely different assertion. However, you can already achieve exactly the semantics you want by using expect.it, for example:
expect(() => { throw new Error() }, 'to throw', expect.it('to have property', 'message'));
expect(() => { throw new Error() }, 'to throw error', expect.it('to have property', 'message'));
expect(() => { throw new Error() }, 'to throw exception', expect.it('to have property', 'message'));
The to throw... assertion already uses "to satisfy" semantics, though? In the light of that I think it would make sense to add the ...satisfying variant as an alias?
My apologies, I think I misread the value type to mean it wouldn't compare the error using satisfy semantics - the assertion does something a little special for strings I believe (comparing the message against it) hence the expect.it suggestion.
changed the title [-]Alias 'to throw' as 'to throw (error|exception...etc) satisfying'[/-][+]Alias 'to throw' as 'to throw ... satisfying'[/+]on Dec 3, 2018
The issue title was also a bit confusing, I've updated it to make it a bit clearer. While I have your attention, what does <function> to throw (a|an) <function> do :)?
I've been thinking about this some more - I can definitely see the value in this alias but I'd like to raise one concern.
Using satisfying here would introduce something that behaves a little differently than for example "to have an item satisfying" primarily because of the matching of the error message to a string of its supplied. That would make this use of "satisfying" a little different to all the others. My concern is also that since (unfortunately) strings can be thrown at the language level this would create an annoying ambibugity. I might be over losing this though so keen to hear thoughts.
@joelmukuthu thanks for expanding on the description a bit :)
@alexjeffburke I agree, but I've also always thought of to satisfy as an assertion that allows you to assert against different types which to me makes this an acceptable ambiguity aka "it's a feature" :D. Actually I think to throw error satisfying <string> reads better than to throw error <string>
Activity
alexjeffburke commentedon Dec 2, 2018
Aliases for "to throw" are already supported - see the documentation for the assertion here.
The word "satisfying" is not something defined for "to throw", but if we added it that wouldn't really be an alias but an entirely different assertion. However, you can already achieve exactly the semantics you want by using expect.it, for example:
papandreou commentedon Dec 3, 2018
The
to throw... assertion already uses "to satisfy" semantics, though? In the light of that I think it would make sense to add the ...satisfyingvariant as an alias?alexjeffburke commentedon Dec 3, 2018
My apologies, I think I misread the value type to mean it wouldn't compare the error using satisfy semantics - the assertion does something a little special for strings I believe (comparing the message against it) hence the expect.it suggestion.
[-]Alias 'to throw' as 'to throw (error|exception...etc) satisfying'[/-][+]Alias 'to throw' as 'to throw ... satisfying'[/+]joelmukuthu commentedon Dec 3, 2018
The issue title was also a bit confusing, I've updated it to make it a bit clearer. While I have your attention, what does
<function> to throw (a|an) <function>do :)?unexpected/lib/assertions.js
Lines 1030 to 1044 in 9b30831
alexjeffburke commentedon Dec 4, 2018
I've been thinking about this some more - I can definitely see the value in this alias but I'd like to raise one concern.
Using satisfying here would introduce something that behaves a little differently than for example "to have an item satisfying" primarily because of the matching of the error message to a string of its supplied. That would make this use of "satisfying" a little different to all the others. My concern is also that since (unfortunately) strings can be thrown at the language level this would create an annoying ambibugity. I might be over losing this though so keen to hear thoughts.
@joelmukuthu thanks for expanding on the description a bit :)
joelmukuthu commentedon Dec 4, 2018
@alexjeffburke I agree, but I've also always thought of
to satisfyas an assertion that allows you to assert against different types which to me makes this an acceptable ambiguity aka "it's a feature" :D. Actually I thinkto throw error satisfying <string>reads better thanto throw error <string>papandreou commentedon Dec 4, 2018
I agree about
to throw error satisfying <string>reading better. Andto throw error satisfying <object>even more so!<function> to throw (a|an) <function>checks that the thrown value is an instance of a particular class, eg.:alexjeffburke commentedon Dec 4, 2018
Agreed about that spelling for which I think means we've reached some broad agreement - I can have a look next time I'm near core.
Fix special casing of UnexpectedError in <function> to error/throw
papandreou commentedon Dec 9, 2018
You're right, that's a bit of a wart in itself, but I think we can fix that so it reduces directly to
<UnexpectedError> to satisfy...: #538.Fix special casing of UnexpectedError in <function> to error/throw
papandreou commentedon Jan 3, 2019
@joelmukuthu, personally I'd welcome a PR for this now that the other oddity got resolved.