Jasmine JavaScript Testing Framework Jasmine JavaScript 5 — Questions and Answers
Question 1: What does `jasmine.stringMatching()` accept as its argument?
- A string or regular expression to match against (Correct answer)
- Only an exact string for strict equality
- Only a RegExp object
- A function that returns a boolean
Correct answer: A string or regular expression to match against
`jasmine.stringMatching()` accepts either a substring or a RegExp and checks the actual value against it.
Question 2: How do you run only a specific `describe` block in Jasmine while skipping all others?
- Change `describe` to `fdescribe` (Correct answer)
- Change `describe` to `xdescribe`
- Add `.only` to the describe call
- Use `jasmine.focus('suiteName')`
Correct answer: Change `describe` to `fdescribe`
`fdescribe` (focused describe) causes Jasmine to run only that suite and skip all unfocused specs.
Question 3: What information does `spy.calls.all()` provide?
- An array of call objects, each with args, object, and returnValue (Correct answer)
- Just the argument arrays for every call
- A count of total calls made
- The first and last call objects only
Correct answer: An array of call objects, each with args, object, and returnValue
`calls.all()` returns an array where each element is a call record containing context, arguments, and return value.
Question 4: Which Jasmine configuration option sets the default timeout for async specs?
- jasmine.DEFAULT_TIMEOUT_INTERVAL (Correct answer)
- jasmine.config.timeout
- jasmine.setAsyncTimeout(ms)
- jasmine.env.timeoutInterval
Correct answer: jasmine.DEFAULT_TIMEOUT_INTERVAL
Setting `jasmine.DEFAULT_TIMEOUT_INTERVAL` changes the maximum time Jasmine waits for `done` to be called.
Question 5: What does using `and.stub()` on an existing spy do?
- Resets the spy so it returns undefined and does nothing (Correct answer)
- Removes the spy and restores the original function
- Makes the spy record calls without any action
- Configures the spy to throw on the next call
Correct answer: Resets the spy so it returns undefined and does nothing
`and.stub()` clears any configured behavior (e.g., callThrough or returnValue) so the spy becomes a no-op returning `undefined`.
Question 6: How do you add a custom equality tester in Jasmine?
- jasmine.addCustomEqualityTester(fn) (Correct answer)
- jasmine.customEqualityTesters.push(fn)
- jasmine.env.registerEqualityTester(fn)
- expect.addEqualityTester(fn)
Correct answer: jasmine.addCustomEqualityTester(fn)
`jasmine.addCustomEqualityTester()` registers a function that Jasmine consults when comparing two values for equality.
Question 7: What is the correct way to use `jasmine.clock()` to test a `setInterval` callback?
- Install the clock, trigger tick() by the interval duration, then uninstall (Correct answer)
- Use jasmine.clock().runAll() to flush all intervals at once
- Spy on setInterval and call its callback manually
- Wrap the setInterval call in a Promise and await it
Correct answer: Install the clock, trigger tick() by the interval duration, then uninstall
Installing the fake clock, calling `tick()` with the interval amount, and then uninstalling cleanly exercises the interval callback.
What does `jasmine.stringMatching()` accept as its argument?