Jest JUnit store_test_results

Using store_test_results with Jest (from CRA) and a JUnit reporter isn’t detected by CircleCI linux. What is wrong with my config?

config.yml

    version: 2
    jobs:
      build:
        working_directory: ~/grocery_tracker
        docker:
          - image: circleci/node:12
        steps:
          - checkout
          - restore_cache:
              key: npm-cache-v1-{{ checksum "package.json" }}
          - run:
              name: Install Dependencies
              command: npm install
          - save_cache:
              key: npm-cache-v1-{{ checksum "package.json" }}
              paths:
                - node_modules
          - run:
              name: Run Tests
              command: |
                mkdir -p ~/test-results/junit
                npm test --ci --reporters=jest-junit
              environment:
                JEST_JUNIT_OUTPUT: ~/test-results/junit/results.xml
          - store_test_results:
              path: ~/test-results

My Build works and the output from the tests shows 2 tests succeeding but doesn’t store them.

image

Running yarn test --ci --reporters=jest-junit on my windows machine produces a junit.xml file in my CWD.

Can you add a store_artifacts step to save the xml? That way you can inspect it to see if it’s malformed or otherwise strange.

https://circleci.com/docs/2.0/configuration-reference/#store_artifacts

Hi! I’ve added a store artifacts line in so my YML is now:

version: 2
jobs:
  build:
    working_directory: ~/grocery_tracker
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - restore_cache:
          key: npm-cache-v1-{{ checksum "package.json" }}
      - run:
          name: Install Dependencies
          command: npm install
      - save_cache:
          key: npm-cache-v1-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: Run Tests
          command: |
            mkdir -p ~/test-results/junit
            npm test --ci --reporters=jest-junit
          environment:
            JEST_JUNIT_OUTPUT: ~/test-results/junit/results.xml
      - store_artifacts:
          path: ~/test-results/junit
      - store_test_results:
          path: ~/test-results

I get a “no artifacts found” when path for store_artifacts is either ~/test-results or ~/test_results/junit. I guess my test doesn’t produce any files? My understanding is store_test_results just marks where CircleCI should look for them? Thanks

Yep. Is that where the results are being saved? I know jest-junit saves to junit.xml in the project root without config.

I’ve figured it out now. I had a few problems.

  • Didn’t create the “test-results/junit” directory in my workspace (where the test was run).
  • yarn test produced a junit.xml in the workspace, however npm run test doesn’t.
  • The environment of the jest-junit was moved into package.json and this fixed the issue of the output filepath.

Thanks for your quick replies.

Final YML:

version: 2
jobs:
  build:
    working_directory: ~/grocery_tracker
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - restore_cache:
          key: npm-cache-v1-{{ checksum "package.json" }}
      - run:
          name: Install Dependencies
          command: yarn install
      - save_cache:
          key: npm-cache-v1-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: Run Tests
          command: |
            mkdir -p ./test-results/junit
            yarn test --ci --reporters=jest-junit
      - store_artifacts:
          path: ./test-results/junit
      - store_test_results:
          path: ./test-results

package.json line:

"jest-junit":{
    "output": "./test-results/junit/results.xml"
  },
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.