Jasmine JavaScript Testing Framework Jasmine Custom Matchers and Extensions 1 — Questions and Answers
Question 1: Which Jasmine method is used to register custom matchers so they are available in specs?
- jasmine.addMatchers() (Correct answer)
- jasmine.registerMatcher()
- jasmine.extendMatchers()
- jasmine.createMatcher()
Correct answer: jasmine.addMatchers()
jasmine.addMatchers() accepts an object map of matcher names to factory functions and makes them available inside describe/it blocks.
Question 2: Where should jasmine.addMatchers() typically be called to ensure custom matchers are available for every spec in a suite?
- Inside a beforeEach block (Correct answer)
- Inside an afterEach block
- Inside an it block
- At the top level outside any describe
Correct answer: Inside a beforeEach block
Calling jasmine.addMatchers() inside a beforeEach block ensures the custom matchers are registered before each spec runs within that suite.
Question 3: What does jasmine.objectContaining({ name: 'Alice' }) do when used in an expectation?
- Passes if the actual object has at least a name property equal to 'Alice' (Correct answer)
- Passes only if the actual object is exactly { name: 'Alice' }
- Fails if the actual object has any extra properties
- Checks that the object is a class instance named Alice
Correct answer: Passes if the actual object has at least a name property equal to 'Alice'
jasmine.objectContaining() is an asymmetric matcher that passes as long as the specified key-value pairs are present, ignoring any additional properties.
Question 4: What must a custom matcher factory function return?
- An object with a compare function (Correct answer)
- A boolean value
- A Promise
- A string describing the matcher
Correct answer: An object with a compare function
A custom matcher factory returns an object that must include a compare function, which Jasmine calls with the actual and expected values.
Question 5: What does the compare function inside a custom matcher need to return?
- An object with a pass property (boolean) and optionally a message (Correct answer)
- A boolean true or false
- The difference between actual and expected
- A new Error object
Correct answer: An object with a pass property (boolean) and optionally a message
The compare function must return an object with at least { pass: boolean }, and can include a message property for failure descriptions.
Question 6: What does jasmine.arrayContaining(['a', 'b']) check?
- That the actual array contains at least 'a' and 'b' elements (Correct answer)
- That the actual array is exactly ['a', 'b']
- That 'a' and 'b' are the first two elements
- That the array length equals 2
Correct answer: That the actual array contains at least 'a' and 'b' elements
jasmine.arrayContaining() passes when the actual array contains all the specified elements, regardless of order or extra elements.
Question 7: What argument types does jasmine.stringMatching() accept?
- A string or a RegExp (Correct answer)
- Only a string
- Only a RegExp
- A string and a flags object
Correct answer: A string or a RegExp
jasmine.stringMatching() accepts either a string (checks for substring match) or a RegExp (checks against the pattern), making it a flexible asymmetric matcher.
Which Jasmine method is used to register custom matchers so they are available in specs?