Skip to content

Fix missing failed examples if rspec hooks fail#221

Merged
zhming0 merged 1 commit intomainfrom
ming/tai-427
Sep 9, 2024
Merged

Fix missing failed examples if rspec hooks fail#221
zhming0 merged 1 commit intomainfrom
ming/tai-427

Conversation

@zhming0
Copy link
Contributor

@zhming0 zhming0 commented Sep 9, 2024

[Skipping my journey of discovering this because this is a public repo....]

Rspec's around hook has some undocumented behavoir, look at this example:

RSpec.configure do |config|
  config.around(:each) do |example|
    puts "Outer around hook start"
    begin
      example.run
    ensure
      puts "Outer around hook end"
    end
  end
end

RSpec.configure do |config|
  config.around(:each) do |example|
    puts "Inner around hook start"
    raise "Error in inner hook"
    example.run
    puts "Inner around hook end"
  end
end

RSpec.describe "Example" do
  it "does something" do
    expect(true).to be true
  end
end

If we don't use ensure then neither "Inner around hook end" and "Outer around hook end" gets executed.


In another word, prior to this fix, any Rspec test fails that's caused by around hook failure will be missed.

@zhming0 zhming0 requested a review from a team September 9, 2024 00:15
@wooly
Copy link

wooly commented Sep 9, 2024

@zhming0 this definitely looks a bit suspicious (the issue, rather than the solution)! what's the output for the Example test? What's the output if you do fail inside the test instead of the true == true expectation?

@zhming0
Copy link
Contributor Author

zhming0 commented Sep 9, 2024

@wooly in the case when the test itself broke, around hook gets fully executed.

The output in my example:

Outer around hook start
Inner around hook start
Outer around hook end

(otherwise we would have a completely broken collector 😅 )

@zhming0 zhming0 changed the title TAI-427: fix missing failed examples if rspec hooks fail Fix missing failed examples if rspec hooks fail Sep 9, 2024
@niceking
Copy link
Contributor

niceking commented Sep 9, 2024

I think I follow your change but I'm missing a bit of context still.

I follow the code change - I think what you're saying is that we need to add a begin ... ensure block around running the example, and then ensuring that the result is persisted, so that any errors that are raised as part of the example running don't bubble up and cause the error result to not be stored

I don't really follow what you're trying to illustrate with the example in the readme?

@niceking
Copy link
Contributor

niceking commented Sep 9, 2024

I found your linear ticket that I won't link here 😛

I agree that this change works and fixes the issue of test failures not being ingested.

I don't think it has anything to do with the configuration of around hooks in places that aren't this repository? I'm just a little confused as to what you're trying to explain with the PR description, it may just be a case of editing the description to match the change though 🤷‍♀️

@niceking
Copy link
Contributor

niceking commented Sep 9, 2024

Hahaha ok re-reading the thread for the THIRD time and maybe making sense of it now 😛

Are you saying that this change fixes the ingestion of failed tests, where the test is failing due to an around hook (not configured by the collector) throwing an exception?

This still seems like the correct change to make but I'd like to just get a bit more context about testing.

My understanding of rspec before, after, and around hooks is that you can have heaps of them and they get executed in reverse order in which they are loaded. I'm wondering if the issue you're fixing here works for execeptions thrown in around blocks loaded before the collector, or after the collector, or both?

@zhming0
Copy link
Contributor Author

zhming0 commented Sep 9, 2024

@niceking sorry for the confusion.

Are you saying that this change fixes the ingestion of failed tests, where the test is failing due to an around hook (not configured by the collector) throwing an exception?

Correct.

I will try my best explaining why this is the fix, but happy to do a zoom catchup if it helps.

How this plugin works is through around hook. Target project call our hook setup code.

We expect when a test fail, no matter for what reason, our around hook's after code will be executed.

However, without the fix, other around hook's failure will stop our around hook's after code to be executed. This is explained in the example in PR description.

I'm wondering if the issue you're fixing here works for execeptions thrown in around blocks loaded before the collector, or after the collector, or both?

This is a good question. This works for around hooks declared after the collector's around hooks setup. We expect this plugin to be loaded earlier than most around hooks so this is okay.

Copy link
Contributor

@niceking niceking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth raising an issue with rspec core as well just so they're across this!

@zhming0 zhming0 merged commit 7eacf8a into main Sep 9, 2024
@zhming0 zhming0 deleted the ming/tai-427 branch September 9, 2024 06:04
@nprizal
Copy link
Contributor

nprizal commented Sep 9, 2024

I've looked into this issue last year and wrote a Basecamp post about it, but we decided to put it as low priority at that time. This solution makes sense to me, and I think it's similar to what I had in mind then. Good to see it being fixed 😊

@niceking
Copy link
Contributor

niceking commented Sep 9, 2024

Omg @nprizal I completely forgot about this post!! Good rememebering :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants