Jasmine JavaScript Testing Framework Jasmine Custom Matchers and Extensions 2 — Questions and Answers
Question 1: In the custom matcher compare function signature compare(actual, expected), what does 'actual' represent?
- The value passed to expect() (Correct answer)
- The value passed to the matcher call
- The Jasmine environment object
- The test description string
Correct answer: The value passed to expect()
The 'actual' parameter in the compare function receives whatever value was passed into expect(), while 'expected' receives arguments from the matcher call itself.
Question 2: How does Jasmine use a custom matcher's negated failure message when .not is used?
- It calls negativeCompare if defined, otherwise it uses the default pass negation with the same message (Correct answer)
- It automatically prepends 'NOT' to the message
- It calls the message function with a false argument
- It ignores the message property entirely
Correct answer: It calls negativeCompare if defined, otherwise it uses the default pass negation with the same message
If a negativeCompare function is provided in the matcher object, Jasmine uses it for .not expectations; otherwise it inverts the pass result but uses the existing message.
Question 3: What does jasmine.any(Constructor) match?
- Any value that is an instance of the given Constructor or primitive type (Correct answer)
- Any value that is strictly equal to Constructor
- Any value that is not null
- Any function whose name matches Constructor
Correct answer: Any value that is an instance of the given Constructor or primitive type
jasmine.any() checks that the actual value is an instance of the provided constructor or, for primitives like Number/String, that typeof matches.
Question 4: What is jasmine.anything() used for in expectations?
- Matches any value except null and undefined (Correct answer)
- Matches every possible JavaScript value including null
- Matches only truthy values
- Matches only objects and arrays
Correct answer: Matches any value except null and undefined
jasmine.anything() is an asymmetric matcher that passes for any value that is not null or undefined, useful when you only want to confirm a value is present.
Question 5: Which custom matcher utility parameter gives access to equality testers when implementing deep equality in a custom matcher?
- util (the jasmine utility object with matchersUtil.equals) (Correct answer)
- env (the Jasmine environment)
- ctx (the context object)
- spec (the current spec reference)
Correct answer: util (the jasmine utility object with matchersUtil.equals)
The factory function receives util as its first parameter, which provides matchersUtil.equals() for performing Jasmine-aware deep equality checks inside a custom matcher.
Question 6: When you define a custom matcher with jasmine.addMatchers() inside a beforeEach, in which scope are those matchers available?
- Only within the describe block that contains the beforeEach (Correct answer)
- Globally across all specs in the file
- Only in the afterEach of the same describe
- Only in the first it block
Correct answer: Only within the describe block that contains the beforeEach
Custom matchers registered in a beforeEach are scoped to the describe block containing that beforeEach; they are not inherited by outer or sibling suites.
Question 7: What is the purpose of jasmine.nothing() used as an asymmetric matcher?
- Asserts that a spy was called with no arguments or matches any call with no arguments (Correct answer)
- Asserts the value is null
- Asserts the function returns undefined
- Asserts there are no properties on the object
Correct answer: Asserts that a spy was called with no arguments or matches any call with no arguments
jasmine.nothing() is an asymmetric equality tester that only passes when compared to no value, typically used with toHaveBeenCalledWith() to assert a spy was called with zero arguments.
In the custom matcher compare function signature compare(actual, expected), what does 'actual' represent?