Skip to content

Commit

Permalink
Use RSpec's when_first_matching_example_defined if available (#1496)
Browse files Browse the repository at this point in the history
This RSpec method was added in v3.5.0 to with expensive setup logic. It
results in similar behavior as just using `before(:example, ...)`, but
it will only run once instead of for each example.

Co-authored-by: Justin Gordon <justin@shakacode.com>
  • Loading branch information
mcls and justin808 authored Jan 29, 2023
1 parent 59747d5 commit 78c37d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Changes since last non-beta release.

### Added
- Exposed `reactHydrateOrRender` utility via [PR 1481](https://github.com/shakacode/react_on_rails/pull/1481) by [vaukalak](https://github.com/vaukalak).
- Optimized `ReactOnRails::TestHelper`'s RSpec integration using
`when_first_matching_example_defined` via [PR 1496](https://github.com/shakacode/react_on_rails/pull/1496)

### [13.1.0] - 2022-08-20

Expand Down
11 changes: 10 additions & 1 deletion lib/react_on_rails/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ module TestHelper
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|
config.before(:example, metatag) { ReactOnRails::TestHelper.ensure_assets_compiled }
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

Expand Down

0 comments on commit 78c37d9

Please sign in to comment.