stackbuilders/assertive-ts

Add FunctionAssertion starter implementation

JoseLion opened this issue ยท 3 comments

We need to add the starter implementation for assertions of type function. By "starter" we mean that we should add the most meaningful assertions needed for function types. More complex assertions can be left to a second iteration. Please follow the following guideline to keep consistency across the library:

  • Name the class FunctionAssertion and extend it from Assertion
  • Use StringAssertion as a guide for creating assertions and adding tests
  • We can get ideas of assertions specific to function by looking into AssertJ, jest, and jest-extended

This assertion can be a tricky one and we need to also decide how important/necessary this could be. The difficult part is thinking about what assertions we can get of a function. So far, the only one I can think of is checking if the function throws or not an error, plus asserting over the error data. Maybe something like this:

expect(() => myUntrustyMethod(args)).toThrow(error)
                                    .toThrow(MyErrorClass)
                                    .toThrow(MyErrorClass, message)

@JoseLion after digging into this, it seems that the main thing to implement here would be checking if a function throws an error or not. I also found that another assertion could be checking if the value passed is a function or not toBeFunction (may not be that useful).
Besides that, I notices that libraries like jest also have assertions to check how many times a function has been called. Although it is useful, it may not be something I will put in this domain exactly. jest-extended puts these assertions in a Mock context, which makes sense.
If I have any ideas I will post them here for discussion.

@Alex0jk, thanks for the update! I agree that toBeFunction may not be that useful in TypeScript's context since we already know we're asserting over a function. I've thought we could probably add an UnknownAssertion for when the value is unknown so users can narrow the type down to the corresponding assertion type. What do you think?

I was also thinking about checking how many times a function was called (and with which args, etc.), and I agree, it sounds like something that should be part of a mock/spy library, like a Sinon plugin or something like that. Let's keep thinking about that idea, but for FunctionAssertion it seems not to be necessary ๐Ÿ™‚

๐ŸŽ‰ This issue has been resolved in version 1.0.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€