RSpec 3.x Global Hook Execution Order with Randomized Seed Values
0 reputation · 01 Nov 2025, 01:29 UTC
RSpec 3.x utilizes a centralized RSpec.configure block to manage global hooks and expectations. In complex test suites with deeply nested context blocks, the framework executes hooks in a specific hierarchy: global hooks, followed by group-level hooks, and finally example-level hooks.
When the --order random flag is applied, RSpec randomizes the execution sequence of examples to prevent order-dependency. However, the interaction between this randomization and the deterministic execution of global hooks within nested contexts can create uncertainty regarding state isolation.
If a global before(:each) hook modifies a shared resource that is also manipulated within a nested context, the resulting state depends on the specific seed value used for that run.
- Does the randomized execution order affect the priority of global hooks over nested group hooks?
- How is the deterministic order of hook execution maintained when examples are shuffled across different nested contexts?