Free Jasmine JavaScript Knowledge Questions and Answers — Questions and Answers
Question 1: How does Jasmine test exceptions?
- Use the `toThrowError()` matcher. (Correct answer)
- Use the `catch()` function.
- Use the `try...catch` statement.
- Use the `expectError()` function.
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 2: Which Jasmine function is employed in a test specification to simulate asynchronous operations?
- `done()` (Correct answer)
- `async()`
- `fakeAsync()`
- `setTimeout()`
Correct answer: `done()`
The `done()` function is a crucial part of Jasmine's asynchronous testing capabilities. When a test function accepts `done` as an argument, Jasmine waits for `done()` to be called before concluding the test, ensuring that asynchronous operations like callbacks or promises have completed their execution.
Question 3: Which Jasmine function is used to specify a test suite?
- `describe()` (Correct answer)
- `it()`
- `suite()`
- `test()`
Correct answer: `describe()`
In Jasmine, the `describe()` function is used to define a test suite, which is a collection of related test specifications. It takes a string description and a function containing the test specs, organizing tests into logical groups for better readability and structure.
Question 4: Which Jasmine function is used to specify a test specification?
- `describe()`
- `test()`
- `spec()`
- `it()` (Correct answer)
Correct answer: `it()`
The `it()` function in Jasmine is used to define an individual test specification, often referred to as a 'spec'. Each `it()` block contains the actual test code and assertions, describing a specific behavior or outcome that the code under test should exhibit.
Question 5: What does Jasmine's "beforeEach()" function do?
- Defines a custom matcher
- Defines a new test suite
- Runs code before each test spec within a suite (Correct answer)
- Asserts a condition in a test
Correct answer: Runs code before each test spec within a suite
Jasmine's `beforeEach()` function is a setup hook that runs a specified block of code before each individual test specification (`it()` block) within its parent `describe()` suite. This is useful for initializing common test data or setting up the environment consistently for every test.
Question 6: Which Jasmine method does your test specification's assertion use?
- `verify()`
- `assert()`
- `validate()`
- `expect()` (Correct answer)
Correct answer: `expect()`
The `expect()` function is the primary method used in Jasmine for making assertions within a test specification. It takes the actual value as an argument and returns an `Expectation` object, which can then be chained with various matchers to compare the actual value against an expected value.
Question 7: What does Jasmine's "matcher" mean?
- A function that runs after test spec
- A function that defines a new test suite
- A function that sets up test data
- A function that compares actual and expected values in a test (Correct answer)
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 8: Which matcher does Jasmine employ to determine whether two values are equal?
- toEqual() (Correct answer)
- toBe()
- isEqual()
- toBeEqual()
Correct answer: toEqual()
Jasmine employs the `toEqual()` matcher to determine whether two values are deeply equal. This means it recursively checks the properties of objects and elements of arrays, making it suitable for comparing complex data structures as well as primitive values. In contrast, `toBe()` checks for strict identity (same object in memory).
Question 9: What does Jasmine's spyOn() function accomplish?
- Defines a custom matcher
- Sets up a spy to track function calls (Correct answer)
- Creates a mock function
- Defines a new test spec
Correct answer: Sets up a spy to track function calls
Jasmine's `spyOn()` function is used to create a spy on an existing method of an object. It allows developers to track if the method was called, how many times, and with what arguments, without altering its original implementation. This is crucial for isolating units of code during testing and verifying interactions between different components.
Question 10: What is a "fixture" in Jasmine?
- A sample input used for testing (Correct answer)
- A synonym for a test spec
- A function that defines a test suite
- A custom assertion function
Correct answer: A sample input used for testing
In Jasmine, a 'fixture' typically refers to a piece of pre-defined HTML or data that sets up a consistent environment for a test. It ensures that each test runs against the same initial state, making tests reliable and repeatable. This is particularly useful for testing UI components that interact with the Document Object Model (DOM).
Question 11: Which Jasmine function is used to indicate that a test specification is pending?
- ignore()
- it()
- skip()
- pending() (Correct answer)
Correct answer: pending()
The `pending()` function in Jasmine is used within a test specification (`it` block) to explicitly mark it as pending. When Jasmine runs, pending tests are reported separately and are not executed, indicating that they are not yet ready or are temporarily disabled. This helps developers keep track of incomplete tests without failing the build.
How does Jasmine test exceptions?