Jasmine JavaScript Testing Framework Jasmine JavaScript 4 — Questions and Answers
Question 1: How do you make a Jasmine spy return different values on successive calls?
- and.returnValues(val1, val2, val3) (Correct answer)
- and.returnValue([val1, val2, val3])
- and.callFake(() => vals.shift())
- spy.sequence([val1, val2, val3])
Correct answer: and.returnValues(val1, val2, val3)
`and.returnValues()` accepts multiple arguments and returns them in sequence on each successive call.
Question 2: Which Jasmine matcher verifies that a function throws an error with a specific message?
- toThrowError(message) (Correct answer)
- toThrow(message)
- toThrowWithMessage(Error, message)
- toThrowMatching(fn)
Correct answer: toThrowError(message)
`toThrowError()` can accept an error type, message string, or regex to match against the thrown error.
Question 3: What is the role of `jasmine.clock()` in Jasmine tests?
- Replaces the native timer functions with controllable mock timers (Correct answer)
- Measures how long a spec takes to execute
- Schedules async specs to run at a future time
- Synchronizes parallel describe blocks
Correct answer: Replaces the native timer functions with controllable mock timers
`jasmine.clock()` installs fake `setTimeout`/`setInterval` implementations that can be advanced manually.
Question 4: After calling `jasmine.clock().install()`, how do you advance fake timers by 500ms?
- jasmine.clock().tick(500) (Correct answer)
- jasmine.clock().advance(500)
- jasmine.clock().run(500)
- jasmine.clock().flush(500)
Correct answer: jasmine.clock().tick(500)
`tick(ms)` moves the fake clock forward by the specified number of milliseconds, firing any due timers.
Question 5: What does `spy.calls.mostRecent()` return?
- An object with `object` and `args` properties for the last call (Correct answer)
- Only the arguments of the last call
- The return value of the last call
- The number of the most recent call
Correct answer: An object with `object` and `args` properties for the last call
`calls.mostRecent()` returns a call object containing `object` (context), `args`, and `returnValue` for the latest invocation.
Question 6: Which approach correctly tests a Promise-based function in Jasmine using `done`?
- Call done() inside .then() and done.fail() inside .catch() (Correct answer)
- Return the promise without using done
- Wrap the promise in jasmine.waitFor()
- Use expect(promise).toResolve()
Correct answer: Call done() inside .then() and done.fail() inside .catch()
Calling `done()` in `.then()` and `done.fail()` in `.catch()` correctly signals async completion or failure to Jasmine.
Question 7: What is the effect of `and.throwError('message')` on a Jasmine spy?
- Causes the spy to throw an error with the given message when called (Correct answer)
- Logs an error without stopping execution
- Marks the spec as failed immediately
- Causes the spy to return an Error object
Correct answer: Causes the spy to throw an error with the given message when called
`and.throwError()` configures the spy to throw an `Error` with the specified message every time it is invoked.
How do you make a Jasmine spy return different values on successive calls?