Jasmine JavaScript Testing Framework Jasmine JavaScript for Beginner's 5 — Questions and Answers
Question 1: What is a Jasmine custom matcher and how is it added?
- A built-in Jasmine extension added via jasmine.addCustomEqualityTester()
- A user-defined matcher added via jasmine.addMatchers() in beforeEach (Correct answer)
- A third-party plugin loaded from npm automatically
- A CSS class applied to failing specs in the HTML reporter
Correct answer: A user-defined matcher added via jasmine.addMatchers() in beforeEach
Custom matchers are objects passed to `jasmine.addMatchers()` inside a `beforeEach`, giving tests domain-specific assertion language.
Question 2: How do you configure Jasmine to run only specs whose descriptions match a pattern?
- Use jasmine.filter('pattern') before the suite
- Pass --filter='pattern' on the command line or set spec_filter in jasmine.json (Correct answer)
- Wrap desired specs in a focus.describe block
- Use jasmine.only('pattern')
Correct answer: Pass --filter='pattern' on the command line or set spec_filter in jasmine.json
The `spec_filter` option in `jasmine.json` (or the CLI `--filter` flag) limits which specs run based on a substring or regex match.
Question 3: What does `spy.and.throwError('message')` do when the spy is invoked?
- Logs the error message to the console
- Makes the spy throw an Error with the given message (Correct answer)
- Marks the spec as pending
- Returns the error message as a string
Correct answer: Makes the spy throw an Error with the given message
`and.throwError` configures the spy to throw an Error object containing the specified message whenever called.
Question 4: Which reporter does Jasmine provide for running tests in a Node.js environment via the CLI?
- HtmlReporter
- ConsoleReporter (Correct answer)
- JUnitReporter
- TerminalReporter
Correct answer: ConsoleReporter
The `ConsoleReporter` (also called the spec reporter) outputs results to stdout, making it the default for Jasmine CLI/Node runs.
Question 5: What does `toMatch` use internally to test string values?
- A substring indexOf check
- A regular expression or string pattern (Correct answer)
- An exact `===` comparison
- A Levenshtein distance threshold
Correct answer: A regular expression or string pattern
`toMatch(pattern)` accepts either a RegExp or a string that is converted to a RegExp, then tests the actual string against it.
Question 6: In Jasmine, what does `jasmine.objectContaining({key: value})` do?
- Asserts the object has no extra keys beyond those listed
- Performs a partial match — the object must have at least the specified keys/values (Correct answer)
- Converts the object to JSON before comparing
- Checks only the first level of nested properties
Correct answer: Performs a partial match — the object must have at least the specified keys/values
`jasmine.objectContaining` creates an asymmetric matcher that passes if the object contains at least the given key-value pairs, ignoring extra properties.
Question 7: What is the difference between `fdescribe` and `fit` in Jasmine?
- fdescribe focuses all tests in a file; fit focuses a single spec in all files
- fdescribe focuses an entire describe block; fit focuses a single spec within any block (Correct answer)
- They are aliases for the same behavior
- fdescribe is for async suites; fit is for sync specs only
Correct answer: fdescribe focuses an entire describe block; fit focuses a single spec within any block
`fdescribe` marks an entire suite to run exclusively, while `fit` marks a single spec to run exclusively, with all non-focused tests skipped.
What is a Jasmine custom matcher and how is it added?