DEV Community

Cover image for Test splitting to speed up your CI/CD
Sibelius Seraphini for Woovi

Posted on

Test splitting to speed up your CI/CD

At Woovi, we care a lot about speed.
As a startup, we need a fast feedback loop to learn and ship faster.
We also care a lot about safety and quality, so we have more than 6 thousands automated tests to make sure we won't break anything.

Making our tests running faster in our CI/CD

We use CircleCI, as our CI/CD tool.
CircleCI enable us to split our tests in many containers, making our tests N containers times faster.

Paralell Jobs

We created a reusable command:

    description: 'test a specific package'
        type: string
      - run:
          name: 'Run tests << parameters.pkg >>'
          command: |
            TESTFILES=$(circleci tests glob "./packages/<< parameters.pkg >>/src/**/__tests__/*.spec.ts" | circleci tests split --split-by=timings)
            JEST_JUNIT_OUTPUT_DIR="./test-results" node --expose-gc ./node_modules/.bin/jest --logHeapUsage --passWithNoTests --maxWorkers=7 --coverage --forceExit --ci --silent ${TESTFILES}
Enter fullscreen mode Exit fullscreen mode

Let me explain what each part does

circleci tests glob "./packages/<< parameters.pkg >>/src/**/__tests__/*.spec.ts"
Enter fullscreen mode Exit fullscreen mode

Find all files that match this glob pattern, all test files inside tests directory that ends in .spec.ts

circleci tests split --split-by=timings
Enter fullscreen mode Exit fullscreen mode

Split tests based on their timings, so each container run the same amount of time.

node --expose-gc ./node_modules/.bin/jest --logHeapUsage --passWithNoTests --maxWorkers=7 --coverage --forceExit --ci --silent ${TESTFILES}
Enter fullscreen mode Exit fullscreen mode

--expose-gc tells the node to expose gc (garbage collector) to jest, so it can cleanup some unused memory
--lopHeapUsage shows how much memory each test is consuming
--passWithNoTests enables tests files without any test case
--maxWorkers=7 controls the concurrency of jest, because it can't really know the real number of cpu available to it
--coverage generates the code coverage report
--forceExit forces tests to finish if needed
--ci do not generate snapshot, breaks
--silent remove console.log to make tests run faster, I/O is expensive
${TESTFILES} list of files to be tested in that specific container

In Summary

Understand how your CI/CD tool works can enable you to make it fast, and provide a fast feedback loop for your team.

Does your CI/CD support test splitting?


Test Splitting Tutorial

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 Gavin Biesheuvel on Unsplash

Top comments (0)