In the context of testing, what is the main difference between a mock and a stub?
- Stubs are used for functions, while mocks are used for objects
- Mocks record and verify interactions, while stubs only simulate behavior
- Stubs are only used in integration tests, while mocks are used in unit tests
- Mocks are less flexible than stubs
The primary difference between a mock and a stub is that mocks record and verify interactions between the code under test and the dependencies, whereas stubs only simulate the behavior of dependencies. Mocks are used to ensure that specific interactions occur as expected, while stubs focus on controlling the response of functions or methods.
In Express.js, how can middleware be utilized to implement authorization checks for different routes?
- Middleware can't be used for authorization checks in Express.js.
- Middleware functions can be added to route definitions to execute specific authorization logic before handling a request.
- Middleware can only be used for logging purposes in Express.js.
- Authorization checks can only be done in the route handler functions.
Middleware in Express.js is versatile and can be used for various purposes, including implementing authorization checks. By adding middleware functions to specific route definitions, you can ensure that certain authorization logic is executed before the route handler processes the request.
Utilizing closures with caution is essential as they can lead to potential memory leaks due to retained ________.
- Variables
- Functions
- References
- Objects
Closures retain references to their containing scope, which includes variables and functions. If not managed carefully, this can lead to memory leaks as these references may prevent objects from being garbage collected.
How can the process object be used to handle application termination in Node.js?
- process.exit()
- process.end()
- process.terminate()
- process.halt()
In Node.js, you can use process.exit() to gracefully terminate a Node.js application. It allows you to specify an exit code. The other options, such as process.end(), process.terminate(), and process.halt(), are not valid methods for application termination.
Which of the following is a primary use case for stubbing in tests?
- Capturing and verifying function calls
- Simulating the behavior of a function or method
- Mocking external dependencies
- Generating random test data
Stubbing in tests is often used to simulate the behavior of a function or method, allowing you to control its responses for testing purposes. It's especially useful when you want to isolate the code under test and ensure that certain functions return specific values or execute specific code paths.
How can you ensure the security of file uploads in a web application?
- Use an unauthenticated endpoint
- Limit file size
- Disallow specific file types
- Perform server-side validation
To ensure the security of file uploads in a web application, it's crucial to perform server-side validation. This includes checking the file's content type, verifying its size, and possibly scanning it for malware. Other important security measures include limiting file sizes to prevent denial-of-service attacks and disallowing specific file types to prevent the upload of potentially harmful files. Using an unauthenticated endpoint is not recommended as it could lead to security vulnerabilities.
In JavaScript, altering an object’s prototype at runtime can lead to ______ performance impacts.
- Positive
- Negative
- No
- Minimal
In JavaScript, altering an object's prototype at runtime can lead to "Negative" performance impacts. Modifying prototypes at runtime can cause performance bottlenecks, as it can affect the behavior of all objects that share the same prototype. It's generally recommended to avoid such runtime modifications for performance reasons.
What is the difference between the == and === operators in JavaScript?
- == compares values for equality, === compares values and types for equality
- == compares values and types for equality, === compares values for equality
- == is used for assignment, === is used for comparison
- == performs a deep comparison, === performs a shallow comparison
In JavaScript, == (loose equality) compares values for equality but performs type coercion if the operands have different types. === (strict equality) compares both values and types for equality without type coercion. Option 1 correctly describes this important distinction.
How does ESLint's --fix option handle issues that it can't automatically fix?
- ESLint raises an error and stops
- It logs the issues without attempting to fix them
- It automatically disables the rule causing the issue
- It leaves the issues as is
When ESLint's --fix option encounters issues it can't automatically fix, it will disable the rule causing the issue to prevent further errors. This allows ESLint to continue running and fix as many issues as possible without breaking. The other options do not accurately describe how --fix behaves.
How can you ensure that your project’s dependencies are secure and up-to-date?
- Ignore dependency updates to maintain stability.
- Regularly review and update dependencies using a tool like npm audit or yarn audit.
- Rely on the package maintainers to keep dependencies secure.
- Never update dependencies to avoid breaking changes.
To ensure that your project's dependencies are secure and up-to-date, you should regularly review and update them using tools like npm audit or yarn audit. These tools help identify and address security vulnerabilities. Ignoring updates may lead to security risks, and relying solely on package maintainers is not a recommended practice. Avoiding updates altogether is not a solution as it can lead to outdated and potentially insecure dependencies.