diff --git a/lib/buildkite/test_collector/library_hooks/rspec.rb b/lib/buildkite/test_collector/library_hooks/rspec.rb index dd7960b..fa84bcc 100644 --- a/lib/buildkite/test_collector/library_hooks/rspec.rb +++ b/lib/buildkite/test_collector/library_hooks/rspec.rb @@ -23,13 +23,20 @@ # The _buildkite prefix here is added as a safeguard against name collisions # as we are in the main thread Thread.current[:_buildkite_tracer] = tracer - example.run - Thread.current[:_buildkite_tracer] = nil - - tracer.finalize - - trace = Buildkite::TestCollector::RSpecPlugin::Trace.new(example, history: tracer.history) - Buildkite::TestCollector.uploader.traces[example.id] = trace + # It's important to use begin/ensure here, because otherwise if other hooks fail, + # the cleanup code won't run, meaning we will miss some data. + # + # Having said that, this behavior isn't documented by RSpec. + begin + example.run + ensure + Thread.current[:_buildkite_tracer] = nil + + tracer.finalize + + trace = Buildkite::TestCollector::RSpecPlugin::Trace.new(example, history: tracer.history) + Buildkite::TestCollector.uploader.traces[example.id] = trace + end end config.after(:suite) do