Skip to content

Commit bd76d10

Browse files
authored
Merge pull request rspec#2442 from devonestes/skip-context-all-blocks
Skip context all blocks
2 parents 8812bcf + 8574e68 commit bd76d10

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/rspec/core/hooks.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ def run(position, scope, example_or_group)
456456
return if RSpec.configuration.dry_run?
457457

458458
if scope == :context
459-
run_owned_hooks_for(position, :context, example_or_group)
459+
unless example_or_group.class.metadata[:skip]
460+
run_owned_hooks_for(position, :context, example_or_group)
461+
end
460462
else
461463
case position
462464
when :before then run_example_hooks_for(example_or_group, :before, :reverse_each)

spec/rspec/core/hooks_filtering_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,57 @@ module RSpec::Core
319319
expect(filters).to eq([])
320320
end
321321

322+
it "runs :all|:context hooks even if there are no unskipped examples in that context" do
323+
filters = []
324+
group = RSpec.describe("un-skipped describe") do
325+
before(:all) { filters << "before all in group"}
326+
after(:all) { filters << "after all in group"}
327+
328+
xcontext("skipped context") do
329+
before(:context) { filters << "before context in group"}
330+
after(:context) { filters << "after context in group"}
331+
332+
it("is skipped") {}
333+
end
334+
end
335+
group.run
336+
expect(filters).to eq(["before all in group", "after all in group"])
337+
end
338+
339+
it "does not run :all|:context hooks in global config if the entire context is skipped" do
340+
filters = []
341+
RSpec.configure do |c|
342+
c.before(:all) { filters << "before all in config"}
343+
c.after(:all) { filters << "after all in config"}
344+
c.before(:context) { filters << "before context in config"}
345+
c.after(:context) { filters << "after context in config"}
346+
end
347+
group = RSpec.xdescribe("skipped describe") do
348+
context("skipped context") do
349+
it("is skipped") {}
350+
end
351+
end
352+
group.run
353+
expect(filters).to eq([])
354+
end
355+
356+
it "does not run local :all|:context hooks if the entire context is skipped" do
357+
filters = []
358+
group = RSpec.xdescribe("skipped describe") do
359+
before(:all) { filters << "before all in group"}
360+
after(:all) { filters << "after all in group"}
361+
362+
context("skipped context") do
363+
before(:context) { filters << "before context in group"}
364+
after(:context) { filters << "after context in group"}
365+
366+
it("is skipped") {}
367+
end
368+
end
369+
group.run
370+
expect(filters).to eq([])
371+
end
372+
322373
context "when the hook filters apply to individual examples instead of example groups" do
323374
let(:each_filters) { [] }
324375
let(:all_filters) { [] }

0 commit comments

Comments
 (0)