Open dasilvacontin opened 7 years ago
The when called with
/when called
assertion actually yields the return value of the function, which is then passed as the subject for the subsequent assertions. Your example will only work if parseActivity('M1')
returns a function that then throws a SyntaxError
when called without any arguments.
I agree that this appears unintuitive, primarily because when called [with]
doesn't explicitly say that the subsequent assertions will receive the return value rather than some context about how the function call went, including whether it threw an exception. Also, due to the general direction of things in Unexpected plugin land, users get accustomed to assertions that form complete sentences, including filler words -- so I'm not really surprised that you gave when called with... to throw a
a shot.
We could fix the main problem by renaming the assertion. If we pull in https://github.com/unexpectedjs/unexpected/pull/383 we could fix both problems along with https://github.com/unexpectedjs/unexpected/issues/394 by renaming/changing it to the exact thing that you expected to work:
expect(fn, 'when called to return value satisfying', 'yadda');
expect(fn, 'when called with parameters', [1, 2], 'to return value satisfying', 'yadda');
expect(fn, 'when called with parameter', 123, 'to return value satisfying', 'yadda');
expect(fn, 'when called with parameter', 123, 'to throw a', SyntaxError);
etc.
We can consider depreciating it, but I don't think doing a major version for this is a good idea. On Sat, 6 May 2017 at 11.02, Andreas Lind notifications@github.com wrote:
The when called with/when called assertion actually yields the return value of the function, which is then passed as the subject for the subsequent assertions. Your example will only work if parseActivity('M1') returns a function that then throws a SyntaxError when called without any arguments.
I agree that this appears unintuitive, primarily because when called [with] doesn't explicitly say that the subsequent assertions will receive the return value rather than some context about how the function call went, including whether it threw an exception. Also, due to the general direction of things in Unexpected plugin land, users get accustomed to assertions that form complete sentences, including filler words -- so I'm not really surprised that you gave when called with... to throw a a shot.
We could fix the main problem by renaming the assertion. If we pull in
383 https://github.com/unexpectedjs/unexpected/pull/383 we could fix
both problems along with #394 https://github.com/unexpectedjs/unexpected/issues/394 by renaming/changing it to the exact thing that you expected to work:
expect(fn, 'when called to return value satisfying', 'yadda');expect(fn, 'when called with parameters', [1, 2], 'to return value satisfying', 'yadda');expect(fn, 'when called with parameter', 123, 'to return value satisfying', 'yadda');expect(fn, 'when called with parameter', 123, 'to throw a', SyntaxError);
etc.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/unexpectedjs/unexpected/issues/395#issuecomment-299626527, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFisnIuxNPW-nWWdfp_RMCY6XUpG_h9ks5r3DcOgaJpZM4NR1Yo .
I’ve made a start updating the docs for this.
Side thought, of we’re going to clarify this case of an assertion that executed a function perhaps we want to do others - but I imagine that’s separate vomits per doc so let’s see where we get.
Vomits? :)
@papandreou sigh "commits" 🙃
@dasilvacontin would you have been more likely to avoid this trap if the "to throw" documentation were as in this PR: https://github.com/unexpectedjs/unexpected/pull/493
@alexjeffburke I don't think so. I would still pass the function directly and try the same thing.
expect(fn, 'when called to return value satisfying', 'yadda');
expect(fn, 'when called with parameters', [1, 2], 'to return value satisfying', 'yadda');
expect(fn, 'when called with parameter', 123, 'to return value satisfying', 'yadda');
expect(fn, 'when called with parameter', 123, 'to throw a', SyntaxError);
@papandreou I don't really like any of these variants as they tie the two asserts together. And I really never intended the assertions to be perfect english. But I do agree that some of these middle assertions can be confusing without reading the documentation. I'm actually a bit puzzled why you wont get a type error. to throw
should only accept functions.
Mhh I get the following error, maybe something was fixed 🤔
We could consider 'when called' and 'when called with' to yield a function invocation. Then we could make versions of 'to throw' that executes the function invocation. That would allow us to have 'to return value satisfying' on a function invocation. It would be a bit more verbose but also more explicit.
@sunesimonsen, or use https://github.com/unexpectedjs/unexpected/pull/383 to do it without the intermediate type :smirk_cat:
This assertion has come up in conversations a couple of times recently: I'm going to be bold and say I feel a consensus is growing that we might need to rethink it, and my personal feeling is Sune's mid-2017 comment (https://github.com/unexpectedjs/unexpected/issues/395#issuecomment-299627343) might be something we should consider.
Given the above, and that it doesn't seem likely we can easily address this behavioural wart, would folks be comfortable if I closed this in favour of a task about looking into withdrawing it?
Doesn't work:
Works: