unexpectedjs / unexpected

Unexpected - the extensible BDD assertion toolkit

Home Page:http://unexpected.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'when called with' silently failing when not-array is given

dasilvacontin opened this issue · comments

expect(parseActivity, 'when called with', 'M1', 'to throw', SyntaxError)

parseActivity was being called without arguments.

You get the following error right?

Unknown assertion 'with', did you mean: 'sorted'

The error message is clearly not okay. But there is also the aspect that the assertion you were using makes sense semantically, but technically it is not how when called with works. when called with forwards the result of calling the given function to the next assertion, so unless you expect parseActivity when called with M1 to return a function that throws, this won't work.

This is also why I have been a bit reluctant towards assertion that is just make a computation like when called with. They are very useful, but we need to very clear about how they work. The documentation actually state the functionality but that does not matter if it does not follow the intuition you have.

You can achieve what I think you want the following way:

expect(parseActivity.bind(null, 'M1'), 'to throw', SyntaxError)

I know this is not beautiful. Depending on how much you will be doing something like this, you can do the following instead. Make a custom assertion:

expect.addAssertionI(
  '<function> [not] to throw a SyntaxError when called with <string+>', 
  (expect, subject, ...args) => {
    expect(subject.bind(null, ...args), '[not] to throw')
  }
)

expect(parseActivity, 'to throw a SyntaxError when called with', 'M1')

#396 solved the original issue, and #395 should cover the discussion about ... when called with ... to throw a ...