feat: add code testing #4
Workflow file for this run
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: Code Snippets testing | |
on: | |
pull_request: | |
branches: [main] | |
paths: | |
- "code/**/*.test.ts" # Only trigger on test file changes | |
schedule: | |
- cron: "0 0 * * *" # Run daily at midnight UTC | |
jobs: | |
snippets-tests: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ./code # Set working directory to code folder | |
strategy: | |
matrix: | |
node-version: [20] | |
fail-fast: false | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Get changed test files | |
id: changed-files | |
uses: tj-actions/changed-files@v39 | |
with: | |
files: | | |
code/**/*.test.ts | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ matrix.node-version }} | |
cache: "pnpm" | |
- name: Install pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
run_install: false | |
- name: Install dependencies | |
run: pnpm install | |
- name: Run tests on changed files | |
if: github.event_name == 'pull_request' | |
run: | | |
CHANGED_FILES="${{ steps.changed-files.outputs.all_changed_files }}" | |
if [ -z "$CHANGED_FILES" ]; then | |
echo "No test files were changed" | |
exit 0 | |
fi | |
# Create array for test files | |
declare -a test_files=() | |
# Collect only .test.ts files | |
for file in $CHANGED_FILES; do | |
if [[ $file == *.test.ts ]]; then | |
test_files+=("$file") | |
echo "Added test file: $file" | |
fi | |
done | |
# If we found test files, run them together | |
if [ ${#test_files[@]} -gt 0 ]; then | |
echo "Running tests for the following files:" | |
printf '%s\n' "${test_files[@]}" | |
# Run all test files in a single command | |
node --import tsx --test "${test_files[@]}" || exit 1 | |
else | |
echo "No test files to run" | |
fi | |
- name: Run all tests | |
if: github.event_name == 'schedule' | |
run: | | |
echo "Running all tests in code directory" | |
pnpm turbo test |