Jasmine JavaScript Testing Framework — Questions and Answers
Question 1: What does expectAsync(promise).toBeResolvedTo(value) check?
- The Promise resolves before a given timeout
- The Promise invokes value as a callback on resolution
- The resolved value's type matches the expected value's type
- The Promise resolves with a value deeply equal to the expected value (Correct answer)
Correct answer: The Promise resolves with a value deeply equal to the expected value
toBeResolvedTo(value) uses deep equality to compare the resolved value against the expected value.
Question 2: Which Jasmine method is used to register custom matchers so they are available in specs?
- jasmine.extendMatchers()
- jasmine.addMatchers() (Correct answer)
- jasmine.createMatcher()
- jasmine.registerMatcher()
Correct answer: jasmine.addMatchers()
jasmine.addMatchers() accepts an object map of matcher names to factory functions and makes them available inside describe/it blocks.
Question 3: What does .and.throwError('msg') do when configured on a Jasmine spy?
- Makes the spy throw an Error with that message when called (Correct answer)
- Marks the spy as expected to throw
- Logs the message to the console on each call
- Catches errors thrown inside the real method
Correct answer: Makes the spy throw an Error with that message when called
throwError('msg') configures the spy to throw a new Error with the given message every time it is invoked.
Question 4: What function creates a spy on an existing object method in Jasmine?
- mockMethod(obj, 'method')
- interceptMethod(obj, 'method')
- spyOn(obj, 'method') (Correct answer)
- createSpy(obj, 'method')
Correct answer: spyOn(obj, 'method')
spyOn(obj, 'method') replaces the named method on obj with a Jasmine spy that tracks calls.
Question 5: What is the effect of having a focused spec (fit) or suite (fdescribe) in a Jasmine test file?
- Only focused specs and suites run; all others are skipped (Correct answer)
- Focused specs are run twice to detect flakiness
- Focused specs run first, then all others run normally
- Focused specs are highlighted but all specs still run
Correct answer: Only focused specs and suites run; all others are skipped
When Jasmine detects any focused spec or suite, it runs only those items, making focus a powerful way to isolate debugging.
Question 6: What does `spy.and.returnValue(42)` do?
- Stores 42 as a call argument
- Asserts the spy returns 42
- Makes the spy always return 42 when called (Correct answer)
- Throws an error if the spy doesn't return 42
Correct answer: Makes the spy always return 42 when called
`and.returnValue` configures the spy to return the specified value every time it is invoked.
Question 7: Which function marks a single spec as focused so only it runs in a suite?
- fit() (Correct answer)
- only()
- focus()
- fspec()
Correct answer: fit()
fit() (focused it) causes Jasmine to run only that spec and skip all non-focused specs in the suite.
Question 8: How does Jasmine test exceptions?
- Use the `catch()` function.
- Use the `try...catch` statement.
- Use the `expectError()` function.
- Use the `toThrowError()` matcher. (Correct answer)
Correct answer: Use the `toThrowError()` matcher.
Jasmine provides the `toThrowError()` matcher to test if a function throws an expected error. This matcher is used within an `expect()` block, wrapping the function call that is expected to throw, and can optionally check for a specific error type or message.
Question 9: What does jasmine.objectContaining({ name: 'Alice' }) do when used in an expectation?
- Fails if the actual object has any extra properties
- Passes if the actual object has at least a name property equal to 'Alice' (Correct answer)
- Checks that the object is a class instance named Alice
- Passes only if the actual object is exactly { name: 'Alice' }
Correct answer: Passes if the actual object has at least a name property equal to 'Alice'
jasmine.objectContaining() is an asymmetric matcher that passes as long as the specified key-value pairs are present, ignoring any additional properties.
Question 10: What does calling `pending()` inside a Jasmine spec do?
- Logs a warning and continues
- Skips subsequent describes only
- Marks the spec as pending regardless of other assertions (Correct answer)
- Fails the spec
Correct answer: Marks the spec as pending regardless of other assertions
Calling pending() anywhere in a spec immediately marks it as pending and stops execution of that spec.
Question 11: Which matcher would you use to confirm a value is `undefined`?
- toBeNull()
- toEqual(undefined)
- toBeFalsy()
- toBeUndefined() (Correct answer)
Correct answer: toBeUndefined()
`toBeUndefined()` specifically checks that the value is strictly `undefined`.
Question 12: Which property on a Jasmine call object holds the value the spy returned?
- .returned
- .result
- .value
- .returnValue (Correct answer)
Correct answer: .returnValue
Each call object in spy.calls has a returnValue property storing whatever that invocation returned.
Question 13: What role does jasmine.objectContaining({key: value}) play in an expectation?
- Exactly matches only an object with exactly those properties
- Checks whether the object has more properties than listed
- Creates a spy that intercepts the listed properties
- Partially matches an object that has at least those key-value pairs (Correct answer)
Correct answer: Partially matches an object that has at least those key-value pairs
objectContaining is an asymmetric matcher that passes if the actual object includes the listed properties, ignoring extras.
Question 14: Which approach correctly checks that a spy was called exactly two times?
- expect(spy).toHaveBeenCalledTimes(2)
- expect(spy.calls.count()).toBe(2)
- expect(spy).calledTwice()
- Both A and B are valid (Correct answer)
Correct answer: Both A and B are valid
Jasmine supports both the toHaveBeenCalledTimes matcher and manually asserting spy.calls.count() with toBe.
Question 15: What must you do after each test that installed the Jasmine clock mock?
- Call jasmine.clock().reset()
- Call jasmine.clock().restore()
- Call jasmine.clock().stop()
- Call jasmine.clock().uninstall() (Correct answer)
Correct answer: Call jasmine.clock().uninstall()
uninstall() removes the mock clock and restores the real timer functions, preventing leakage into subsequent tests.
Question 16: What does jasmine.createSpyObj('MyClass', ['save', 'load']) return?
- A single spy wrapping MyClass
- A plain object with save and load as spy functions (Correct answer)
- A spy attached to the global window object
- An array of two spy functions
Correct answer: A plain object with save and load as spy functions
createSpyObj creates a mock object whose named methods are all individual spies you can configure.
Question 17: What does toThrow() verify about a function passed to expect()?
- That the function throws with a specific message
- That the function never completes execution
- That the function throws any exception when called (Correct answer)
- That the function throws a specific error type
Correct answer: That the function throws any exception when called
toThrow() with no arguments passes as long as the function throws anything at all; use toThrowError() for specifics.
Question 18: What happens when a Jasmine spec's `expect` assertion fails?
- Only that spec is marked as failed; subsequent specs still run (Correct answer)
- The browser reloads automatically
- The entire test file stops executing
- Jasmine exits with a fatal error
Correct answer: Only that spec is marked as failed; subsequent specs still run
A failed expectation marks the spec as failed but Jasmine continues running remaining specs in the suite.
Question 19: What happens if done() is never called in a Jasmine async test?
- Jasmine automatically retries the test
- The test is skipped in the results
- The test passes with a warning
- The test times out and is marked as failed (Correct answer)
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 20: After installing the Jasmine clock mock, how do you advance time by 1000ms?
- jasmine.clock().run(1000)
- jasmine.clock().tick(1000) (Correct answer)
- jasmine.clock().advance(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 21: What is the purpose of jasmine.nothing() used as an asymmetric matcher?
- Asserts the value is null
- Asserts the function returns undefined
- Asserts that a spy was called with no arguments or matches any call with no arguments (Correct answer)
- Asserts there are no properties on the object
Correct answer: Asserts that a spy was called with no arguments or matches any call with no arguments
jasmine.nothing() is an asymmetric equality tester that only passes when compared to no value, typically used with toHaveBeenCalledWith() to assert a spy was called with zero arguments.
Question 22: What is the recommended way to test a rejected Promise in Jasmine?
- Both A and B are valid approaches (Correct answer)
- Use expectAsync(promise).toBeRejected()
- Return the Promise with a .catch that calls done.fail()
- 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.
Question 23: What does `afterAll` do in a Jasmine test suite?
- Marks the suite as complete to the reporter
- 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
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.
Question 24: Which property returns the number of times a Jasmine spy was called?
- spy.times()
- spy.callCount
- spy.invocationCount
- spy.calls.count() (Correct answer)
Correct answer: spy.calls.count()
spy.calls.count() is the method on the calls object that returns the total invocation count.
Question 25: What must a custom matcher factory function return?
- An object with a compare function (Correct answer)
- A string describing the matcher
- A boolean value
- A Promise
Correct answer: An object with a compare function
A custom matcher factory returns an object that must include a compare function, which Jasmine calls with the actual and expected values.
Jasmine JavaScript Testing Framework
A skills assessment covering the Jasmine BDD testing framework for JavaScript, including test suites, matchers, spies, mocking, and asynchronous testing patterns. Tests practical knowledge of writing and organizing unit tests with Jasmine.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds