How can you simulate user actions like clicks or keyboard inputs in Jest?
- jest.spyOn()
- jest.mock()
- jest.fn()
- jest.simulate()
In Jest, you can simulate user actions like clicks or keyboard inputs using jest.fn(). This allows you to create mock functions that can simulate user interactions and track their calls. The other options have different purposes; jest.spyOn() is used to spy on method calls, jest.mock() is used to mock modules, and jest.simulate() is not a valid Jest method for simulating user actions.
Cache ______ is a situation where multiple requests are made to a resource that is expensive to produce, causing a surge in load.
- Throttling
- Bursting
- Collapsing
- Overloading
Cache bursting is a situation where multiple requests are made to a resource that is expensive to produce, causing a surge in load. It typically occurs when cached data expires or when the cache is invalidated.
In which scenario would denormalization be considered a suitable option for query optimization?
- When you want to minimize data redundancy and improve data integrity.
- When you want to improve query performance, even at the cost of some data redundancy and update anomalies.
- Denormalization is never considered a suitable option for query optimization.
- When you want to reduce the size of the database.
Denormalization is considered when you want to optimize read-heavy queries and reduce the number of JOIN operations. It involves introducing redundancy to improve query performance, but it can lead to data anomalies.
You are tasked with optimizing a Node.js application suffering from frequent delays and unresponsive behavior. How would you diagnose and address potential issues related to the Event Loop and blocking operations?
- Increase the size of the Event Loop thread pool.
- Use profiling tools like Node.js built-in profiler or third-party tools like Clinic.js.
- Replace Node.js with a different runtime environment.
- Use synchronous file I/O operations for better performance.
To diagnose and address issues related to the Event Loop and blocking operations in Node.js, you should use profiling tools like Node.js built-in profiler or third-party tools like Clinic.js. These tools help you identify bottlenecks and performance issues in your code. Options a, c, and d are not recommended solutions and may lead to further issues.
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.
How can the 'done' callback be used in asynchronous testing with Mocha?
- done is used to terminate the Mocha test suite prematurely.
- done is used to indicate that the test is asynchronous and should wait for it to complete.
- done is used to skip a test case in Mocha.
- done is not used in Mocha for asynchronous testing.
In Mocha, the done callback is used to indicate that a test is asynchronous and should wait for it to complete before considering the test case finished. It prevents the test case from finishing prematurely. The other options provide incorrect information about the use of done.
How can you create an object in JavaScript that does not inherit the prototype from Object?
- Object.create(null)
- Object() constructor
- {} constructor
- Object.assign({})
In JavaScript, you can create an object that does not inherit the prototype from Object by using Object.create(null). This creates an object with no prototype, making it a clean slate. The other options either inherit from Object or use Object's prototype.
In Express, how can you enable Cross-Origin Resource Sharing (CORS) for your API?
- app.use(cors())
- app.enable(cors)
- app.allow(CORS)
- app.cors(true)
To enable Cross-Origin Resource Sharing (CORS) in Express, you should use the cors middleware by adding app.use(cors()) to your application. This middleware allows or restricts cross-origin HTTP requests. The other options are not valid ways to enable CORS in Express.
What is the primary purpose of using JSON Web Tokens (JWT) in authentication?
- Storing user passwords securely
- Encoding user data in URL parameters
- Storing user sessions on the server
- Securely transmitting information between parties
The primary purpose of using JSON Web Tokens (JWT) in authentication is to securely transmit information between parties. JWTs are used to authenticate users and ensure that data exchanged between the client and server remains tamper-proof and confidential. They are commonly used in web applications to maintain user sessions without the need to store session data on the server. The other options do not represent the primary purpose of JWTs.