Skip to content

Commit

Permalink
chore: shard unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Mar 23, 2024
1 parent 0168c81 commit 47a9d0b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
43 changes: 39 additions & 4 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
module-chunks: ${{ steps.set-module-chunks.outputs.chunks }}
api-matrix: ${{ steps.set-api-matrix.outputs.matrix }}
api-chunks: ${{ steps.set-api-chunks.outputs.chunks }}
unit-tests-matrix: ${{ steps.set-unit-tests-matrix.outputs.matrix }}
unit-tests-chunks: ${{ steps.set-unit-tests-chunks.outputs.chunks }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -36,7 +38,7 @@ jobs:
- id: set-module-chunks
name: Set Module Chunks
working-directory: integration-tests/modules
run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 3 | ceil)]')" >> $GITHUB_OUTPUT
run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 2 | ceil)]')" >> $GITHUB_OUTPUT

- id: set-module-matrix
name: Set Module Matrix
Expand All @@ -47,17 +49,32 @@ jobs:
- id: set-api-chunks
name: Set API Chunks
working-directory: integration-tests/api
run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 3 | ceil)]')" >> $GITHUB_OUTPUT
run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 2 | ceil)]')" >> $GITHUB_OUTPUT

- id: set-api-matrix
name: Set API Matrix
run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT
env:
CHUNKS: ${{ steps.set-api-chunks.outputs.chunks }}

unit-tests:
- id: set-unit-tests-chunks
name: Set Unit Tests Chunks
run: echo "chunks=$(yarn workspaces list --json | jq -j '[inputs | .name]' | jq -r | jq -cM '[_nwise(length / 2 | ceil)]')" >> $GITHUB_OUTPUT

- id: set-unit-tests-matrix
name: Set Unit Tests Matrix
run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT
env:
CHUNKS: ${{ steps.set-unit-tests-chunks.outputs.chunks }}

unit-tests-matrix:
needs: setup
runs-on: ubuntu-latest
name: Shard (${{ matrix.chunk }}) Unit Tests
strategy:
fail-fast: false
matrix:
chunk: ${{ fromJSON(needs.setup.outputs.unit-tests-matrix) }}
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
Expand Down Expand Up @@ -90,7 +107,10 @@ jobs:
run: yarn build

- name: Run unit tests
run: yarn test
run: yarn test:chunk
env:
CHUNK: ${{ matrix.chunk }}
CHUNKS: ${{ needs.setup.outputs.unit-tests-chunks }}

integration-tests-packages:
needs: setup
Expand Down Expand Up @@ -237,6 +257,21 @@ jobs:
- run: exit 0
if: ${{ contains(needs.integration-tests-api-matrix.result, 'success') }}

unit-tests:
if: ${{ always() }}
runs-on: ubuntu-latest
needs: unit-tests-matrix
steps:
- run: exit 1
if: >-
${{
contains(needs.unit-tests-matrix.result, 'failure')
|| contains(needs.unit-tests-matrix.result, 'cancelled')
|| contains(needs.unit-tests-matrix.result, 'skipped')
}}
- run: exit 0
if: ${{ contains(needs.unit-tests-matrix.result, 'success') }}

integration-tests-plugins:
needs: setup
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"prettier": "prettier",
"jest": "jest",
"test": "turbo run test --concurrency=50% --no-daemon",
"test:chunk": "./scripts/run-workspace-unit-tests-in-chunks.sh",
"test:integration:packages": "turbo run test:integration --concurrency=50% --no-daemon --filter='./packages/*'",
"test:integration:api": "turbo run test:integration:chunk --concurrency=50% --no-daemon --filter=integration-tests-api",
"test:integration:plugins": "turbo run test:integration --concurrency=50% --no-daemon --filter=integration-tests-plugins",
Expand Down
18 changes: 18 additions & 0 deletions scripts/run-workspace-unit-tests-in-chunks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Get array of workspaces
# convert NDJSON stream to an array of arrays, divided by chunk size
# get the workspaces of the current CHUNK environment
workspaces=$(echo $CHUNKS | jq -r ".[$CHUNK]")
echo "$workspaces"
# Initialize an empty string for the filters
filters=""

# Loop through each workspace in the array
for workspace in $(echo "$workspaces" | jq -r '.[]'); do
# Add the workspace name to the filters array as an argument
filters+=" --filter=${workspace}"
done
echo "$filters"
# Run the test in the selected chunk
yarn run test $filters

0 comments on commit 47a9d0b

Please sign in to comment.