How to make not function operate with custom matcher

34 views
Skip to first unread message

artie...@gmail.com

unread,
Jul 17, 2017, 5:10:32 PM7/17/17
to Jasmine
Hi Jasmine Users,

After building a successful custom matcher that works for the true case, how can I use the 'not' semantics with the same custom matcher?

    expect(object).not.myCustomMatcher() 

I get error:
Failed:expect(...).not.myCustomMatcher is not a function. 

I have verified that ".not" works correctly with builtin matchers. 

TIA!
AZ

Tony Brix

unread,
Jul 17, 2017, 5:44:18 PM7/17/17
to Jasmine
https://jasmine.github.io/2.6/custom_matcher.html#section-Custom_negative_comparators

You may provide a negativeCompare key along side your compare key or just return with the message key when the matcher passes.

artie...@gmail.com

unread,
Jul 17, 2017, 6:21:46 PM7/17/17
to Jasmine
Hi Tony,

Thanks! This is very helpful info even though my post was erroneous. I would like to read more about providing keys alongside. Not sure how that would look without reading more code. :)

I had moved my matcher and did not complete the changes. 

Very pleased to see it work and use the correct output message in the custom matcher. 

Kudos to Jasmine team!

-az

Gregg Van Hove

unread,
Jul 18, 2017, 12:13:21 AM7/18/17
to jasmi...@googlegroups.com
Jasmine custom matchers should work with `.not` without any modifications. This is why you might want to provide a failure message even if you set `pass` to `true` for the particular comparison. The `negativeCompare` function is available for matchers where the `.not` version is not just a simple negation, or the messaging needs to be significantly different between the two cases.

Hope this helps. Thanks for using Jasmine!

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages