Update RSpec timing adaptor to be more resilient #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
In a project where we are using Knapsack, we noticed that some tests were incorrectly reporting a much shorter time than they were actually taking.
After some investigation, I noticed that it was because we had
Knapsack::Adapters::RSpecAdapter.bind
placed at the bottom of ourspec_helper.rb
Above this, we had an
RSpec.configure
block that contained a slowafter(:each)
block that was adding about 1 second per test - doing DB work.Because
before
aliasesappend_before
andafter
aliasesprepend_after
, if the bind happens after the config of these blocks, theafter
block that contains the end of the timing calculation is executed before the other config block.http://www.rubydoc.info/github/rspec/rspec-core/RSpec%2FCore%2FConfiguration:after
Example Repo
I have pushed a minimal app that shows the issue here
https://github.com/aterris/knapsack-issue
Safer Implementation
While we did not match the prescribed documentation (by putting bind at the bottom instead of top of spec_helper), it feels like this is an easy mistake for other users to make and is also something we can help protect against.
RSpec provides
prepend_before
andappend_after
for extension type work that would fix this in most cases (including the sample app above). It is still possible to hit this if the user is using those methods directly, but this change would make that much less likely since users are not often using them. These methods were added in 2.10 as seen in the changelogI have updated the
rspec_adaptor_spec.rb
but have not added a test specifically around this case. I can look into that but hadn't found what felt like a good way to test this yet.Final Note
I actually noticed this bug originally with Knapsack Pro but ended up just making this PR here for now. If you are happy with this change, I can make at PR on pro as well, or you can do it yourself, whichever you prefer.