|
| 1 | +module ReactOnRails |
| 2 | + module TestHelper |
| 3 | + # Because you will probably want to run RSpec tests that rely on compiled webpack assets |
| 4 | + # (typically, your integration/feature specs where `js: true`), you will want to ensure you |
| 5 | + # don't accidentally run tests on missing or stale webpack assets. If you did use stale |
| 6 | + # Webpack assets, you will get invalid test results as your tests do not use the very latest |
| 7 | + # JavaScript code. |
| 8 | + # |
| 9 | + # Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` |
| 10 | + # file, passing the config as an argument. You can customize this to your particular needs by |
| 11 | + # replacing any of the default components. |
| 12 | + # |
| 13 | + # RSpec.configure do |config| |
| 14 | + # ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) |
| 15 | + # |
| 16 | + # You can pass an RSpec metatag as an optional second parameter to this helper method |
| 17 | + # if you want this helper to run on examples other than where `js: true` (default). The helper |
| 18 | + # will compile webpack files at most once per test run. |
| 19 | + # |
| 20 | + # If you do not want to be slowed down by re-compiling webpack assets from scratch every test |
| 21 | + # run, you can call `npm run build:client` (and `npm run build:server` if doing server |
| 22 | + # rendering) to have webpack recompile these files in the background, which will be *much* |
| 23 | + # faster. The helper looks for these processes and will abort recompiling if it finds them |
| 24 | + # to be running. |
| 25 | + # |
| 26 | + # See docs/additional_reading/rspec_configuration.md for more info |
| 27 | + def self.configure_rspec_to_compile_assets(config, metatag = :js) |
| 28 | + config.before(:example, metatag) { ReactOnRails::TestHelper.ensure_assets_compiled } |
| 29 | + end |
| 30 | + |
| 31 | + # Main entry point to ensuring assets are compiled. See `configure_rspec_to_compile_assets` for |
| 32 | + # an example of usage. |
| 33 | + # |
| 34 | + # Typical usage passes all params as nil defaults. |
| 35 | + # webpack_assets_status_checker: provide: `up_to_date?`, `whats_not_up_to_date`, `client_dir` |
| 36 | + # defaults to ReactOnRails::TestHelper::WebpackAssetsStatusChecker |
| 37 | + # webpack_process_checker: provide one method: `def running?` |
| 38 | + # defaults to ReactOnRails::TestHelper::WebpackProcessChecker |
| 39 | + # webpack_assets_compiler: provide one method: `def compile` |
| 40 | + # defaults to ReactOnRails::TestHelper::WebpackAssetsCompiler |
| 41 | + # client_dir and compiled_dirs are passed into the default webpack_assets_status_checker if you |
| 42 | + # don't provide one. |
| 43 | + def self.ensure_assets_compiled(webpack_assets_status_checker: nil, |
| 44 | + webpack_assets_compiler: nil, |
| 45 | + webpack_process_checker: nil, |
| 46 | + client_dir: nil, |
| 47 | + compiled_dirs: nil) |
| 48 | + |
| 49 | + if webpack_assets_status_checker.nil? |
| 50 | + client_dir ||= Rails.root.join("client") |
| 51 | + compiled_dirs ||= ReactOnRails.configuration.generated_assets_dirs |
| 52 | + webpack_assets_status_checker ||= |
| 53 | + WebpackAssetsStatusChecker.new(client_dir: client_dir, |
| 54 | + compiled_dirs: compiled_dirs) |
| 55 | + end |
| 56 | + |
| 57 | + webpack_assets_compiler ||= WebpackAssetsCompiler.new |
| 58 | + webpack_process_checker ||= WebpackProcessChecker.new |
| 59 | + |
| 60 | + ReactOnRails::TestHelper::EnsureAssetsCompiled.new( |
| 61 | + webpack_assets_status_checker: webpack_assets_status_checker, |
| 62 | + webpack_assets_compiler: webpack_assets_compiler, |
| 63 | + webpack_process_checker: webpack_process_checker |
| 64 | + ).call |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments