From b1d31a28ef58f009e3800078147257ed1dc2cb24 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 13 Mar 2020 10:09:15 +0100 Subject: [PATCH 1/2] Add system specs configuration Replicate the feature specs configuration and helpers for system specs. --- backend/spec/spec_helper.rb | 25 ++++++++++++--- .../testing_support/authorization_helpers.rb | 1 + frontend/spec/spec_helper.rb | 32 ++++++++++++++----- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/backend/spec/spec_helper.rb b/backend/spec/spec_helper.rb index 1db63f75371..8d900484141 100644 --- a/backend/spec/spec_helper.rb +++ b/backend/spec/spec_helper.rb @@ -59,6 +59,14 @@ ActiveJob::Base.queue_adapter = :test +def check_missing_translations(page, example) + missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) + if missing_translations.any? + puts "Found missing translations: #{missing_translations.inspect}" + puts "In spec: #{example.location}" + end +end + RSpec.configure do |config| config.color = true config.infer_spec_type_from_file_location! @@ -88,6 +96,12 @@ end end + config.when_first_matching_example_defined(type: :system) do + config.before :suite do + Rails.application.precompiled_assets + end + end + config.before do Rails.cache.clear if RSpec.current_example.metadata[:js] && page.driver.browser.respond_to?(:url_blacklist) @@ -96,13 +110,14 @@ end config.include BaseFeatureHelper, type: :feature + config.include BaseFeatureHelper, type: :system config.after(:each, type: :feature) do |example| - missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) - if missing_translations.any? - puts "Found missing translations: #{missing_translations.inspect}" - puts "In spec: #{example.location}" - end + check_missing_translations(page, example) + end + + config.after(:each, type: :system) do |example| + check_missing_translations(page, example) end config.include FactoryBot::Syntax::Methods diff --git a/core/lib/spree/testing_support/authorization_helpers.rb b/core/lib/spree/testing_support/authorization_helpers.rb index abdb207a9f9..48963f15f6c 100644 --- a/core/lib/spree/testing_support/authorization_helpers.rb +++ b/core/lib/spree/testing_support/authorization_helpers.rb @@ -65,4 +65,5 @@ def custom_authorization!(&block) config.extend Spree::TestingSupport::AuthorizationHelpers::Controller, type: :controller config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :feature config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :request + config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :system end diff --git a/frontend/spec/spec_helper.rb b/frontend/spec/spec_helper.rb index cb7a5992e24..cc2ffebcf10 100644 --- a/frontend/spec/spec_helper.rb +++ b/frontend/spec/spec_helper.rb @@ -46,6 +46,20 @@ ActiveJob::Base.queue_adapter = :test +def check_missing_translations(page, example) + missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) + if missing_translations.any? + puts "Found missing translations: #{missing_translations.inspect}" + puts "In spec: #{example.location}" + end +end + +def set_url_blacklist(browser) + if browser.respond_to?(:url_blacklist) + browser.url_blacklist = ['http://fonts.googleapis.com'] + end +end + RSpec.configure do |config| config.color = true config.infer_spec_type_from_file_location! @@ -78,17 +92,19 @@ end config.before(:each, type: :feature) do - if page.driver.browser.respond_to?(:url_blacklist) - page.driver.browser.url_blacklist = ['http://fonts.googleapis.com'] - end + set_url_blacklist(page.driver.browser) + end + + config.before(:each, type: :system) do + set_url_blacklist(page.driver.browser) end config.after(:each, type: :feature) do |example| - missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) - if missing_translations.any? - puts "Found missing translations: #{missing_translations.inspect}" - puts "In spec: #{example.location}" - end + check_missing_translations(page, example) + end + + config.after(:each, type: :system) do |example| + check_missing_translations(page, example) end config.include FactoryBot::Syntax::Methods From 6ef1e7f27dbf1704c9436043f46a35eb942492c0 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 20 Mar 2020 10:26:43 +0100 Subject: [PATCH 2/2] Move the new spec helper methods in testing_support modules --- backend/spec/spec_helper.rb | 34 ++----------------- .../spree/testing_support/blacklist_urls.rb | 23 +++++++++++++ .../testing_support/precompiled_assets.rb | 15 ++++++++ .../lib/spree/testing_support/translations.rb | 25 ++++++++++++++ frontend/spec/spec_helper.rb | 34 +++---------------- 5 files changed, 70 insertions(+), 61 deletions(-) create mode 100644 core/lib/spree/testing_support/blacklist_urls.rb create mode 100644 core/lib/spree/testing_support/precompiled_assets.rb create mode 100644 core/lib/spree/testing_support/translations.rb diff --git a/backend/spec/spec_helper.rb b/backend/spec/spec_helper.rb index 8d900484141..1c33a09356f 100644 --- a/backend/spec/spec_helper.rb +++ b/backend/spec/spec_helper.rb @@ -36,6 +36,8 @@ require 'spree/testing_support/url_helpers' require 'spree/testing_support/order_walkthrough' require 'spree/testing_support/capybara_ext' +require 'spree/testing_support/precompiled_assets' +require 'spree/testing_support/translations' require 'capybara-screenshot/rspec' Capybara.save_path = ENV['CIRCLE_ARTIFACTS'] if ENV['CIRCLE_ARTIFACTS'] @@ -59,14 +61,6 @@ ActiveJob::Base.queue_adapter = :test -def check_missing_translations(page, example) - missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) - if missing_translations.any? - puts "Found missing translations: #{missing_translations.inspect}" - puts "In spec: #{example.location}" - end -end - RSpec.configure do |config| config.color = true config.infer_spec_type_from_file_location! @@ -87,21 +81,6 @@ def check_missing_translations(page, example) DatabaseCleaner.clean_with :truncation end - config.when_first_matching_example_defined(type: :feature) do - config.before :suite do - # Preload assets - # This should avoid capybara timeouts, and avoid counting asset compilation - # towards the timing of the first feature spec. - Rails.application.precompiled_assets - end - end - - config.when_first_matching_example_defined(type: :system) do - config.before :suite do - Rails.application.precompiled_assets - end - end - config.before do Rails.cache.clear if RSpec.current_example.metadata[:js] && page.driver.browser.respond_to?(:url_blacklist) @@ -112,14 +91,6 @@ def check_missing_translations(page, example) config.include BaseFeatureHelper, type: :feature config.include BaseFeatureHelper, type: :system - config.after(:each, type: :feature) do |example| - check_missing_translations(page, example) - end - - config.after(:each, type: :system) do |example| - check_missing_translations(page, example) - end - config.include FactoryBot::Syntax::Methods config.include ActiveSupport::Testing::Assertions config.include ActiveJob::TestHelper @@ -128,6 +99,7 @@ def check_missing_translations(page, example) config.include Spree::TestingSupport::UrlHelpers config.include Spree::TestingSupport::ControllerRequests, type: :controller config.include Spree::TestingSupport::Flash + config.include Spree::TestingSupport::Translations config.extend WithModel diff --git a/core/lib/spree/testing_support/blacklist_urls.rb b/core/lib/spree/testing_support/blacklist_urls.rb new file mode 100644 index 00000000000..faacf8ec06d --- /dev/null +++ b/core/lib/spree/testing_support/blacklist_urls.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Spree + module TestingSupport + module BlacklistUrls + def setup_url_blacklist(browser) + if browser.respond_to?(:url_blacklist) + browser.url_blacklist = ['http://fonts.googleapis.com'] + end + end + end + end +end + +RSpec.configure do |config| + config.before(:each, type: :feature) do + setup_url_blacklist(page.driver.browser) + end + + config.before(:each, type: :system) do + setup_url_blacklist(page.driver.browser) + end +end diff --git a/core/lib/spree/testing_support/precompiled_assets.rb b/core/lib/spree/testing_support/precompiled_assets.rb new file mode 100644 index 00000000000..a647a580de9 --- /dev/null +++ b/core/lib/spree/testing_support/precompiled_assets.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + config.when_first_matching_example_defined(type: :feature) do + config.before :suite do + Rails.application.precompiled_assets + end + end + + config.when_first_matching_example_defined(type: :system) do + config.before :suite do + Rails.application.precompiled_assets + end + end +end diff --git a/core/lib/spree/testing_support/translations.rb b/core/lib/spree/testing_support/translations.rb new file mode 100644 index 00000000000..77e7c7824ed --- /dev/null +++ b/core/lib/spree/testing_support/translations.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Spree + module TestingSupport + module Translations + def check_missing_translations(page, example) + missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) + if missing_translations.any? + puts "Found missing translations: #{missing_translations.inspect}" + puts "In spec: #{example.location}" + end + end + end + end +end + +RSpec.configure do |config| + config.after(:each, type: :feature) do |example| + check_missing_translations(page, example) + end + + config.after(:each, type: :system) do |example| + check_missing_translations(page, example) + end +end diff --git a/frontend/spec/spec_helper.rb b/frontend/spec/spec_helper.rb index cc2ffebcf10..b9fabb652b3 100644 --- a/frontend/spec/spec_helper.rb +++ b/frontend/spec/spec_helper.rb @@ -36,6 +36,8 @@ require 'spree/testing_support/url_helpers' require 'spree/testing_support/order_walkthrough' require 'spree/testing_support/caching' +require 'spree/testing_support/blacklist_urls' +require 'spree/testing_support/translations' require 'capybara-screenshot/rspec' Capybara.save_path = ENV['CIRCLE_ARTIFACTS'] if ENV['CIRCLE_ARTIFACTS'] @@ -46,20 +48,6 @@ ActiveJob::Base.queue_adapter = :test -def check_missing_translations(page, example) - missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/) - if missing_translations.any? - puts "Found missing translations: #{missing_translations.inspect}" - puts "In spec: #{example.location}" - end -end - -def set_url_blacklist(browser) - if browser.respond_to?(:url_blacklist) - browser.url_blacklist = ['http://fonts.googleapis.com'] - end -end - RSpec.configure do |config| config.color = true config.infer_spec_type_from_file_location! @@ -91,28 +79,14 @@ def set_url_blacklist(browser) Rails.cache.clear end - config.before(:each, type: :feature) do - set_url_blacklist(page.driver.browser) - end - - config.before(:each, type: :system) do - set_url_blacklist(page.driver.browser) - end - - config.after(:each, type: :feature) do |example| - check_missing_translations(page, example) - end - - config.after(:each, type: :system) do |example| - check_missing_translations(page, example) - end - config.include FactoryBot::Syntax::Methods config.include Spree::TestingSupport::Preferences config.include Spree::TestingSupport::UrlHelpers config.include Spree::TestingSupport::ControllerRequests, type: :controller config.include Spree::TestingSupport::Flash + config.include Spree::TestingSupport::BlacklistUrls + config.include Spree::TestingSupport::Translations config.example_status_persistence_file_path = "./spec/examples.txt"