DEV Community

Cover image for Effortless Project Discovery: Automating Jest Multi-Project Runs in Monorepos
Sibelius Seraphini for Woovi

Posted on

5

Effortless Project Discovery: Automating Jest Multi-Project Runs in Monorepos

At Woovi we have more than 6k automated tests.
We have 2 monorepos: one for the backend and another for frontend, just for historical reasons. We are going to merge them in the future.

Each package in a monorepo can have different test infrastructure requirements.
To make this possible we use Jest Multi-Project-Runner feature, which enables each package to define its own test configuration.
We can have a package that needs to use Node runtime, another that needs to run using jsdom, some packages that use elasticsearch.

Automating the discovery of projects in a monorepo

Before automating the discovery of projects for Jest, we had something like this:

module.exports = {
  projects: [
   '<rootDir>/packages/a/jest.config.js',
    '<rootDir>/packages/b/jest.config.js',
    '<rootDir>/packages/c/jest.config.js',
     ...
    '<rootDir>/packages/z/jest.config.js',
   ],
}
Enter fullscreen mode Exit fullscreen mode

Our codebase and package number are growing fast.
To make sure we are testing every package, we decided to automate the project discovered based on our monorepo pattern

const getWorkspaces = require('get-yarn-workspaces');

const cwd = process.cwd();
const pkg = require('./package.json');

const cleanWorkspaces = (workspace) => {
  const workspaces = pkg.workspaces || [];

  return workspaces.reduce((acc, w) => {
    const packagesPath = `${cwd}/${w.replace(/\/\*/g, '')}/`;

    return acc.replace(packagesPath, '');
  }, workspace);
};

const getProjects = () => {
  const workspaces = getWorkspaces();

  const pkgNames = workspaces.map((pkg) => cleanWorkspaces(pkg));

  return pkgNames.reduce(
    (acc, pkgName) => [...acc, `<rootDir>/packages/${pkgName}/jest.config.js`],
    [],
  );
};

module.exports = {
  projects: [...getProjects()],
}
Enter fullscreen mode Exit fullscreen mode

The code above will find all packages in our monorepo, and automatically add them to our jest multi-project-runner configuration.
When a new package is added to our monorepo, we ensure they are part of our test suite.

To Sum Up

When you have fewer packages or a small monorepo, this kind of automation or optimization does not make sense.
However, as you scale you need to add these to reduce the entropy to work in your codebase.

This simple optimization catch a few packages that had no tests at all, and we also make the template to create a new package to always contain a basic test setup.


Woovi
Woovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.

If you want to work with us, we are hiring!


Photo by Yuyang Liu on Unsplash

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay