Jasmine JavaScript Testing Framework — Questions and Answers
Question 1: What does the compare function inside a custom matcher need to return?
- An object with a pass property (boolean) and optionally a message (Correct answer)
- A new Error object
- A boolean true or false
- The difference between actual and expected
Correct answer: An object with a pass property (boolean) and optionally a message
The compare function must return an object with at least { pass: boolean }, and can include a message property for failure descriptions.
Question 2: How can you create a Jasmine spy for a standalone function (not a method)?
- jasmine.createSpy('name') (Correct answer)
- spyOn(window, 'functionName')
- new jasmine.Spy('name')
- jasmine.createSpyObj(['functionName'])
Correct answer: jasmine.createSpy('name')
`jasmine.createSpy()` creates a bare spy function not attached to any object.
Question 3: When you define a custom matcher with jasmine.addMatchers() inside a beforeEach, in which scope are those matchers available?
- Only in the first it block
- Only in the afterEach of the same describe
- Globally across all specs in the file
- Only within the describe block that contains the beforeEach (Correct answer)
Correct answer: Only within the describe block that contains the beforeEach
Custom matchers registered in a beforeEach are scoped to the describe block containing that beforeEach; they are not inherited by outer or sibling suites.
Question 4: How do you reset all recorded call data for a Jasmine spy?
- spy.calls.reset() (Correct answer)
- spy.reset()
- spy.calls.clear()
- spy.clearCalls()
Correct answer: spy.calls.reset()
spy.calls.reset() clears the internal call log so count, args, and other tracking data start fresh.
Question 5: How does Jasmine use a custom matcher's negated failure message when .not is used?
- It calls negativeCompare if defined, otherwise it uses the default pass negation with the same message (Correct answer)
- It ignores the message property entirely
- It calls the message function with a false argument
- It automatically prepends 'NOT' to the message
Correct answer: It calls negativeCompare if defined, otherwise it uses the default pass negation with the same message
If a negativeCompare function is provided in the matcher object, Jasmine uses it for .not expectations; otherwise it inverts the pass result but uses the existing message.
Question 6: Which Jasmine matcher checks that a string or value matches a regular expression?
- toMatch(regex) (Correct answer)
- toBe(regex)
- toEqual(regex)
- toContain(regex)
Correct answer: toMatch(regex)
toMatch() accepts a RegExp or string pattern and tests the actual value against it.
Question 7: How do you configure a Jasmine spy to invoke the original implementation?
- .and.passThrough()
- .and.original()
- .and.delegate()
- .and.callThrough() (Correct answer)
Correct answer: .and.callThrough()
callThrough() tells the spy to forward the call to the real method while still tracking it.
Question 8: What does `jasmine.stringMatching()` accept as its argument?
- A function that returns a boolean
- A string or regular expression to match against (Correct answer)
- Only an exact string for strict equality
- Only a RegExp object
Correct answer: A string or regular expression to match against
`jasmine.stringMatching()` accepts either a substring or a RegExp and checks the actual value against it.
Question 9: How does Jasmine signal that a test function is asynchronous using the callback pattern?
- By returning jasmine.async()
- By accepting a done parameter in the test function (Correct answer)
- By calling jasmine.beginAsync() at the start
- By wrapping code in jasmine.wait()
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 10: How do you set a custom timeout for a single it() block in Jasmine?
- Call jasmine.setTimeout(timeout) inside the block
- Pass the timeout as a third argument: it('desc', fn, timeout) (Correct answer)
- Use it.timeout('desc', fn, timeout)
- Use it('desc', {timeout: ms}, fn)
Correct answer: Pass the timeout as a third argument: it('desc', fn, timeout)
Jasmine's it() accepts an optional third argument (timeout in ms) that overrides the default for that spec only.
Question 11: Which reporter does Jasmine provide for running tests in a Node.js environment via the CLI?
- ConsoleReporter (Correct answer)
- TerminalReporter
- JUnitReporter
- HtmlReporter
Correct answer: ConsoleReporter
The `ConsoleReporter` (also called the spec reporter) outputs results to stdout, making it the default for Jasmine CLI/Node runs.
Question 12: What does jasmine.createSpyObj('MyClass', ['save', 'load']) return?
- A plain object with save and load as spy functions (Correct answer)
- An array of two spy functions
- A spy attached to the global window object
- A single spy wrapping MyClass
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 13: Which configuration option sets the timeout for async specs in Jasmine?
- jasmine.config.timeout
- jasmine.specTimeout
- jasmine.DEFAULT_TIMEOUT_INTERVAL (Correct answer)
- jasmine.asyncTimeout
Correct answer: jasmine.DEFAULT_TIMEOUT_INTERVAL
`jasmine.DEFAULT_TIMEOUT_INTERVAL` (default 5000ms) determines how long Jasmine waits for an async spec to complete.
Question 14: How do you use async/await syntax in a Jasmine it() block?
- Pass done and call it after each await
- Declare the function as async and use await inside (Correct answer)
- Return jasmine.awaitAll() at the end
- Wrap the function in jasmine.async()
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.
Question 15: The __ method is called when each test specification has been completed.
- EachAfter()
- AfterEach () (Correct answer)
- Each ()
Correct answer: AfterEach ()
The `afterEach()` function in Jasmine is a setup/teardown hook that executes a specified block of code after *each* test specification (`it` block) within its `describe` block has completed. It is commonly used to clean up resources, reset the state, or perform other necessary actions to ensure test isolation and prevent side effects between tests.
Question 16: What does .and.throwError('msg') do when configured on a Jasmine spy?
- Marks the spy as expected to throw
- Catches errors thrown inside the real method
- Makes the spy throw an Error with that message when called (Correct answer)
- Logs the message to the console on each call
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 17: What does Jasmine's "matcher" mean?
- A function that compares actual and expected values in a test (Correct answer)
- A function that sets up test data
- A function that defines a new test suite
- A function that runs after test spec
Correct answer: A function that compares actual and expected values in a test
In Jasmine, a 'matcher' is a function that performs a comparison between an actual value (provided to `expect()`) and an expected value. Matchers return a boolean indicating whether the comparison passed or failed, forming the core of Jasmine's assertion mechanism.
Question 18: 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 19: What happens when you call spyOn on a property that does not exist on the object?
- It ignores the call and continues
- It logs a warning to the console
- Jasmine throws an error (Correct answer)
- It silently creates the property as a spy
Correct answer: Jasmine throws an error
Jasmine requires the property to exist on the object before you can spy on it, and throws if it doesn't.
Question 20: 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
- Logs the reason and continues running
- Causes Jasmine to retry the test
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 21: What distinguishes Jasmine from Mocha?
- Readable syntax
- Presence of test doubles in Jasmine (Correct answer)
- Presence of command line utility in Mocha
- Absence of command line utility in Mocha
Correct answer: Presence of test doubles in Jasmine
A key distinction between Jasmine and Mocha is Jasmine's built-in support for test doubles, such as spies, mocks, and stubs, through functions like `spyOn()`. Mocha, while also a popular JavaScript testing framework, typically requires external libraries like Sinon.js to provide similar test double functionality.
Question 22: Where should jasmine.addMatchers() typically be called to ensure custom matchers are available for every spec in a suite?
- Inside a beforeEach block (Correct answer)
- Inside an it block
- At the top level outside any describe
- Inside an afterEach block
Correct answer: Inside a beforeEach block
Calling jasmine.addMatchers() inside a beforeEach block ensures the custom matchers are registered before each spec runs within that suite.
Question 23: What does the `xdescribe` function do in Jasmine?
- Throws a syntax error
- Marks the suite as focused
- Skips only the first spec
- Disables all specs inside the describe block (Correct answer)
Correct answer: Disables all specs inside the describe block
xdescribe marks an entire describe block as pending, so all specs inside are skipped without being removed.
Question 24: Which Jasmine matcher performs a deep equality check between two objects?
- toEqual() (Correct answer)
- toContain()
- toBe()
- toMatch()
Correct answer: toEqual()
toEqual() recursively compares all properties of objects, while toBe() uses strict reference equality.
Question 25: How do you activate Jasmine's mock clock before a test?
- jasmine.clock().install() (Correct answer)
- jasmine.useFakeTimers()
- jasmine.clock().mock()
- jasmine.clock().start()
Correct answer: jasmine.clock().install()
install() activates the mock clock, replacing the browser/Node timer functions with Jasmine's controlled versions.
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