Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit ae6596e

Browse files
committed
Skip unnecessary :context|:all hooks
This makes that previous test pass, and the test suite is still green, so I'm _assuming_ that this works and doesn't break other stuff! However, I'd be happy to make any alterations if this doesn't look like it's the right way of tackling this problem.
1 parent 8bf66f5 commit ae6596e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
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: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,24 @@ module RSpec::Core
319319
expect(filters).to eq([])
320320
end
321321

322-
it "does not run :all|:context hooks if the entire context is skipped" do
322+
it "runs :all|:context hooks even if there are no unskipped examples in that context" do
323+
filters = []
324+
group = RSpec.describe("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
323340
filters = []
324341
RSpec.configure do |c|
325342
c.before(:all) { filters << "before all in config"}
@@ -328,7 +345,24 @@ module RSpec::Core
328345
c.after(:context) { filters << "after context in config"}
329346
end
330347
group = RSpec.xdescribe("skipped describe") do
331-
xcontext("skipped context") 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+
332366
it("is skipped") {}
333367
end
334368
end

0 commit comments

Comments
 (0)