Refactor GitHub Actions workflow for improved testing and caching #226
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test | |
on: [push, pull_request] | |
jobs: | |
Setup: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: [18.x] | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Node.js ${{ matrix.node-version }} | |
uses: actions/[email protected] | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Node.js modules cache | |
uses: actions/cache@v4 | |
id: modules-cache | |
with: | |
path: | | |
~/.cache/Cypress | |
${{ github.workspace }}/node_modules | |
${{ github.workspace }}/backend/node_modules | |
${{ github.workspace }}/frontend/node_modules | |
key: ${{ runner.os }}-${{ matrix.node-version }}-modules-${{ hashFiles('**/package-lock.json') }} | |
- name: Install dependencies | |
if: steps.modules-cache.outputs.cache-hit != 'true' | |
run: npm install | |
- name: Check for uncomitted changes | |
run: git diff --exit-code | |
Lint: | |
needs: Setup | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Restore cache | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.cache/Cypress | |
${{ github.workspace }}/node_modules | |
${{ github.workspace }}/backend/node_modules | |
${{ github.workspace }}/frontend/node_modules | |
key: ${{ runner.os }}-18.x-modules-${{ hashFiles('**/package-lock.json') }} | |
- name: Run linting | |
run: npm run lint | |
Unit-Tests: | |
needs: Setup | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Restore cache | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.cache/Cypress | |
${{ github.workspace }}/node_modules | |
${{ github.workspace }}/backend/node_modules | |
${{ github.workspace }}/frontend/node_modules | |
key: ${{ runner.os }}-18.x-modules-${{ hashFiles('**/package-lock.json') }} | |
- name: Run unit tests | |
run: npm run test:unit | |
Get-Test-Groups: | |
runs-on: ubuntu-latest | |
outputs: | |
test-groups: ${{ steps.set-groups.outputs.groups }} | |
steps: | |
- uses: actions/checkout@v4 | |
- id: set-groups | |
shell: bash | |
run: | | |
set -x # Enable debug mode to see each command | |
echo "Checking directory structure..." | |
ls -la frontend/src/__tests__/cypress/cypress/tests/ || echo "Base test directory not found" | |
echo "Looking for mocked tests directory..." | |
if [ -d "frontend/src/__tests__/cypress/cypress/tests/mocked" ]; then | |
echo "Found mocked tests directory. Contents:" | |
ls -la frontend/src/__tests__/cypress/cypress/tests/mocked/ | |
echo "Generating test groups..." | |
echo "Step 1: Changing directory and running find..." | |
cd frontend/src/__tests__/cypress/cypress/tests/mocked || exit 1 | |
echo "Step 2: Running find command..." | |
find . -maxdepth 1 -mindepth 1 -type d | |
echo "Step 3: Processing with sed..." | |
find . -maxdepth 1 -mindepth 1 -type d | sed 's|^./||' | |
echo "Step 4: Creating JSON array..." | |
GROUPS=$(find . -maxdepth 1 -mindepth 1 -type d | \ | |
sed 's|^./||' | \ | |
jq -R . | \ | |
jq -s .) | |
echo "Step 5: Generated groups:" | |
echo "$GROUPS" | |
if [ -z "$GROUPS" ] || [ "$GROUPS" == "[]" ]; then | |
echo "Warning: No test groups found, falling back to default" | |
GROUPS='["default"]' | |
fi | |
echo "Step 6: Writing to output..." | |
echo "groups=$GROUPS" >> $GITHUB_OUTPUT | |
else | |
echo "Warning: Mocked tests directory not found at expected location:" | |
echo "frontend/src/__tests__/cypress/cypress/tests/mocked" | |
echo "Falling back to default group" | |
echo "groups=['default']" >> $GITHUB_OUTPUT | |
fi | |
echo "Final output:" | |
cat $GITHUB_OUTPUT | |
Cypress-Tests: | |
needs: [Setup, Get-Test-Groups] | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
test-group: ${{ fromJson(needs.Get-Test-Groups.outputs.test-groups) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Restore cache | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.cache/Cypress | |
${{ github.workspace }}/node_modules | |
${{ github.workspace }}/backend/node_modules | |
${{ github.workspace }}/frontend/node_modules | |
key: ${{ runner.os }}-18.x-modules-${{ hashFiles('**/package-lock.json') }} | |
- name: Run Cypress tests | |
run: | | |
if [ "${{ matrix.test-group }}" == "default" ]; then | |
npm run test:cypress | |
else | |
npm run test:cypress -- --spec "frontend/src/__tests__/cypress/cypress/tests/mocked/${{ matrix.test-group }}/**/*" | |
fi | |
- name: Upload coverage reports to Codecov | |
if: matrix.test-group == 'pipelines' | |
uses: codecov/[email protected] | |
with: | |
token: ${{ secrets.CODECOV_TOKEN }} | |
fail_ci_if_error: false | |
name: frontend | |
directory: ./frontend/coverage | |
verbose: true | |
- uses: actions/upload-artifact@v4 | |
if: always() | |
with: | |
name: cypress-results-${{ matrix.test-group }} | |
path: ./frontend/src/__tests__/cypress/results/ |