Why is it advantageous to use stubbing when dealing with external services or APIs in tests?
- Stubbing allows you to make actual API calls during testing.
- Stubbing provides better performance in tests.
- Stubbing isolates your tests from the external services, making tests more reliable and faster.
- Stubbing is required by external services for testing.
It is advantageous to use stubbing when dealing with external services or APIs in tests because stubbing isolates your tests from the actual external services, making tests more reliable and faster. With stubbing, you can control the responses from external services, ensuring that your tests are not affected by changes or issues with the real services. Options (1), (2), and (4) do not accurately describe the advantages of stubbing in this context.
When publishing a package to the NPM registry, what file is crucial to define the package properties and dependencies?
- package-config.json
- dependencies.json
- package-lock.json
- package.json
When publishing a package to the NPM registry, the package.json file is crucial. This file contains metadata about the package, including its name, version, description, entry points, and most importantly, its dependencies. The package-lock.json file is used to lock dependency versions but is not responsible for defining the package properties. Options 1 and 2 do not exist, and option 3, while related, is not used for defining package properties.
You need to expose a global utility function that should be accessible across different modules in your Node.js application. How would you leverage the global object to achieve this?
- global.utility = require('./utility');
- global.util = require('./utility');
- global.import('./utility')
- global.include('./utility')
To expose a global utility function in Node.js, you can use global.utility = require('./utility');. This allows you to require the module once and make it accessible globally across different modules. The other options do not achieve this in the correct way.
When would you use export default over named exports in a module?
- export default is used when you want to export multiple values from a module as an object with named keys.
- export default is used when you want to export a single value, function, or class from a module.
- export default is used when you want to export a module without specifying a name for it.
- export default is used when you want to create a private module that can't be imported from other modules.
export default is used when you want to export a single value, function, or class as the default export of a module. This allows you to import it using any name you prefer when importing. Named exports are used when you want to export multiple values with specific names.
What does the return statement do in a JavaScript function?
- Returns a value from the function and exits the function
- Declares a variable
- Creates a loop
- Includes a comment in the code
The return statement in a JavaScript function is used to return a value from the function and immediately exit the function. It is used to send data back to the caller of the function. The other options do not describe the purpose of the return statement.
How does the use of mocking and stubbing affect the isolation of unit tests?
- It increases isolation by replacing real dependencies with controlled substitutes.
- It reduces isolation by relying on actual dependencies.
- It has no effect on isolation.
- It increases isolation by running tests concurrently.
The use of mocking and stubbing in unit tests increases isolation by replacing real dependencies, such as external services or APIs, with controlled substitutes. This allows you to focus on testing the specific unit of code without worrying about the behavior of the real dependencies. Option (2) is incorrect because relying on actual dependencies doesn't create isolation in unit tests. Options (3) and (4) are also incorrect.
How can middleware be added to an Express application to process requests?
- Using the use() method
- Using the get() method
- Using the addMiddleware() function
- Using the handleRequest() method
Middleware can be added to an Express application using the use() method. This method allows you to specify middleware functions that can process requests in the order they are defined. The other options are not standard ways of adding middleware in Express.
Which of the following array methods does not mutate the original array in JavaScript?
- splice()
- push()
- concat()
- pop()
The concat() method in JavaScript creates a new array that combines the elements of the original array and the elements passed as arguments, without modifying the original array. The other methods, such as splice(), push(), and pop(), directly modify the original array.
The fs.createReadStream method is particularly useful when dealing with ______ sized files.
- small
- large
- binary
- text
The fs.createReadStream method in Node.js is particularly useful when dealing with large sized files. This method allows you to efficiently read large files in smaller chunks, which can be memory-efficient for handling large datasets.
What happens to the Event Loop when the callback queue and the task queue are both not empty?
- It suspends the Event Loop until both queues are empty.
- It processes callbacks from the callback queue before tasks from the task queue.
- It processes tasks from the task queue before callbacks from the callback queue.
- It randomly selects items to process from both queues.
When both the callback queue and the task queue are not empty, the Event Loop in Node.js follows a specific order. It first processes callbacks from the callback queue before moving to tasks from the task queue. This order ensures that callback functions, which often include I/O operations, are handled promptly.