version: 2
# Define common steps all parts of the test workflow use
references:
  cache-options: &cache-options
    key: package-cache-{{ .Branch }}-{{ .Revision }}

  shared-test-steps: &shared-test-steps
    steps:
      - checkout
      - restore_cache:
          <<: *cache-options
      - run:
          name: rebuild packages for this node version
          command: npm rebuild
      - run:
          name: display-node-version
          command: node --version
      - run:
          name: display-npm-version
          command: npm --version
      - run:
          name: test
          command: npm test
          when: always

# Test the common workflow on multiple versions of node
jobs:
  build:
    docker:
      - image: circleci/node:14
    steps:
      - checkout
      - restore_cache:
          <<: *cache-options
      - run:
          name: npm-install
          command: npm install
      - save_cache:
          <<: *cache-options
          paths:
            - ./node_modules

  test-node-v16:
    docker:
      - image: circleci/node:16
      - image: redis:4
      - image: selenium/standalone-chrome:latest
    <<: *shared-test-steps

  test-node-v14:
    docker:
      - image: circleci/node:14
      - image: redis:4
      - image: selenium/standalone-chrome:latest
    <<: *shared-test-steps

# Run all the tests is parallel
workflows:
  version: 2
  test-all-node-versions:
    jobs:
      - build

      - test-node-v16:
          requires:
            - build

      - test-node-v14:
          requires:
            - build
