Module: ReactOnRails::TestHelper

Defined in:
lib/react_on_rails/test_helper.rb,
lib/react_on_rails/test_helper/dev_assets_detector.rb,
lib/react_on_rails/test_helper/ensure_assets_compiled.rb,
lib/react_on_rails/test_helper/webpack_assets_compiler.rb,
lib/react_on_rails/test_helper/webpack_assets_status_checker.rb,
sig/react_on_rails/test_helper.rbs

Defined Under Namespace

Modules: EnsureAssetsCompiled Classes: DevAssetsDetector, WebpackAssetsCompiler, WebpackAssetsStatusChecker

Class Method Summary collapse

Class Method Details

.configure_rspec_to_compile_assets(config, *metatags) ⇒ void

This method returns an undefined value.

Because you will probably want to run RSpec tests that rely on compiled JavaScript assets (typically, your integration/feature specs where js: true), you will want to ensure you don't accidentally run tests on missing or stale generated assets. If you did use stale assets, you will get invalid test results as your tests do not use the very latest JavaScript code.

Call this method from inside of the RSpec.configure block in your spec/rails_helper.rb file, passing the config as an argument. You can customize this to your particular needs by replacing any of the default components.

RSpec.configure do |config| ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)

You can pass an RSpec metatag as an list of parameter to this helper method if you want this helper to run on examples other than where js: true or server_rendering: true (default). The helper will compile generated files at most once per test run.

If you do not want to be slowed down by re-compiling generated assets from scratch every test run, you can call yarn run build:client (and yarn run build:server if doing server rendering) to have the bundler recompile these files in the background, which will be much faster. The helper looks for these processes and will abort recompiling if it finds them to be running.

See https://github.com/shakacode/react_on_rails/blob/master/docs/oss/building-features/testing-configuration.md for more info

Params: config - config for rspec metatags - metatags to add the ensure_assets_compiled check. Default is :js, :server_rendering, :controller

Parameters:

  • config (Object)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/react_on_rails/test_helper.rb', line 36

def self.configure_rspec_to_compile_assets(config, *metatags)
  metatags = %i[js server_rendering controller] if metatags.empty?

  # Supported since RSpec 3.5.0
  supports_first_matching_example = config.respond_to?(:when_first_matching_example_defined)

  metatags.each do |metatag|
    if supports_first_matching_example
      config.when_first_matching_example_defined(metatag) do
        ReactOnRails::TestHelper.ensure_assets_compiled
      end
    else
      config.before(:example, metatag) { ReactOnRails::TestHelper.ensure_assets_compiled }
    end
  end
end

.ensure_assets_compiled(webpack_assets_status_checker: nil, webpack_assets_compiler: nil, source_path: nil, generated_assets_full_path: nil, webpack_generated_files: nil) ⇒ void

This method returns an undefined value.

Main entry point to ensuring assets are compiled. See configure_rspec_to_compile_assets for an example of usage.

Typical usage passes all params as nil defaults. webpack_assets_status_checker: provide: up_to_date?, whats_not_up_to_date, source_path defaults to ReactOnRails::TestHelper::WebpackAssetsStatusChecker webpack_assets_compiler: provide one method: def compile defaults to ReactOnRails::TestHelper::WebpackAssetsCompiler source_path and generated_assets_full_path are passed into the default webpack_assets_status_checker if you don't provide one. webpack_generated_files List of files to check for up-to-date-status, defaulting to webpack_generated_files in your configuration rubocop:disable Metrics/CyclomaticComplexity

Parameters:

  • force_compile: (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/react_on_rails/test_helper.rb', line 66

def self.ensure_assets_compiled(webpack_assets_status_checker: nil,
                                webpack_assets_compiler: nil,
                                source_path: nil,
                                generated_assets_full_path: nil,
                                webpack_generated_files: nil)
  ReactOnRails::PackerUtils.check_manifest_not_cached
  if webpack_assets_status_checker.nil?
    source_path ||= ReactOnRails::Utils.source_path
    generated_assets_full_path ||= ReactOnRails::Utils.generated_assets_full_path
    webpack_generated_files ||= ReactOnRails.configuration.webpack_generated_files

    webpack_assets_status_checker ||=
      WebpackAssetsStatusChecker.new(source_path:,
                                     generated_assets_full_path:,
                                     webpack_generated_files:)

    unless @printed_once
      puts
      puts "====> React On Rails: Checking files in " \
           "#{webpack_assets_status_checker.generated_assets_full_path} for " \
           "outdated/missing bundles based on source_path #{source_path}"
      puts
      @printed_once = true

      if ReactOnRails::Utils.using_packer_source_path_is_not_defined_and_custom_node_modules?
        msg = <<~MSG
          WARNING: Define config/shakapacker.yml to include sourcePath to configure
          the location of your JavaScript source for React on Rails.
          Default location of #{source_path} is used.
        MSG
        puts ReactOnRails::Utils.wrap_message(msg, :orange)
      end
    end
  end

  webpack_assets_compiler ||= WebpackAssetsCompiler.new

  ReactOnRails::TestHelper::EnsureAssetsCompiled.new(
    webpack_assets_status_checker:,
    webpack_assets_compiler:
  ).call
end

.webpack_assets_compiled?Boolean

Returns:

  • (Boolean)


5
# File 'sig/react_on_rails/test_helper.rbs', line 5

def self.webpack_assets_compiled?: () -> bool