Jasmine JavaScript Testing Framework Jasmine Async Testing 2 — Questions and Answers
Question 1: What does done.fail('reason') do inside a Jasmine async test?
- Immediately fails the test with the provided reason (Correct answer)
- Stops execution without recording a failure
- Causes Jasmine to retry the test
- Logs the reason and continues running
Correct answer: Immediately fails the test with the provided reason
done.fail() is the mechanism for failing an async test from within a callback when an unexpected condition is detected.
Question 2: Which Jasmine feature allows you to control setTimeout and setInterval in tests?
- jasmine.clock() (Correct answer)
- jasmine.fakeTimers()
- jasmine.mockTime()
- jasmine.timerControl()
Correct answer: jasmine.clock()
jasmine.clock() provides a mock clock that lets you control time-based functions like setTimeout and setInterval.
Question 3: How do you activate Jasmine's mock clock before a test?
- jasmine.clock().install() (Correct answer)
- jasmine.clock().start()
- jasmine.useFakeTimers()
- jasmine.clock().mock()
Correct answer: jasmine.clock().install()
install() activates the mock clock, replacing the browser/Node timer functions with Jasmine's controlled versions.
Question 4: After installing the Jasmine clock mock, how do you advance time by 1000ms?
- jasmine.clock().tick(1000) (Correct answer)
- jasmine.clock().advance(1000)
- jasmine.clock().run(1000)
- jasmine.clock().skip(1000)
Correct answer: jasmine.clock().tick(1000)
tick(ms) synchronously advances the mocked clock by the given milliseconds, triggering any due timers.
Question 5: What must you do after each test that installed the Jasmine clock mock?
- Call jasmine.clock().uninstall() (Correct answer)
- Call jasmine.clock().reset()
- Call jasmine.clock().restore()
- Call jasmine.clock().stop()
Correct answer: Call jasmine.clock().uninstall()
uninstall() removes the mock clock and restores the real timer functions, preventing leakage into subsequent tests.
Question 6: What is the recommended way to test a rejected Promise in Jasmine?
- Return the Promise with a .catch that calls done.fail()
- Use expectAsync(promise).toBeRejected()
- Both A and B are valid approaches (Correct answer)
- Use jasmine.rejectPromise(promise)
Correct answer: Both A and B are valid approaches
Both returning a Promise with .catch/done.fail and using the expectAsync async matcher are valid Jasmine patterns.
What does done.fail('reason') do inside a Jasmine async test?