defaults: &defaults
  working_directory: ~/ministryofjustice/express-template-to-pdf
  docker:
    - image: circleci/node:latest-browsers

version: 2
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: Update npm
          command: 'sudo npm install -g npm@latest'
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: Install Dependencies
          command: npm ci
      - save_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
      - run:
          name: Linter check
          command: npm run lint
      - persist_to_workspace:
          root: .
          paths:
            - node_modules

  test:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: unit tests
          command: npm run test-report
          environment:
            MOCHA_FILE: ./reports/test-results.xml
      - store_test_results:
          path: ./reports

workflows:
  version: 2
  build_test_deploy:
    jobs:
      - build
      - test:
          requires:
            - build
