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

Scrum: What is Agile?

If you ask 10 people to define Agile development and you will receive 11 definitions emphasizing different aspects of the process. I got to hear a talk (Presented by Anthony Boobier (Nomad8)) to clear up the confusion of this buzzwords and shed light onto the fundamentals of Agile software development. We looked into what problems Agile is trying to solve and dive deep into the reasons why it works. We undertook a brief journey into the history of software development, explore the principles and values behind Agile and then later focused on how to manage our work in an Agile fashion using its most prevalent flavour, Scrum. Scrum is a development framework in which cross-functional teams develop products or projects in an iterative, incremental manner. It structures development in cycles of work called Sprints. These iterations are no more than four weeks each (the most common is two weeks), and take place one after the other without pause. The Sprints are timeboxed – they en...

UI Testing in Xcode

Xcode 7 has introduced new UI testing features fully integrated into the IDE. I learned about the new APIs and how UI testing fits in with existing testing features in Xcode. Also got to see how to get started by recording your app, and how to efficiently craft and maintain UI tests. check this link introducing this API and UI testing tool in WWDC/2015 Though this tool is in beta stage, IT industry has taken this as a tool for future UI testing for apple apps and have started using it already. I came across another good article related to this. It summarizes the WWDC 2015 UI Testing talk and gives the detail of coming beta releases as well. Check it out here

A re-introduction to JavaScript (JS tutorial)

Why a re-introduction? Because JavaScript is notorious for being the world's most misunderstood programming language. It is used by an incredible number of high-profile applications, showing that deeper knowledge of this technology is an important skill for any web or mobile developer. Rekindle your memory with awesomeness of javascript. Full article available on Mozilla developer Network (MDN)