JavaScript Minification Testing
JavaScript Minification Testing
This directory contains automated tests to prevent JavaScript minification issues that can break your Jekyll site when deployed to GitHub Pages.
The Problem
GitHub Pages minifies JavaScript by removing newlines, which can cause issues with:
- Single-line comments (
//) that comment out the rest of the minified line - Function definitions that aren’t available when called
- Missing semicolons that cause syntax errors when minified
The Solution
Automated Testing
We’ve created comprehensive tests that check for these issues:
test_javascript_minification.py- Main test scripttest_js.sh- Simple shell wrapper.github/workflows/js-minification-tests.yml- GitHub Actions integrationpre-commit-hook.sh- Git pre-commit hook
Running Tests
Local Testing
# Quick test of source files only (fastest)
./test_js.sh source
# Test including draft posts (builds site first)
./test_js.sh drafts
# Full production test (builds with drafts + exits on errors)
./test_js.sh full
# Advanced usage
python3 test_javascript_minification.py --source-only # Source files only
python3 test_javascript_minification.py --build-with-drafts # Build + test with drafts
python3 test_javascript_minification.py --report detailed.md # Generate report
python3 test_javascript_minification.py --ci # CI mode (exit codes)
Testing Strategy
The test system supports different scenarios:
- Source-only testing (
--source-only)- Fast check of
_includes,_layouts,_pagesfiles - Good for development and pre-commit hooks
- Doesn’t require Jekyll build
- Fast check of
- Draft-inclusive testing (
--build-with-drafts)- Builds site with
jekyll build -Dfirst - Tests all content including draft posts
- Essential for comprehensive testing since GitHub Pages excludes drafts by default
- Builds site with
- Production testing (CI mode)
- Combines draft build with strict error checking
- Perfect for GitHub Actions and deployment gates
Setting Up Pre-commit Hook
# Install the pre-commit hook
cp pre-commit-hook.sh .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
# Now tests run automatically before each commit
git commit -m "Your changes" # Tests run automatically
GitHub Actions Integration
The workflow automatically runs on:
- Pushes to
gh-pages,main, ormasterbranches - Pull requests to these branches
- Manual trigger from GitHub Actions tab
What the Tests Check
- Single-line Comments (
//)- ❌
// This will break minified code - ✅
/* This works when minified */
- ❌
- Function Definitions
- Ensures functions called in
onclickhandlers are defined - Checks for proper function declaration timing
- Ensures functions called in
- Syntax Issues
- Missing semicolons that could cause minification problems
- Other common minification pitfalls
Example Test Output
🔍 Scanning for JavaScript minification issues...
📁 Site root: /home/jack/jackbarker-com-au
📄 Found 15 HTML files to check
🔧 Checking _includes/portfolio-slideshow.html (1 JS blocks)
💥 CRITICAL ISSUES (2):
❌ CRITICAL: _includes/portfolio-slideshow.html:147 - Single-line comment found: '// Define functions immediately'
❌ CRITICAL: _includes/portfolio-slideshow.html:195 - Single-line comment found: '// Update progress bar'
⚠️ WARNINGS (1):
⚠️ WARNING: _includes/portfolio-slideshow.html:146 - Function 'changeSlide' called in onclick but not defined in this script block
📊 Scanned 3 JavaScript blocks
Quick Fix Guidelines
- Replace single-line comments:
// Bad - will break when minified function myFunction() { // This comment breaks minification return true; } /* Good - works when minified */ function myFunction() { /* This comment works fine */ return true; } - Ensure function definitions come first:
/* Bad - function called before definition */ onclick="myFunction()" // ... later in script ... function myFunction() { } /* Good - function defined immediately */ function myFunction() { } // ... can now use onclick="myFunction()" safely - Add semicolons:
// Bad - might break when minified var x = 5 var y = 10 // Good - explicit statement endings var x = 5; var y = 10;
Integration with CI/CD
The tests integrate with your development workflow:
- Local development: Run
./test_js.shbefore committing - Pre-commit: Automatically blocks commits with issues
- GitHub Actions: Tests run on every push/PR
- Deployment: Only clean code gets deployed to GitHub Pages
Continuous Monitoring
Set up notifications to catch issues early:
- GitHub Actions will fail if issues are found
- Pull requests get automatic comments with test results
- Test reports are saved as build artifacts
- Email notifications for failed builds (configure in GitHub settings)
This comprehensive testing approach ensures your JavaScript will work correctly when GitHub Pages minifies it for production deployment.