Add factory_bot.before_run_factory instrumentation event#1795
Open
hammadxcm wants to merge 1 commit intothoughtbot:mainfrom
Open
Add factory_bot.before_run_factory instrumentation event#1795hammadxcm wants to merge 1 commit intothoughtbot:mainfrom
factory_bot.before_run_factory instrumentation event#1795hammadxcm wants to merge 1 commit intothoughtbot:mainfrom
Conversation
Fire a non-block ActiveSupport::Notifications event immediately before the existing `run_factory` instrument block. This allows subscribers to build a factory call stack at runtime, which is useful for debugging errors in deeply nested association callbacks where the standard stack trace only shows the top-level factory. The payload is identical to `run_factory` (`:name`, `:strategy`, `:traits`, `:overrides`, `:factory`). Closes thoughtbot#1673
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
factory_bot.before_run_factoryActiveSupport::Notifications event that fires immediately beforerun_factoryinstrumentation_payload— no new payload construction neededCloses #1673
Motivation
When factories are deeply nested via associations, errors in
aftercallbacks only surface the top-level factory in the stack trace. Abefore_run_factoryevent lets users push/pop a call stack to identify the exact factory chain that led to an error.Existing alternatives don't work:
compile_factoryonly fires once per factory definition (cached)start/finishsubscriber onrun_factoryworks but is an obscure pattern most users won't discoverChanges
lib/factory_bot/factory_runner.rbinstrumentcall before existingrun_factoryblockspec/acceptance/activesupport_instrumentation_spec.rbdocs/src/activesupport-instrumentation/summary.mdGETTING_STARTED.mdNEWS.mdTest plan
bundle exec rspec spec/acceptance/activesupport_instrumentation_spec.rb)bundle exec rake standard— no formatting offenses