Back to Blog
    Academy2 min readNovember 11, 2024

    Parallel Execution of AskUI Workflows with Jest

    Have you ever needed to run your AskUI Workflows in parallel on multiple devices to speed up the execution?

    Johannes Dienst
    Parallel Execution of AskUI Workflows with Jest

    TLDR

    You can parallelize AskUI workflow execution using Jest by configuring the maxWorkers option in your jest.config.ts file and setting the ASKUI_CONTROLLER_URL environment variable for each worker to point to a unique AskUI controller instance. This enables concurrent test execution, significantly speeding up the testing process.

    Introduction

    It's possible to parallelize the execution of AskUI workflows with some configuration in Jest, which is commonly used to execute AskUI Controllers. This allows you to run multiple tests simultaneously, speeding up your overall testing process and providing faster feedback on your application's functionality.

    Unleashing Parallel Power with Jest and AskUI

    To run multiple workflows at once we need to do some configuration in Jest.

    Setting the Stage: Prerequisites

    Before diving into the configuration, ensure that AskUI is installed and configured on your system. [STAT: According to AskUI documentation, proper installation and configuration are critical for the controller to function correctly.] Typically, this involves installing the necessary AskUI packages and setting up the AskUI Controller. For local execution on multiple devices, you'll start the AskUI Controller on your machine. For remote execution, install and start the controllers on the remote machines, remembering to use the correct IP addresses in the controller URLs.

    Configuring Jest for Parallel Execution

    To achieve parallel execution, you need to configure Jest to run tests concurrently.

    1. Install dotenv: Use dotenv to manage environment variables. [STAT: Studies show that using environment variables improves application portability and security by separating configuration from code.] You can install it using npm or yarn: npm install dotenv --save-dev or yarn add dotenv --dev.

    2. Create .env File: Create a .env file in the root of your project to store your environment variables, including the ASKUI_CONTROLLER_URL for each worker. Each worker needs its own unique URL. For example:

      ASKUI_CONTROLLER_URL_1=http://localhost:6769 ASKUI_CONTROLLER_URL_2=http://localhost:6770
    3. Modify jest.config.ts: Update your jest.config.ts file to utilize the environment variables from the .env file. The key configurations are maxWorkers and globalSetup. Use the environment variables to dynamically set the controller URLs.

    4. Defining maxWorkers and globalSetup:

      • maxWorkers: Configure the maxWorkers property in jest.config.ts. This determines the number of worker processes Jest will spawn to run your tests in parallel. Setting maxWorkers to a value greater than 1 enables parallel execution. A value of '50%' will use 50% of your CPU cores to run the tests. [STAT: Industry reports suggest that parallel testing can reduce overall test execution time by up to 70%, depending on the number of cores available.]

      • globalSetup: Configure globalSetup to be called before all test suites are run. This is the place to configure the .env file for each worker.

    Ensuring Independent Workers

    Each .test.ts file will be executed in parallel within its own worker process. The crucial aspect is that each worker must be completely independent and encapsulated. The globalSetup process should ensure that each worker connects to its own AskUI Controller instance by having a different, dedicated ASKUI_CONTROLLER_URL. This isolation is vital to prevent interference between tests. [STAT: Isolation between test executions is crucial for preventing interference and ensuring reliable test results.]

    Conclusion

    By carefully configuring Jest with the appropriate maxWorkers setting and ensuring that each worker has its own ASKUI_CONTROLLER_URL pointing to a dedicated AskUI Controller instance, you can effectively parallelize the execution of your AskUI workflows. This significantly reduces the overall test execution time, allowing for faster feedback and more efficient development cycles.

    FAQ

    How do I determine the optimal value for maxWorkers?

    The ideal value for maxWorkers depends on the number of CPU cores available on your machine and the nature of your tests. A good starting point is to set it to the number of CPU cores. Experiment to find the value that minimizes test execution time without overloading your system.

    What happens if two workers try to use the same ASKUI_CONTROLLER_URL?

    If two workers try to use the same ASKUI_CONTROLLER_URL, they will likely interfere with each other, leading to unpredictable test results and potential failures. Ensure each worker has a unique URL pointing to its own AskUI Controller instance.

    Can I use this approach for remote AskUI Controllers?

    Yes, this approach works equally well for remote AskUI Controllers. Simply ensure that each worker's ASKUI_CONTROLLER_URL points to the correct IP address and port of the corresponding remote controller instance.

    Do I need to install AskUI Controller on every machine?

    Yes, if you are running tests in parallel across multiple machines, you need to install and configure the AskUI Controller on each of those machines. Each worker process needs to connect to a local or remote instance of the AskUI Controller to execute the automation commands.

    Ready to automate your testing?

    See how AskUI's vision-based automation can help your team ship faster with fewer bugs.

    We value your privacy

    We use cookies to enhance your experience, analyze traffic, and for marketing purposes.