From 14e50630fdd2d50f8fdbeeb9951ece6abb11b0f0 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Wed, 10 Jun 2020 14:12:19 -0500 Subject: [PATCH] Flaky Spec Fix: Clear Elasticsearch Data For js: true Specs to Fix Server Errors (#8388) Co-authored-by: Vaidehi Joshi --- docs/tests/readme.md | 1 + docs/tests/test-flags.md | 56 +++++++++++++++++++ .../user_visits_articles_by_tag_spec.rb | 8 +-- .../user_visits_homepage_articles_spec.rb | 4 +- spec/system/user/view_user_comments_spec.rb | 2 +- 5 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 docs/tests/test-flags.md diff --git a/docs/tests/readme.md b/docs/tests/readme.md index ee4e4f4b1..06e20a163 100644 --- a/docs/tests/readme.md +++ b/docs/tests/readme.md @@ -10,6 +10,7 @@ items: - regression-tests.md - code-coverage.md - codeclimate.md + - test-flags.md --- We use the following testing tools: diff --git a/docs/tests/test-flags.md b/docs/tests/test-flags.md new file mode 100644 index 000000000..a2bf3329f --- /dev/null +++ b/docs/tests/test-flags.md @@ -0,0 +1,56 @@ +--- +title: Test Flags +--- + +# Test Flags + +When creating tests using Rspec we have the ability to add flags to those tests +that will signal to Rspec to run certain commands before, after, or around the +test example. + +Some flags that we use are: + +- `js: true` +- `elasticsearch: reset` +- `elasticsearch: ` +- `throttle: true` +- `type: ` + +### Elasticsearch Flags + +Two Elasticsearch flags that can be used when dealing with specs that will be +hitting Elasticsearch. When running a spec that is going to interact with +Elasticsearch, you want Elasticsearch to be clean like your database. Since we +don't have a "database cleaner" for Elasticsearch, we have to do it manually. +There are two ways to do this: + +1. `elasticsearch: reset` - This will trigger a complete tear down and rebuild + of Elasticsearch via a block. This takes time so it's not something you + want to be doing unless you absolutely have to. + +```ruby + config.around(:each, elasticsearch_reset: true) do |example| + Search::Cluster.recreate_indexes + example.run + Search::Cluster.recreate_indexes + end +``` + +2. `elasticsearch: ` - This will clear the data for the given + search class index. For example, if you passed "User" as your search class + name it would clear out the user index data using code from this block. + +```ruby + config.around(:each, :elasticsearch) do |ex| + klasses = Array.wrap(ex.metadata[:elasticsearch]).map do |search_class| + Search.const_get(search_class) + end + klasses.each { |klass| clear_elasticsearch_data(klass) } + ex.run + end +``` + +**NOTE** Any specs that use the `js: true` flag might be hitting Elasticsearch. +You may want to consider clearing out Elasticsearch data for those specs even if +you don't intend on working with Elasticsearch directly to ensure that you have +a clean page load with no unexpected data. diff --git a/spec/system/articles/user_visits_articles_by_tag_spec.rb b/spec/system/articles/user_visits_articles_by_tag_spec.rb index 0325b9657..b8a8e7f7a 100644 --- a/spec/system/articles/user_visits_articles_by_tag_spec.rb +++ b/spec/system/articles/user_visits_articles_by_tag_spec.rb @@ -14,13 +14,13 @@ RSpec.describe "User visits articles by tag", type: :system do context "when 2 articles" do before { visit "/t/javascript" } - it "shows the header", js: true, percy: true do + it "shows the header", js: true, percy: true, elasticsearch: "FeedContent" do Percy.snapshot(page, name: "Tags: logged out user") within("h1") { expect(page).to have_text("javascript") } end - it "shows the follow button", js: true do + it "shows the follow button", js: true, elasticsearch: "FeedContent" do within("h1") { expect(page).to have_button("Follow") } end @@ -44,7 +44,7 @@ RSpec.describe "User visits articles by tag", type: :system do end end - it "when user clicks 'week'", js: true do + it "when user clicks 'week'", js: true, elasticsearch: "FeedContent" do click_on "WEEK" within("#articles-list") do @@ -72,7 +72,7 @@ RSpec.describe "User visits articles by tag", type: :system do visit "/t/functional" end - it "shows the following button", js: true do + it "shows the following button", js: true, elasticsearch: "FeedContent" do # TODO: Add Percy snapshot? wait_for_javascript diff --git a/spec/system/homepage/user_visits_homepage_articles_spec.rb b/spec/system/homepage/user_visits_homepage_articles_spec.rb index 9b1744cc5..e68fb9ce4 100644 --- a/spec/system/homepage/user_visits_homepage_articles_spec.rb +++ b/spec/system/homepage/user_visits_homepage_articles_spec.rb @@ -17,7 +17,7 @@ RSpec.describe "User visits a homepage", type: :system do expect(page).to have_selector(".crayons-story--featured", visible: :visible) end - it "shows the main article readable date", js: true do + it "shows the main article readable date", js: true, elasticsearch: "FeedContent" do expect(page).to have_selector(".crayons-story--featured time", text: "Mar 4") end @@ -40,7 +40,7 @@ RSpec.describe "User visits a homepage", type: :system do expect(page).to have_text(article2.title) end - it "shows all articles dates", js: true do + it "shows all articles dates", js: true, elasticsearch: "FeedContent" do expect(page).to have_selector(".crayons-story time", text: "Mar 4", count: 2) end diff --git a/spec/system/user/view_user_comments_spec.rb b/spec/system/user/view_user_comments_spec.rb index 4bc0ac43f..64d70f93f 100644 --- a/spec/system/user/view_user_comments_spec.rb +++ b/spec/system/user/view_user_comments_spec.rb @@ -15,7 +15,7 @@ RSpec.describe "User comments", type: :system do end end - it "shows user's comments", js: true, percy: true do + it "shows user's comments", js: true, percy: true, elasticsearch: "FeedContent" do Percy.snapshot(page, name: "Comments: /:user_id/comments renders") within("#substories div.index-comments") do