version: 2.1

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: node:16.5.0-buster-slim
  resource_class: small

jobs:
  npm-install:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: npm install
          command: |
            npm install
            npm install --no-save
      - persist_to_workspace:
          root: .
          paths:
            - .
  format:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: install prettier
          command: npm i prettier
      - run:
          name: format
          command: npx prettier -l './*.{mjs,js,ts,md,json,html}' './{src,docs,tests}/{**/,}*.{mjs,js,ts,md,json,html}'

  test-types:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Test types
          command: npm run test:types

  test-unit:
    docker:
      - image: cypress/browsers:node14.17.0-chrome91-ff89
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Build bundle
          command: npm run build
      - run:
          name: Run tests
          command: |
            export INSIDE_DOCKER=1
            npx karma start
      - store_test_results:
          path: test-results

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - npm-install
      - test-types:
          requires:
            - npm-install
      - test-unit:
          requires:
            - npm-install
