Jasmine JavaScript Testing Framework Jasmine Async Testing 1 — Questions and Answers
Question 1: How does Jasmine signal that a test function is asynchronous using the callback pattern?
- By accepting a done parameter in the test function (Correct answer)
- By returning jasmine.async()
- By wrapping code in jasmine.wait()
- By calling jasmine.beginAsync() at the start
Correct answer: By accepting a done parameter in the test function
When it() or beforeEach() receives a function with a done parameter, Jasmine waits for done() to be called before proceeding.
Question 2: What happens if done() is never called in a Jasmine async test?
- The test times out and is marked as failed (Correct answer)
- The test passes with a warning
- Jasmine automatically retries the test
- The test is skipped in the results
Correct answer: The test times out and is marked as failed
Jasmine has a configurable timeout interval, and any async test that doesn't call done within that window is failed.
Question 3: How do you handle async testing with Promises in Jasmine?
- Return the Promise from the it() block (Correct answer)
- Call done() inside the Promise .then()
- Wrap the Promise with jasmine.promise()
- Use jasmine.whenResolved() on the Promise
Correct answer: Return the Promise from the it() block
When it() returns a Promise, Jasmine awaits its resolution or rejection to determine pass or fail.
Question 4: What is the default async timeout interval in Jasmine?
- 5000 milliseconds (Correct answer)
- 2000 milliseconds
- 10000 milliseconds
- 1000 milliseconds
Correct answer: 5000 milliseconds
Jasmine's DEFAULT_TIMEOUT_INTERVAL defaults to 5000ms, after which async tests that haven't completed are failed.
Question 5: How do you globally change the async timeout for all Jasmine tests?
- jasmine.DEFAULT_TIMEOUT_INTERVAL = newValue (Correct answer)
- jasmine.timeout = newValue
- jasmine.setTimeoutInterval(newValue)
- jasmine.config.timeout = newValue
Correct answer: jasmine.DEFAULT_TIMEOUT_INTERVAL = newValue
Setting jasmine.DEFAULT_TIMEOUT_INTERVAL overrides the 5000ms default for all async specs in the suite.
Question 6: How do you use async/await syntax in a Jasmine it() block?
- Declare the function as async and use await inside (Correct answer)
- Pass done and call it after each await
- Wrap the function in jasmine.async()
- Return jasmine.awaitAll() at the end
Correct answer: Declare the function as async and use await inside
Jasmine supports native async functions; simply declare the callback as async and use await normally.
How does Jasmine signal that a test function is asynchronous using the callback pattern?