Hone logo
Hone
Problems

Implementing Global Setup in Jest with TypeScript

Jest allows for global setup and teardown functions to be executed before and after all tests in a project or a specific test file. This is incredibly useful for tasks like initializing database connections, setting up environment variables, or configuring global mocks that are needed across multiple tests. This challenge will guide you in implementing a global setup function in a TypeScript Jest project.

Problem Description

You need to create a global setup file in your Jest project that will execute before any tests are run. This setup file should:

  1. Initialize a global variable: Create a variable named globalConfig and assign it a value of { apiEndpoint: 'https://example.com/api' }. This variable should be accessible from all test files.
  2. Log a message: Log a message to the console indicating that the global setup has been executed. The message should include the value of globalConfig.
  3. Handle potential errors: Wrap the initialization and logging in a try...catch block to gracefully handle any errors that might occur during setup. If an error occurs, log the error message to the console.

Key Requirements:

  • The setup file must be correctly configured in your jest.config.js or jest.config.ts file.
  • The globalConfig variable must be accessible from within your test files.
  • The console message should clearly indicate successful setup and display the globalConfig value.
  • Error handling should prevent setup failures from crashing the test suite.

Expected Behavior:

When tests are run, the global setup function should execute before any tests are run. The console should display the "Global setup executed" message with the globalConfig value. If an error occurs during setup, the console should display an error message instead.

Edge Cases to Consider:

  • What happens if the globalConfig assignment fails?
  • How do you ensure the setup function is only executed once per test run? (Jest handles this automatically, but it's good to be aware of).
  • How does this interact with other Jest configurations?

Examples

Example 1:

Input: Running Jest tests with a correctly configured global setup file.
Output: Console log: "Global setup executed: { apiEndpoint: 'https://example.com/api' }"
Explanation: The global setup function executes, initializes `globalConfig`, and logs the message.

Example 2:

Input: Running Jest tests with an error in the global setup file (e.g., trying to assign a non-object to `globalConfig`).
Output: Console log: "Error during global setup: TypeError: Cannot assign to read only property 'apiEndpoint' of object '#<Object>'"
Explanation: The `try...catch` block catches the error and logs the error message to the console. The tests still run, but the setup failed.

Constraints

  • The solution must be written in TypeScript.
  • The global setup file should be named src/setupTests.ts (or a similar convention that Jest recognizes).
  • The apiEndpoint value in globalConfig must be a string.
  • The setup function must not take any arguments.
  • The solution should be compatible with a standard Jest configuration.

Notes

  • You'll need to configure your jest.config.js or jest.config.ts file to recognize your setup file. The setupFilesAfterEnv option is typically used for this purpose.
  • Consider using console.log for debugging and verifying the setup process.
  • Remember that the global setup function is executed in a synchronous manner. Avoid performing asynchronous operations within the setup function unless you handle them correctly (e.g., using async/await and ensuring the setup completes before tests start).
Loading editor...
typescript