DEV Community

Molly Crendraven
Molly Crendraven

Posted on

Test splitting with Jest on CircleCI

One of our customers asked on Twitter, "How do I setup test splitting on CircleCI with Jest?"

This is briefly explain in the CircleCI docs, but I can see where it could be confusing.

I'm not the best writer, so I will let my code do the talking. This should serve as a good exercise in code documentation as well.

A portion of my package.json

  "scripts": {
    "test": "jest",
  },
  "jest": {
    "reporters": [
      "default",
      "jest-junit"
    ]
  },
  "jest-junit": {
    "outputDirectory": "test_results/junit"
  }
Enter fullscreen mode Exit fullscreen mode

My .circleci/config.json file

version: 2
jobs:
  build:
    parallelism: 3
    docker:
      # I want to make sure I know exactly what version I'm using
      - image: circleci/node:10.15.3

    working_directory: ~/repo

    steps:
      - checkout

      # Npm CI uses the package-lock.json file to ensure exact versions
      # This prevents changes and compromised upstream dependencies
      # from changing your project. It wipes the node_modules directory
      # so there is no need for a dependencies cache here.
      - run: npm ci

      # First we save the split test file names into an env
      # Then we pass that to jest
      - run:
          name: Run Jest tests using test splitting
          command: |
            TESTFILES=$(circleci tests glob "test/**/*.test.js" | circleci tests split --split-by=timings)
            npm test $TESTFILES

      - store_test_results:
          path: test_results

      - store_artifacts:
          path: test_results
          destination: test_results
Enter fullscreen mode Exit fullscreen mode

Hope this helps!

Top comments (0)