Free Jasmine JavaScript for Beginner's Questions and Answers — Questions and Answers
Question 1: Which of the following is a Jasmine.js substitute?
- Tape
- Mocha
- Jest
- All of the above (Correct answer)
Correct answer: All of the above
Jasmine is a popular JavaScript testing framework, but there are several other robust alternatives available in the ecosystem. Mocha, Jest, and Tape are all widely used testing frameworks that provide similar functionalities for writing and running JavaScript tests. Each has its own philosophy and features, catering to different development preferences and project needs.
Question 2: The __ method is called when each test specification has been completed.
- EachAfter()
- Each ()
- AfterEach () (Correct answer)
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 3: Are toBeTruthy () and toBeFalse () equivalent?
- Yes
- No (Correct answer)
Correct answer: No
`toBeTruthy()` and `toBeFalse()` are not equivalent matchers in Jasmine; they assert opposite conditions. `toBeTruthy()` checks if a value evaluates to `true` in a boolean context (e.g., non-empty strings, non-zero numbers, objects), while `toBeFalse()` specifically checks if a value is strictly `false`. For example, `0` is `toBeFalsy()` but not `toBeFalse()`.
Question 4: Which of the aforementioned businesses employs Jasmine?
- Walmart
- Accenture
- Gitlab
- E-Commerce
- All of the above (Correct answer)
Correct answer: All of the above
Jasmine is a widely adopted JavaScript testing framework used by a diverse range of companies, from large enterprises like Walmart and Accenture to tech-focused companies like GitLab, and various e-commerce platforms. Its popularity stems from its clear syntax, comprehensive features, and ease of integration into different development workflows, making it suitable for projects of all sizes.
Question 5: Is the function It() a global or local function?
- Local
- Global (Correct answer)
Correct answer: Global
The `it()` function in Jasmine, which defines an individual test specification, is a global function. This means it can be called directly anywhere within your test files without needing to be imported or referenced from a specific object. Jasmine injects these core functions into the global scope to simplify test writing.
Question 6: When was Jasmine.js made available?
- 2000
- 2001
- 2019
- 2010 (Correct answer)
Correct answer: 2010
Jasmine.js was first released in 2010. Since its initial release, it has evolved through several versions, gaining popularity as a behavior-driven development (BDD) framework for testing JavaScript code. Its consistent API and comprehensive features have contributed to its sustained use in the web development community.
Question 7: To see if any variables were previously undefined, ___ check.
- toBeUndefined () (Correct answer)
- toBeNotDefined ()
- toUndefined()
Correct answer: toBeUndefined ()
The `toBeUndefined()` matcher in Jasmine is used to assert that a variable or property currently holds the value `undefined`. This is distinct from `null` or other falsy values and is specifically useful for verifying that a variable has not been assigned a value or that a property does not exist on an object.
Question 8: Which of the following commands controls the Jasmine Clock, which you can use to control how much time passes during your tests?
- Jasmine.Clock() (Correct answer)
- toBeClock ()
- Clock ()
Correct answer: Jasmine.Clock()
`jasmine.clock()` is the object used to control Jasmine's mock clock functionality. It allows developers to manage and advance time within tests, which is essential for testing asynchronous code that relies on `setTimeout`, `setInterval`, or `Date` objects. By controlling the clock, tests can run much faster and more predictably.
Question 9: The Jasmine framework includes a feature called ___ that lets you spy on a particular line of code.
- CreateSpy ()
- SpyOn() (Correct answer)
- Spy ()
Correct answer: SpyOn()
`spyOn()` is the primary Jasmine feature used to create a spy on an existing method of an object. It allows you to observe calls to that method, track its arguments, and even change its implementation temporarily. This is invaluable for isolating units under test and verifying interactions with dependencies.
Question 10: How many different spy technologies are there in Jasmine?
- 2 (Correct answer)
- 5
- 4
- 3
Correct answer: 2
Jasmine primarily offers two ways to create spies: `spyOn()` and `jasmine.createSpy()`. `spyOn()` is used to spy on an *existing* method of an object, while `jasmine.createSpy()` creates a *standalone* spy function that isn't attached to any object. Both serve to track function calls and control behavior during testing.
Question 11: We use the unique matcher known as ___ when we are unsure of the output.
- All
- Any (Correct answer)
- None
- Some
Correct answer: Any
The `jasmine.any()` matcher is a special type of matcher used when you want to assert that a value is of a certain type, but you don't care about its specific value. For example, `expect(myFunction).toHaveBeenCalledWith(jasmine.any(Number))` asserts that the function was called with any number, regardless of its magnitude. This is useful for flexible assertions.
Which of the following is a Jasmine.js substitute?