ci: add lock file sync validation to prevent confusing npm install failures #3666
Workflow file for this run
This file contains hidden or 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: Run Jest Tests on PR | |
| on: | |
| pull_request_target: | |
| types: | |
| - opened | |
| - synchronize | |
| jobs: | |
| test: | |
| name: Run Jest Tests | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| contents: read | |
| issues: write | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: Validate lock file sync | |
| run: | | |
| echo "Validating package-lock.json is in sync with package.json..." | |
| if ! npm ci --dry-run > /dev/null 2>&1; then | |
| echo "" | |
| echo "ERROR: package-lock.json is out of sync with package.json" | |
| echo "" | |
| echo "This usually means package.json was modified but package-lock.json was not updated." | |
| echo "" | |
| echo "To fix this, run the following commands locally and commit the changes:" | |
| echo " 1. npm install" | |
| echo " 2. git add package-lock.json" | |
| echo " 3. git commit -m 'chore: update lock file'" | |
| echo " 4. git push" | |
| echo "" | |
| exit 1 | |
| fi | |
| echo "Lock file is in sync. Proceeding with Jest tests..." | |
| - name: Install Dependencies | |
| run: npm install | |
| - name: Run Jest Tests | |
| id: jest | |
| run: | | |
| npm test -- --json --outputFile=jest-results.json || echo "TESTS_FAILED=true" >> $GITHUB_ENV | |
| - name: Read Jest Results | |
| id: results | |
| run: | | |
| # Default TESTS_FAILED to false | |
| if [ -z "$TESTS_FAILED" ]; then | |
| TESTS_FAILED=false | |
| fi | |
| # Set TESTS_PASSED properly | |
| if [ "$TESTS_FAILED" = "true" ]; then | |
| TESTS_PASSED=false | |
| echo "TESTS_PASSED=false" >> $GITHUB_ENV | |
| FAILED_TESTS=$(jq -r '[.testResults[] | select(.status == "failed") | .name] | map(split("/") | last) | join("\n")' jest-results.json) | |
| echo "FAILED_TESTS<<EOF" >> $GITHUB_ENV | |
| echo "$FAILED_TESTS" >> $GITHUB_ENV | |
| echo "EOF" >> $GITHUB_ENV | |
| else | |
| TESTS_PASSED=true | |
| echo "TESTS_PASSED=true" >> $GITHUB_ENV | |
| echo "FAILED_TESTS=None" >> $GITHUB_ENV | |
| fi | |
| # Extract PR number | |
| PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") | |
| # Generate a temporary file for the comment | |
| COMMENT_FILE=$(mktemp) | |
| # Save variables to GITHUB_OUTPUT | |
| echo "TESTS_PASSED=$TESTS_PASSED" >> $GITHUB_ENV | |
| echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT | |
| echo "COMMENT_FILE=$COMMENT_FILE" >> $GITHUB_OUTPUT | |
| # Generate content for the comment file | |
| { | |
| if [ "$TESTS_PASSED" = "true" ]; then | |
| echo "✅ All Jest tests passed! This PR is ready to merge." | |
| else | |
| echo "❌ Some Jest tests failed. Please check the logs and fix the issues before merging." | |
| echo "" | |
| echo "**Failed Tests:**" | |
| echo "" | |
| echo '```' | |
| echo "$FAILED_TESTS" | |
| echo '```' | |
| fi | |
| } > "$COMMENT_FILE" | |
| - name: PR comment | |
| uses: thollander/actions-comment-pull-request@v3 | |
| with: | |
| file-path: ${{ steps.results.outputs.COMMENT_FILE }} | |
| pr-number: ${{ steps.results.outputs.PR_NUMBER }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} |