Jasmine JavaScript Testing Framework Jasmine JavaScript Knowledge 3 — Questions and Answers
Question 1: In Jasmine, which function is used to handle asynchronous tests by signaling completion?
- done() (Correct answer)
- resolve()
- async()
- complete()
Correct answer: done()
Jasmine passes a `done` callback to async specs; calling done() tells Jasmine the async work has finished.
Question 2: What is the default timeout for an asynchronous Jasmine spec before it fails?
- 5000 ms (Correct answer)
- 2000 ms
- 10000 ms
- 1000 ms
Correct answer: 5000 ms
Jasmine's default async timeout is 5000 milliseconds (5 seconds) before the spec is marked as failed.
Question 3: Which Jasmine feature allows returning a Promise from a spec instead of using the `done` callback?
- Native Promise support in async specs (Correct answer)
- jasmine.promisify()
- done.resolve()
- jasmine.async()
Correct answer: Native Promise support in async specs
Modern Jasmine treats a returned Promise as the async signal; the spec passes when the Promise resolves.
Question 4: How do you use async/await syntax in a Jasmine spec?
- Mark the callback as async and use await inside (Correct answer)
- Use jasmine.async() wrapper
- Call done.async()
- Use jasmine.awaitSpec()
Correct answer: Mark the callback as async and use await inside
You can mark the spec callback with async and use await; Jasmine detects the returned Promise automatically.
Question 5: What does calling `done.fail('reason')` do inside an async Jasmine spec?
- Fails the spec immediately with the given message (Correct answer)
- Marks the spec as pending
- Throws an unhandled error
- Skips remaining assertions
Correct answer: Fails the spec immediately with the given message
done.fail() forces the spec to fail and report the provided message without waiting for timeout.
Question 6: Which Jasmine clock method allows you to manually advance time in tests involving setTimeout?
- jasmine.clock().tick(ms) (Correct answer)
- jasmine.clock().advance(ms)
- jasmine.clock().run(ms)
- jasmine.clock().forward(ms)
Correct answer: jasmine.clock().tick(ms)
After installing the mock clock with jasmine.clock().install(), tick(ms) simulates the passage of that many milliseconds.
Question 7: What must you call before using jasmine.clock() to prevent it from affecting other tests?
- jasmine.clock().install() (Correct answer)
- jasmine.clock().init()
- jasmine.clock().start()
- jasmine.clock().enable()
Correct answer: jasmine.clock().install()
jasmine.clock().install() replaces the native timer functions; call .uninstall() in afterEach to restore them.
In Jasmine, which function is used to handle asynchronous tests by signaling completion?