Skip to content

Commit 7bc21fe

Browse files
committed
Warn rspec testers when they expect_inertia but render inertia: is never called
1 parent 8465725 commit 7bc21fe

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/inertia_rails/rspec.rb

Lines changed: 6 additions & 0 deletions
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

@@ -57,6 +60,9 @@ def inertia_tests_setup?
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/inertia/rspec_helper_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
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+
end
14+
315
RSpec.describe InertiaRails::RSpec, type: :request do
416
describe 'correctly set up inertia tests with inertia: true', inertia: true do
517
context 'with props' do
@@ -61,5 +73,41 @@
6173
it 'does not complain about test helpers' do
6274
expect { expect_inertia }.not_to raise_error
6375
end
76+
77+
# h/t for this technique:
78+
# https://blog.arkency.com/testing-deprecations-warnings-with-rspec/
79+
it 'warns that the renderer was never created' do
80+
begin
81+
original_stderr = $stderr
82+
fake_std_err = FakeStdErr.new
83+
$stderr = fake_std_err
84+
expect_inertia
85+
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'
86+
expect(fake_std_err.messages[0].chomp).to(eq(warn_message))
87+
ensure
88+
$std_err = original_stderr
89+
end
90+
end
91+
92+
context 'with the :skip_missing_renderer_warnings setting set to true' do
93+
before {
94+
@original = ::RSpec.configuration.inertia[:skip_missing_renderer_warnings]
95+
::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = true
96+
}
97+
after {
98+
::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = @original
99+
}
100+
it 'skips the warning' do
101+
begin
102+
original_stderr = $stderr
103+
fake_std_err = FakeStdErr.new
104+
$stderr = fake_std_err
105+
expect_inertia
106+
expect(fake_std_err.messages).to be_empty
107+
ensure
108+
$std_err = original_stderr
109+
end
110+
end
111+
end
64112
end
65113
end

0 commit comments

Comments
 (0)