Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exception when using :focus tag to avoid skipping RSpec tests #167

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

### 3.2.1

* Raise exception when using `:focus` tag to avoid skipping RSpec tests

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/167

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v3.2.0...v3.2.1

### 3.2.0

* Add an error message to `KnapsackPro::Adapters::RspecAdapter#bind`
Expand Down
12 changes: 12 additions & 0 deletions lib/knapsack_pro/adapters/rspec_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def bind_time_tracker
current_test_path
end

if example.metadata[:focus] && KnapsackPro::Adapters::RSpecAdapter.rspec_configuration.filter.rules[:focus]
raise "We detected a test file path #{current_test_path} with a test using metadata `:focus` tag. RSpec might not run some tests in Queue Mode (causing random tests skipping problem). Please remove `:focus` tag from your codebase. See more: https://knapsackpro.com/faq/question/rspec-is-not-running-some-tests"
end

example.run
end

Expand Down Expand Up @@ -95,6 +99,14 @@ def bind_before_queue_hook
end
end
end

private

# Hide RSpec configuration so that we could mock it in the spec.
# Mocking existing RSpec configuration could impact test's runtime.
def self.rspec_configuration
::RSpec.configuration
end
end

# This is added to provide backwards compatibility
Expand Down
30 changes: 29 additions & 1 deletion spec/knapsack_pro/adapters/rspec_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,35 @@
let(:logger) { instance_double(Logger) }
let(:global_time) { 'Global time: 01m 05s' }
let(:test_path) { 'spec/a_spec.rb' }
let(:current_example) { double }
let(:current_example) { double(metadata: {}) }

context "when the example's metadata has :focus tag AND RSpec inclusion rule includes :focus" do
let(:current_example) { double(metadata: { focus: true }) }

it do
expect(KnapsackPro::Config::Env).to receive(:rspec_split_by_test_examples?).and_return(false)

expect(config).to receive(:prepend_before).with(:context).and_yield

allow(KnapsackPro).to receive(:tracker).and_return(tracker)
expect(tracker).to receive(:start_timer).ordered

expect(config).to receive(:around).with(:each).and_yield(current_example)
expect(::RSpec).to receive(:configure).and_yield(config)

expect(tracker).to receive(:stop_timer).ordered

expect(described_class).to receive(:test_path).with(current_example).and_return(test_path)

expect(tracker).to receive(:current_test_path=).with(test_path).ordered

expect(described_class).to receive_message_chain(:rspec_configuration, :filter, :rules, :[]).with(:focus).and_return(true)

expect {
subject.bind_time_tracker
}.to raise_error /We detected a test file path spec\/a_spec\.rb with a test using metadata `:focus` tag/
end
end

context 'when rspec split by test examples is disabled' do
before do
Expand Down