Testing Configuration Guide
This guide explains how to configure React on Rails for optimal testing with RSpec, Minitest, or other test frameworks.
Quick Start
For most applications, the simplest approach is to let Shakapacker handle asset compilation automatically:
# config/shakapacker.yml
test:
compile: true
public_output_path: webpack/testThat's it! Shakapacker will automatically compile assets before running tests.
Two Approaches to Test Asset Compilation
React on Rails supports two mutually exclusive approaches for compiling webpack assets during tests:
Approach 1: Shakapacker Auto-Compilation (Recommended)
Best for: Most applications, especially simpler test setups
Configuration:
# config/shakapacker.yml
test:
<<: *default
compile: true
public_output_path: webpack/testHow it works:
- Shakapacker automatically compiles assets when they're requested
- No additional configuration in React on Rails or test helpers needed
- Assets are compiled on-demand during test runs
Pros:
- ✅ Simplest configuration
- ✅ No extra setup in spec helpers
- ✅ Automatically integrates with Rails test environment
- ✅ Works with any test framework (RSpec, Minitest, etc.)
Cons:
- ⚠️ May compile assets multiple times during test runs
- ⚠️ Less explicit control over when compilation happens
- ⚠️ Can slow down tests if assets change frequently
When to use:
- You want the simplest possible configuration
- Your test suite is relatively fast
- You don't mind automatic compilation on-demand
Approach 2: React on Rails Test Helper (Explicit Control)
Best for: Applications needing precise control over compilation timing
Configuration:
# config/initializers/react_on_rails.rb
ReactOnRails.configure do |config|
config.build_test_command = "RAILS_ENV=test bin/shakapacker"
end# spec/rails_helper.rb (for RSpec)
require "react_on_rails/test_helper"
RSpec.configure do |config|
# Ensures webpack assets are compiled before the test suite runs
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
end# test/test_helper.rb (for Minitest)
require "react_on_rails/test_helper"
class ActiveSupport::TestCase
# Ensures webpack assets are compiled before running tests
ReactOnRails::TestHelper.ensure_assets_compiled
endHow it works:
- Compiles assets once before the test suite starts
- Uses the
build_test_commandconfiguration - Fails fast if compilation has errors
Pros:
- ✅ Explicit control over compilation timing
- ✅ Assets compiled only once per test run
- ✅ Clear error messages if compilation fails
- ✅ Can customize the build command
Cons:
- ⚠️ Requires additional configuration in test helpers
- ⚠️ More setup to maintain
- ⚠️ Requires
build_test_commandto be set
When to use:
- You want to compile assets exactly once before tests
- You need to customize the build command
- You want explicit error handling for compilation failures
- Your test suite is slow and you want to optimize compilation
⚠️ Important: Don't Mix Approaches
Do not use both approaches together. They are mutually exclusive:
❌ Wrong:
# config/shakapacker.yml
test:
compile: true # ← Don't do this...# config/initializers/react_on_rails.rb
config.build_test_command = "RAILS_ENV=test bin/shakapacker" # ← ...with this
# spec/rails_helper.rb
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) # ← ...and thisThis will cause assets to be compiled multiple times unnecessarily.
Migrating Between Approaches
From React on Rails Test Helper → Shakapacker Auto-Compilation
-
Set
compile: trueinconfig/shakapacker.ymltest section:test: compile: true public_output_path: webpack/test -
Remove test helper configuration from spec/test helpers:
# spec/rails_helper.rb - REMOVE these lines: require "react_on_rails/test_helper" ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) -
Remove or comment out
build_test_commandin React on Rails config:# config/initializers/react_on_rails.rb # config.build_test_command = "RAILS_ENV=test bin/shakapacker" # ← Comment out
From Shakapacker Auto-Compilation → React on Rails Test Helper
-
Set
compile: falseinconfig/shakapacker.ymltest section:test: compile: false public_output_path: webpack/test -
Add
build_test_commandto React on Rails config:# config/initializers/react_on_rails.rb config.build_test_command = "RAILS_ENV=test bin/shakapacker" -
Add test helper configuration:
# spec/rails_helper.rb (for RSpec) require "react_on_rails/test_helper" RSpec.configure do |config| ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) end
Verifying Your Configuration
Use the React on Rails doctor command to verify your test configuration:
bundle exec rake react_on_rails:doctorThe doctor will check:
- Whether
compile: trueis set in shakapacker.yml - Whether
build_test_commandis configured - Whether test helpers are properly set up
- Whether you're accidentally using both approaches
Troubleshooting
Assets not compiling during tests
Problem: Tests fail because JavaScript/CSS assets are not compiled.
Solution: Check which approach you're using:
-
If using Shakapacker auto-compilation:
# config/shakapacker.yml test: compile: true # ← Make sure this is true -
If using React on Rails test helper:
- Verify
build_test_commandis set - Check that test helper is configured in spec/test helper
- Run
bundle exec rake react_on_rails:doctor
- Verify
Assets compiling multiple times
Problem: Tests are slow because assets compile repeatedly.
Solutions:
-
If using Shakapacker auto-compilation:
- Switch to React on Rails test helper for one-time compilation
- Or ensure
cache_manifest: truein shakapacker.yml
-
If using React on Rails test helper:
- This shouldn't happen - assets should compile only once
- Check that you don't also have
compile: truein shakapacker.yml
Build command fails
Problem: build_test_command fails with errors.
Check:
-
Does
bin/shakapackerexist and is it executable?ls -la bin/shakapacker chmod +x bin/shakapacker # If needed -
Can you run the command manually?
RAILS_ENV=test bin/shakapacker -
Are your webpack configs valid for test environment?
Test helper not found
Problem: LoadError: cannot load such file -- react_on_rails/test_helper
Solution: Make sure react_on_rails gem is available in test environment:
# Gemfile
gem "react_on_rails", ">= 16.0" # Not in a specific group
# Or explicitly in test group:
group :test do
gem "react_on_rails"
endPerformance Considerations
Asset Compilation Speed
Shakapacker auto-compilation:
- Compiles on first request per test process
- May compile multiple times in parallel test environments
- Good for: Small test suites, simple webpack configs
React on Rails test helper:
- Compiles once before entire test suite
- Blocks test start until compilation complete
- Good for: Large test suites, complex webpack configs
Caching Strategies
Improve compilation speed with caching:
# config/shakapacker.yml
test:
cache_manifest: true # Cache manifest between runs# config/initializers/react_on_rails.rb
# If using test helper, webpack will use its own caching
config.build_test_command = "RAILS_ENV=test bin/shakapacker"Parallel Testing
When running tests in parallel (with parallel_tests gem):
Shakapacker auto-compilation:
- Each process compiles independently (may be slow)
- Consider precompiling assets before running parallel tests:
RAILS_ENV=test bin/shakapacker bundle exec rake parallel:spec
React on Rails test helper:
- Compiles once before forking processes (efficient)
- Works well out of the box with parallel testing
CI/CD Considerations
GitHub Actions / GitLab CI
Option 1: Precompile before tests
- name: Compile test assets
run: RAILS_ENV=test bundle exec rake react_on_rails:assets:compile_environment
- name: Run tests
run: bundle exec rspecOption 2: Use Shakapacker auto-compilation
# config/shakapacker.yml
test:
compile: true
# CI workflow
- name: Run tests (assets auto-compile)
run: bundle exec rspecDocker
When running tests in Docker, consider:
- Caching
node_modulesbetween builds - Precompiling assets in Docker build stage
- Using bind mounts for local development
Best Practices
- Choose one approach - Don't mix Shakapacker auto-compilation with React on Rails test helper
- Use doctor command - Run
rake react_on_rails:doctorto verify configuration - Precompile in CI - Consider precompiling assets before running tests in CI
- Cache node_modules - Speed up installation with caching
- Monitor compile times - If tests are slow, check asset compilation timing
Summary Decision Matrix
| Scenario | Recommendation |
|---|---|
| Simple test setup | Shakapacker compile: true |
| Large test suite | React on Rails test helper |
| Parallel testing | React on Rails test helper or precompile |
| CI/CD pipeline | Precompile before tests |
| Quick local tests | Shakapacker compile: true |
| Custom build command | React on Rails test helper |
Related Documentation
Need Help?
- Forum: ShakaCode Forum
- Docs: React on Rails Guides
- Support: [email protected]