Skip to content

Commit fa03d73

Browse files
authored
Merge pull request #62 from inertiajs/better-setup-specs-warning
Better setup specs warning
2 parents 5c71d77 + 2af91cf commit fa03d73

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

lib/inertia_rails/rspec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module Helpers
3535
def inertia
3636
raise 'Inertia test helpers aren\'t set up! Make sure you add inertia: true to describe blocks using inertia tests.' unless inertia_tests_setup?
3737

38+
if @_inertia_render_wrapper.nil? && !::RSpec.configuration.inertia[:skip_missing_renderer_warnings]
39+
warn 'WARNING: the test never created an Inertia renderer. Maybe the code wasn\'t able to reach a `render inertia:` call? If this was intended, or you don\'t want to see this message, set ::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = true'
40+
end
3841
@_inertia_render_wrapper
3942
end
4043

@@ -49,14 +52,17 @@ def inertia_wrap_render(render)
4952
protected
5053

5154
def inertia_tests_setup?
52-
@_inertia_render_wrapper.present?
55+
::RSpec.current_example.metadata.fetch(:inertia, false)
5356
end
5457
end
5558
end
5659
end
5760

5861
RSpec.configure do |config|
5962
config.include ::InertiaRails::RSpec::Helpers
63+
config.add_setting :inertia, default: {
64+
skip_missing_renderer_warnings: false
65+
}
6066

6167
config.before(:each, inertia: true) do
6268
new_renderer = InertiaRails::Renderer.method(:new)

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def inertia_partial_request_test
2323
end
2424
end
2525

26+
def non_inertiafied
27+
render plain: 'hey'
28+
end
29+
2630
# Calling it my_location to avoid this in Rails 5.0
2731
# https://github.com/rails/rails/issues/28033
2832
def my_location

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
get 'error_500' => 'inertia_test#error_500'
2525
get 'content_type_test' => 'inertia_test#content_type_test'
2626
get 'lazy_props' => 'inertia_render_test#lazy_props'
27+
get 'non_inertiafied' => 'inertia_test#non_inertiafied'
2728

2829
inertia 'inertia_route' => 'TestComponent'
2930
end

spec/inertia/rspec_helper_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
require_relative '../../lib/inertia_rails/rspec'
22

3+
class FakeStdErr
4+
attr_accessor :messages
5+
6+
def initialize
7+
@messages = []
8+
end
9+
10+
def write(msg)
11+
@messages << msg
12+
end
13+
14+
# Rails 5.0 + Ruby 2.6 require puts to be a public method
15+
def puts(thing)
16+
end
17+
end
18+
319
RSpec.describe InertiaRails::RSpec, type: :request do
420
describe 'correctly set up inertia tests with inertia: true', inertia: true do
521
context 'with props' do
@@ -54,4 +70,48 @@
5470
expect { expect_inertia }.to raise_error(/inertia: true/)
5571
end
5672
end
73+
74+
describe 'expecting inertia on a non inertia route', inertia: true do
75+
before { get non_inertiafied_path }
76+
77+
it 'does not complain about test helpers' do
78+
expect { expect_inertia }.not_to raise_error
79+
end
80+
81+
# h/t for this technique:
82+
# https://blog.arkency.com/testing-deprecations-warnings-with-rspec/
83+
it 'warns that the renderer was never created' do
84+
begin
85+
original_stderr = $stderr
86+
fake_std_err = FakeStdErr.new
87+
$stderr = fake_std_err
88+
expect_inertia
89+
warn_message = 'WARNING: the test never created an Inertia renderer. Maybe the code wasn\'t able to reach a `render inertia:` call? If this was intended, or you don\'t want to see this message, set ::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = true'
90+
expect(fake_std_err.messages[0].chomp).to(eq(warn_message))
91+
ensure
92+
$std_err = original_stderr
93+
end
94+
end
95+
96+
context 'with the :skip_missing_renderer_warnings setting set to true' do
97+
before {
98+
@original = ::RSpec.configuration.inertia[:skip_missing_renderer_warnings]
99+
::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = true
100+
}
101+
after {
102+
::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = @original
103+
}
104+
it 'skips the warning' do
105+
begin
106+
original_stderr = $stderr
107+
fake_std_err = FakeStdErr.new
108+
$stderr = fake_std_err
109+
expect_inertia
110+
expect(fake_std_err.messages).to be_empty
111+
ensure
112+
$std_err = original_stderr
113+
end
114+
end
115+
end
116+
end
57117
end

0 commit comments

Comments
 (0)