Jasmine JavaScript Testing Framework Jasmine JavaScript 3 — Questions and Answers
Question 1: What does `jasmine.arrayContaining()` check?
- That the actual array contains at least all elements in the expected array (Correct answer)
- That two arrays are strictly equal
- That an array is sorted in the expected order
- That an array has exactly the expected length
Correct answer: That the actual array contains at least all elements in the expected array
`jasmine.arrayContaining()` passes when the actual array includes every element listed in the argument.
Question 2: How do you prevent a test in Jasmine from running without deleting it?
- Change `it` to `xit` (Correct answer)
- Wrap the test in `skip()`
- Prefix the test name with `#`
- Use `it.only()` on other tests
Correct answer: Change `it` to `xit`
Changing `it` to `xit` marks the spec as pending so Jasmine skips it and reports it separately.
Question 3: Which Jasmine method lets you inspect what arguments were passed on a spy's nth call?
- spy.calls.argsFor(n) (Correct answer)
- spy.calls.nthCall(n).args
- spy.getArgs(n)
- spy.calls.allArgs()[n]
Correct answer: spy.calls.argsFor(n)
`calls.argsFor(n)` returns the argument array for the call at the specified zero-based index.
Question 4: When using `done` in an async Jasmine spec, what happens if `done` is never called?
- The spec times out and is marked as failed (Correct answer)
- The spec passes by default
- Jasmine skips remaining specs
- An unhandled exception is thrown immediately
Correct answer: The spec times out and is marked as failed
Jasmine waits up to the configured timeout and then fails the spec with a timeout error if `done` is not invoked.
Question 5: What is a Jasmine spy object created with `jasmine.createSpyObj('name', ['method1', 'method2'])`?
- An object whose listed methods are all spies (Correct answer)
- A spy that wraps an existing object's prototype
- A mock class with a constructor spy
- A namespace holding standalone spy functions
Correct answer: An object whose listed methods are all spies
`createSpyObj` returns a plain object where each named method is a pre-created Jasmine spy.
Question 6: Which Jasmine matcher checks that a number is within a given precision of an expected value?
- toBeCloseTo(expected, precision) (Correct answer)
- toBeNear(expected, precision)
- toApproximatelyEqual(expected, decimals)
- toEqualWithin(expected, delta)
Correct answer: toBeCloseTo(expected, precision)
`toBeCloseTo()` compares numbers to a specified number of decimal places to handle floating-point imprecision.
Question 7: What does `afterAll` do in a Jasmine test suite?
- Runs teardown code once after all specs in a describe block finish (Correct answer)
- Runs after each individual spec
- Restores all spies to original implementations
- Marks the suite as complete to the reporter
Correct answer: Runs teardown code once after all specs in a describe block finish
`afterAll` executes a single time after every `it` block in the enclosing `describe` has run.
What does `jasmine.arrayContaining()` check?