How to call before(:suite) and after(:suite) RSpec hooks only once in Queue Mode?
This issue has been fixed in knapsack_pro version 7.0. Learn more about Knapsack Pro hooks in the docs.
For Knapsack Pro versions older than 7.0, keep reading.
Knapsack Pro Queue Mode runs subset of test files from the work queue many times. This means the RSpec hooks before(:suite)
and after(:suite)
will be executed multiple times. If you want to run some code only once before Queue Mode starts work and after it finishes then you should do it this way:
# spec_helper.rb or rails_helper.rb
KnapsackPro::Hooks::Queue.before_queue do |queue_id|
# This will be called only once before the tests started on the CI node.
# It will be run inside of the RSpec before(:suite) block only once.
# It means you will have access to whatever RSpec provides in the context of the before(:suite) block.
end
KnapsackPro::Hooks::Queue.after_queue do |queue_id|
# This will be called only once after test suite is finished.
# Note this hook won't be called inside of RSpec after(:suite) block because
# we are not able to determine which after(:suite) block will be called as the last one
# due to the fact the Knapsack Pro Queue Mode allocates tests in dynamic way.
end