-
-
Notifications
You must be signed in to change notification settings - Fork 430
Add flipper/test_help to automatically configure Flipper in tests/specs. #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2672714
f76d663
f5e26fb
32b3928
f3620af
414579e
a7d9266
903b9a5
05e2ce0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Flipper | ||
module TestHelp | ||
def flipper_configure | ||
# Create a single shared memory adapter instance for each test | ||
@flipper_adapter = Flipper::Adapters::Memory.new | ||
|
||
Flipper.configure do |config| | ||
config.adapter { @flipper_adapter } | ||
config.default { Flipper.new(config.adapter) } | ||
end | ||
end | ||
|
||
def flipper_reset | ||
Flipper.instance = nil # Reset previous flipper instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got some spec failures after this - you get a new flipper instance but using the same memory adapter, so there's leakage between tests. I can make everything green again if I add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I hit this. The app thread and spec thread end up using a different flipper instance (fine). The app thread is running for the entire duration of the specs so keeps the same flipper instance. On the second spec the before each creates a new flipper instance but this only affects the spec thread, the app thread still has the old memory adapter. To make this work I had to disable the builtin flipper/test_help (with the env var) and then do what I was doing before: create the shared memory adapter once in before(suite) and clear out its contents in before each |
||
end | ||
end | ||
end | ||
|
||
if defined?(RSpec) | ||
RSpec.configure do |config| | ||
config.include Flipper::TestHelp | ||
config.before(:all) { flipper_configure } | ||
config.before(:each) { flipper_reset } | ||
end | ||
end | ||
|
||
if defined?(ActiveSupport) | ||
ActiveSupport.on_load(:active_support_test_case) do | ||
ActiveSupport::TestCase.class_eval do | ||
include Flipper::TestHelp | ||
|
||
setup :flipper_configure | ||
setup :flipper_reset | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require_relative "../helper" | ||
|
||
# Not worth trying to test on old Rails versions | ||
return unless Rails::VERSION::MAJOR >= 7 | ||
|
||
require "capybara/cuprite" | ||
require "flipper" | ||
require "flipper/test_help" | ||
|
||
require 'action_dispatch/system_testing/server' | ||
ActionDispatch::SystemTesting::Server.silence_puma = true | ||
|
||
class TestApp < Rails::Application | ||
config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" | ||
config.eager_load = false | ||
config.logger = ActiveSupport::Logger.new(StringIO.new) | ||
routes.append do | ||
root to: "features#index" | ||
end | ||
end | ||
|
||
TestApp.initialize! | ||
|
||
class FeaturesController < ActionController::Base | ||
def index | ||
render json: Flipper.enabled?(:test) ? "Enabled" : "Disabled" | ||
end | ||
end | ||
|
||
class TestHelpTest < ActionDispatch::SystemTestCase | ||
# Any driver that runs the app in a separate thread will test what we want here. | ||
driven_by :cuprite | ||
|
||
# Ensure this test uses this app instance | ||
setup { Rails.application = TestApp.instance } | ||
|
||
test "configures a shared adapter between tests and app" do | ||
Flipper.disable(:test) | ||
visit "/" | ||
assert_selector "*", text: "Disabled" | ||
|
||
Flipper.enable(:test) | ||
visit "/" | ||
assert_selector "*", text: "Enabled" | ||
end | ||
end |
Uh oh!
There was an error while loading. Please reload this page.