Flaky Spec Fix: Clear Elasticsearch Data For js: true Specs to Fix Server Errors (#8388)

Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
This commit is contained in:
Molly Struve 2020-06-10 14:12:19 -05:00 committed by GitHub
parent 54afa3ba91
commit 14e50630fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 7 deletions

View file

@ -10,6 +10,7 @@ items:
- regression-tests.md
- code-coverage.md
- codeclimate.md
- test-flags.md
---
We use the following testing tools:

56
docs/tests/test-flags.md Normal file
View file

@ -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: <search class name>`
- `throttle: true`
- `type: <test 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: <search class name>` - 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.

View file

@ -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

View file

@ -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

View file

@ -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