Mastering Jest's afterAll Hook for Resource Cleanup
Jest's afterAll hook is crucial for performing cleanup operations that should run once after all tests in a describe block have completed. This is particularly useful for releasing resources like database connections, server instances, or file handles, ensuring your tests don't leave lingering processes or consume unnecessary resources. This challenge will guide you through implementing and utilizing this essential hook.
Problem Description
Your task is to demonstrate a practical implementation of Jest's afterAll hook. You will be working with a simulated asynchronous operation that requires setup before tests begin and cleanup after all tests within a specific scope have finished.
What needs to be achieved:
- Create a Jest test suite.
- Implement
beforeAllto simulate setting up a resource (e.g., starting a mock server, establishing a database connection). - Implement
afterAllto simulate cleaning up that resource (e.g., stopping the mock server, closing the database connection). - Write at least one test within the suite that uses the resource.
- Verify that the cleanup operation in
afterAllis indeed executed.
Key requirements:
- Use TypeScript for your Jest tests.
- Employ
beforeAllandafterAllhooks. - The setup and cleanup operations should ideally be asynchronous to mimic real-world scenarios.
- You should be able to observe or verify that
afterAllruns.
Expected behavior:
The output of your Jest run should indicate that all tests have passed, and logs (or console statements) should clearly show the sequence of execution: setup, test(s), and finally, cleanup.
Important edge cases to consider:
- What happens if an error occurs during the setup in
beforeAll? - What happens if an error occurs within one of the tests?
Examples
For this challenge, we won't provide explicit input/output in the traditional sense. Instead, the "output" will be the successful execution of your Jest tests and visible confirmation in your console that the afterAll hook has run.
Conceptual Example:
Imagine you are testing a service that requires a database connection.
beforeAll: Establishes the database connection.- Tests: Perform various operations using the database connection.
afterAll: Closes the database connection.
The successful completion of all tests and the fact that the connection is closed at the end signifies the correct implementation.
Constraints
- Your solution should be written entirely in TypeScript.
- You must use Jest as your testing framework.
- The
beforeAllandafterAlloperations should be asynchronous (return Promises). - You should not rely on any external libraries for the core
beforeAll/afterAllfunctionality, only Jest itself.
Notes
- Consider using
console.logstatements within yourbeforeAllandafterAllhooks, as well as within your tests, to visually confirm the order of execution. - Think about how you can mock or simulate asynchronous operations effectively for the setup and cleanup.
- While this challenge focuses on
afterAll, understanding its relationship withbeforeAlland individual test execution is key.