Skip to content

Commit

Permalink
feat(ci): add GitHub Actions workflows for running unit tests
Browse files Browse the repository at this point in the history
The new workflows allow running unit tests in two modes:
1. `run-unit-tests.yml`: Runs all unit tests sequentially in a single job.
2. `run-unit-tests-parallel.yml`: Splits unit tests into chunks and runs them in parallel jobs for faster execution.

The parallel mode uses a matrix strategy to define the number of chunks. Each job in the matrix runs a subset of tests based on the total number of tests and the current chunk index.

A new script `run-unit-tests-range.sh` is added to support running a specific range of tests based on `START_INDEX` and `END_INDEX` environment variables.

The workflows upload test results as artifacts and add a summary of the results to the job summary for easier review.

This will help catch regressions early by running tests on every push to main and pull requests.
  • Loading branch information
MartinMinkov committed Jul 16, 2024
1 parent 54041b8 commit f79c491
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/run-unit-tests-parallel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Run unit tests parallel
on:
push:
branches:
- main
pull_request:
workflow_dispatch: {}

jobs:
Build-And-Test-Server:
timeout-minutes: 210
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
chunk: [1, 2, 3, 4]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '18'

- name: Build o1js and prepare for tests
id: prepare
run: |
git submodule update --init --recursive
npm ci
npm run build
echo "test_count=$(find ./dist/node -name "*.unit-test.js" | wc -l)" >> $GITHUB_OUTPUT
touch profiling.md
- name: Run unit tests
env:
TOTAL_TESTS: ${{ steps.prepare.outputs.test_count }}
CHUNK: ${{ matrix.chunk }}
CHUNKS: 4 # This should match the number of chunks in the matrix
run: |
start_index=$(( (TOTAL_TESTS * (CHUNK - 1) / CHUNKS) ))
end_index=$(( (TOTAL_TESTS * CHUNK / CHUNKS) ))
START_INDEX=$start_index END_INDEX=$end_index ./run-unit-tests.sh
continue-on-error: false

- name: Upload test results
if: always()
uses: actions/upload-artifact@v2
with:
name: test-results-${{ matrix.chunk }}
path: profiling.md

- name: Add to job summary
if: always()
run: |
echo "### Test Results for Chunk ${{ matrix.chunk }}" >> $GITHUB_STEP_SUMMARY
cat profiling.md >> $GITHUB_STEP_SUMMARY
32 changes: 32 additions & 0 deletions .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Run unit tests
on:
push:
branches:
- main
pull_request:
workflow_dispatch: {}

jobs:
Build-And-Test-Server:
timeout-minutes: 210
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Build o1js and execute tests
env:
TEST_TYPE: ${{ matrix.test_type }}
continue-on-error: false
run: |
git submodule update --init --recursive
npm ci
npm run build
npm run test:unit
touch profiling.md
cat profiling.md >> $GITHUB_STEP_SUMMARY
20 changes: 20 additions & 0 deletions run-unit-tests-range.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -e
shopt -s globstar # to expand '**' into nested directories

npm run build

# Get all test files
test_files=(./dist/node/**/*.unit-test.js)
total_tests=${#test_files[@]}

# If START_INDEX and END_INDEX are provided, use them
start=${START_INDEX:-0}
end=${END_INDEX:-$total_tests}

# Run the specified range of tests
for ((i=start; i<end; i++)); do
f="${test_files[$i]}"
echo "Running $f"
node --enable-source-maps --stack-trace-limit=1000 "$f"
done

0 comments on commit f79c491

Please sign in to comment.