# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2.1

orbs:
  node: circleci/node@3.0.0

jobs:
  build:
    docker:
      - image: cimg/node:16.11.0
      - image: mongo:4.4

    working_directory: ~/homebrewery
    executor: node/default

    steps:
      - checkout:
          path: ~/homebrewery

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: sudo npm install -g npm@8.10.0
      - node/install-packages:
          app-dir: ~/homebrewery
          cache-path: node_modules
          override-ci-command: npm i

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      - persist_to_workspace:
          root: .
          paths:
            - .

  test:
    docker:
      - image: cimg/node:16.11.0

    working_directory: ~/homebrewery
    parallelism: 1

    steps:
      - attach_workspace:
          at: .

      # run tests!
      - run:
          name: Test - API Unit Tests
          command: npm run test:api-unit
      - run:
          name: Test - Basic
          command: npm run test:basic
      - run:
          name: Test - Mustache Spans
          command: npm run test:mustache-syntax
      - run:
          name: Test - Routes
          command: npm run test:route
      - run:
          name: Test - Coverage
          command: npm run test:coverage

workflows:
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build
