Skip to main content

Change based testing in a Yarn-managed monorepo

Change-Based Testing in a Yarn-managed monorepo

Change-Based Testing in a Yarn-Based Monorepo

In the world of software development, especially when working with large projects, efficiency is key. One approach to manage large codebases effectively is by using a monorepo, where multiple projects are stored in a single repository. Yarn, a popular package manager for JavaScript, provides powerful tools to manage such monorepos. One of the standout features is the ability to run commands across multiple workspaces with precision and efficiency. In this blog post, we'll explore how to leverage Yarn's capabilities for change-based testing, focusing on a specific command: yarn workspaces foreach --since --recursive --topological --exclude root -vv exec eslint src.

Understanding the Command

Before diving into the details, let's break down what this command does:

  • yarn workspaces foreach: This is the base command to run a specified operation across all workspaces defined in your monorepo.
  • --since: Runs the command only in workspaces that have changes since the last commit (or a specified point in history).
  • --recursive: Applies the command recursively to all dependent workspaces.
  • --topological: Ensures the command runs in topological order, meaning dependencies are processed before their dependents.
  • --exclude root: Excludes the root workspace from the operation.
  • -vv: Sets verbosity to very verbose, providing detailed logging.
  • exec eslint src: Executes the ESLint command on the src directory within each applicable workspace.

The Need for Change-Based Testing

In large monorepos, running tests or linters across all projects every time can be time-consuming and inefficient. Change-based testing addresses this by focusing only on the parts of the codebase that have changed. This not only saves time but also speeds up the feedback loop, allowing developers to catch and fix issues faster.

Benefits of Change-Based Testing with Yarn

  • Efficiency: By running tests and linters only on changed workspaces, you reduce the amount of computational resources required.
  • Speed: Faster test runs mean quicker feedback, which is crucial for maintaining high development velocity.
  • Focus: Developers can concentrate on relevant issues rather than being overwhelmed by unrelated test failures.

Implementing Change-Based Testing

Let's walk through how you can implement change-based testing in a Yarn-based monorepo using the command we've discussed.

1. Setting Up Your Monorepo

Ensure your monorepo is correctly set up with Yarn workspaces. Your package.json should include the following configuration:


{
  "private": true,
  "workspaces": ["packages/*"]
}

2. Adding ESLint

Make sure each workspace has ESLint installed and configured. You can add ESLint to a workspace by running:


yarn workspace <workspace-name> add eslint

3. Running the Command

Use the following command to lint only the changed workspaces:


yarn workspaces foreach --since --recursive --topological --exclude root -vv exec eslint src

How It Works

  • Detect Changes: The --since flag ensures that only workspaces with changes since the last commit are considered.
  • Respect Dependencies: The --recursive and --topological flags ensure that any dependencies are processed first, maintaining the integrity of your workspace relationships.
  • Exclude Unnecessary Workspaces: The --exclude root flag skips the root workspace, which typically doesn’t need linting.
  • Detailed Logging: The -vv flag provides verbose output, which helps in debugging and understanding the command execution flow.

Real-World Example

Consider a monorepo with the following structure:


/monorepo
  /packages
    /package-a
      /src
      /tests
    /package-b
      /src
      /tests

If you’ve made changes only to package-a, running the command will lint only the src directory within package-a, ignoring package-b entirely unless it has dependencies affected by package-a.

Conclusion

Change-based testing in a Yarn-based monorepo is a powerful strategy to maintain code quality efficiently. By leveraging the yarn workspaces foreach command with appropriate flags, you can ensure that only relevant parts of your codebase are tested, saving time and resources. This approach not only enhances productivity but also aligns with modern development practices, promoting faster and more focused feedback loops.

Embrace change-based testing in your Yarn monorepo today, and experience the benefits of a more efficient and streamlined development process.

Comments

Popular posts from this blog

Are Code Challenges Useful?

Are Code Challenges Useful? Are Code Challenges Useful to Learn? In the rapidly evolving world of technology, staying relevant and sharpening your programming skills are crucial. One method that has gained popularity among both beginners and seasoned developers is the use of code challenges. But are these challenges genuinely useful for learning, or are they just a trendy distraction? Let's delve into the benefits and potential drawbacks of code challenges to understand their role in the learning process. The Benefits of Code Challenges 1. Reinforcement of Concepts Code challenges are excellent for reinforcing theoretical knowledge. They require you to apply concepts in practical scenarios, helping to cement your understanding. When you solve a problem using loops, data structures, or algorithms, the repetition and application in different contexts make these concepts more intuitive and memorable. 2. Problem-Solving...

Essential Linux Monitoring Tools in 2024

Essential Linux Monitoring Tools in 2024 Essential Linux Monitoring Tools in 2024 In the ever-evolving landscape of IT infrastructure, the role of a system administrator is critical to maintaining the health, performance, and security of Linux servers. With the multitude of monitoring tools available, it's essential to select the right ones to meet your specific needs. This blog explores the essential Linux monitoring tools every sysadmin should consider, categorized into command line, network, system, log, and infrastructure monitoring tools. Command Line Monitoring Tools 1. top/htop top and htop are quintessential for real-time monitoring of system processes. htop offers a more user-friendly and interactive interface compared to top . Real-time process monitoring. CPU, memory, and swap usage. Process management capabilities (kill, renice). 2. iotop iotop is a handy tool for monito...

Must-see presentation for end to end testing using protractor for angularJS apps

In Wellington AngularJS meetup , I gave a talk on using protractor for end to end testing of angularJS applications . Here I presented and demonstrated some best practices with a live demo on how to get started with using protractor for e2e testing. My presentation and demonstration application are both available online. Links are as below: My presentation My demo angular app for demonstrating some best practices like page objects, reporting, login etc.