You are developing a suite of unit tests for a service that interacts with an external API. How would you employ mocking and stubbing to ensure the tests are isolated and reliable?
- Mock the external API calls to simulate responses
- Stub all internal functions used in the service
- Avoid mocking and use real external API calls
- Mock only the database interactions
In unit testing, it's crucial to isolate the code being tested. To ensure tests are isolated and reliable when dealing with an external API, you should mock the external API calls. This allows you to control the responses and not rely on external services during tests. Stubbing internal functions is not the primary approach for isolating external API calls. Avoiding mocking by using real API calls can lead to unreliable and slow tests. Mocking the database, in this case, is unnecessary.
You are configuring a Node.js project and want to ensure that a certain command runs before your tests run each time. How would you configure this using the package.json file?
- Use the "pretest" script in the "scripts" section of the package.json file to specify the command that should run before tests.
- Include the command in the "description" field of the package.json file.
- Create a separate pretest.json configuration file.
- Use a comment in the test file to specify the pretest command.
To ensure a certain command runs before tests each time, you should use the "pretest" script in the "scripts" section of the package.json file. This script will be executed automatically before running tests when the npm test command is invoked. The other options are not standard ways to configure pretest actions.
You are tasked with developing a logging system for an Express.js application to log every incoming request. How would you implement middleware to log the details of every request made to the application?
- Create a middleware function that logs request details and use app.use to apply it globally to all routes.
- Include logging code within each route handler to log request details individually.
- Use a third-party logging library like Morgan and configure it within your Express.js application.
- Use JavaScript's built-in console.log within each route to log request details.
To log details of every incoming request in an Express.js application efficiently, you should create a dedicated middleware function that logs request details and use app.use to apply it globally to all routes. This centralizes logging and avoids duplicating code in each route handler. The other options are either less efficient or not recommended for production applications.
How can you install ESLint in your Node.js project?
- Using npm or yarn: npm install eslint --save-dev or yarn add eslint --dev
- Downloading it from the official website
- Installing it globally using npm install -g eslint
- Adding it directly to your HTML file
You can install ESLint in your Node.js project using npm or yarn by running the command npm install eslint --save-dev or yarn add eslint --dev. This ensures that ESLint is added as a development dependency for your project.
Which of the following is a suitable method to prevent variable leakage in the global scope?
- Using the var keyword to declare variables.
- Using immediately-invoked function expressions (IIFE).
- Using global variables exclusively.
- Using let and const keywords to declare variables.
Using immediately-invoked function expressions (IIFE) is a common technique to prevent variable leakage into the global scope. It encapsulates variables within a function scope, avoiding global pollution. The other options do not address this concern effectively.
When using the map method on an array in JavaScript, the original array ______ be mutated.
- may
- will
- must
- can't
When using the map method on an array in JavaScript, the original array can't be mutated. The map method creates a new array with the results of calling a provided function on every element in the original array without changing the original array itself.
What happens when you try to access an index in a buffer that does not exist in Node.js?
- It returns null
- It throws an "IndexOutOfRange" error
- It returns an "undefined" value
- It automatically resizes the buffer
When you try to access an index in a buffer that does not exist, Node.js throws an "IndexOutOfRange" error. Buffers do not automatically resize.
What happens when an error is thrown inside an async function and it is not caught within the function?
- The error will be caught by the global error handler and potentially crash the application.
- The error is automatically logged to the console, but the function continues executing.
- The error is ignored, and the function continues executing.
- The error is caught by the JavaScript runtime and handled silently.
If an error is thrown inside an async function and is not caught within the function, it will propagate to the global error handler, which can potentially crash the application. Proper error handling is crucial in async code.
How can you define optional route parameters in Express.js?
- Enclose parameters in square brackets [param]
- Use the optional keyword before the parameter
- Use the ? symbol after the parameter
- Wrap parameters in parentheses (param)
In Express.js, you can define optional route parameters by using the ? symbol after the parameter name, like /route/:param?. This makes the parameter optional, allowing it to match both routes with and without that parameter. The other options do not represent the correct way to define optional route parameters in Express.js.
In a system following eventual consistency, what implications does it have on Read operations after a Write operation?
- Strong consistency
- Read operations may return stale data
- Write operations are blocked until consistency is achieved
- Write operations are delayed
In a system with eventual consistency, Read operations may return stale or outdated data for a period of time after a Write operation. This is because eventual consistency prioritizes availability and performance over strict consistency. Strong consistency (Option 1) ensures that all reads return the most recent write but may lead to higher latency. Options 3 and 4 are not characteristic of eventual consistency.
________ is a security practice that involves encoding information so that only authorized parties can access it.
- Encryption
- Hashing
- Salting
- Obfuscation
Encryption is a security practice that involves encoding information in a way that only authorized parties with the decryption key can access it. This is commonly used to protect sensitive data during transmission and storage.
In a complex CORS scenario, how can you selectively allow certain types of requests while denying others?
- Create custom middleware
- Set Access-Control-Allow-Origin to *
- Use the OPTIONS method
- Configure server-side routing
In complex CORS scenarios, you can selectively allow or deny requests by creating custom middleware on your server. This middleware can inspect the request headers, methods, or other criteria to determine whether to allow or deny a request. The other options are components of CORS but do not provide fine-grained control over request types.