Testing your Jekyll Website with Capybara
Even when it is just a static website like this one, it is always good to know that the most important things work.
I recently had problems when I was migrating this blog from Jekyll 2.5 to Jekyll 3.3. The permalinks were broken after the upgrade and I only noticed this after the change went live. Some basic tests could have easily prevented this.
After I fixed the broken permalinks I decided to add some basic Capybara tests, as I write them day-to-day for Ruby on Rails apps, too.
The setup is pretty basic for now with
rake and most importantly
gem 'jekyll', '3.3.1'
group :test do
Capybara.app to use
Rack::Jekyll to serve a Rack version of our Jekyll site for our tests.
Capybara.app = Rack::Jekyll.new(force_build: true)
class CapybaraTestCase < MiniTest::Test
test_helper configured we can now start writing Capybara tests as usual:
class ThisBlogPostTest < CapybaraTestCase
assert page.has_content?('configured we can now start '\
'writing Capybara tests as usual:')
What to test
This of course depends on your website. For me I identified a few things:
- The frontpage (which is a bit outdated) should have the link to my CV
- Clicking on the archive link should bring me to the archive
- /archive should contain the first and last post
- The dates on /archive should be in the correct format
- The 'Read more' link on /writing should bring me to the correct blog post
- A blog post should have the correct time to read
- A blog post should have its date in the correct format
- The main navigation items go to pages with the correct content
- The atom feed should be a valid atom feed
Apart from the atom feed validation this is all straight Capybara using
has_content and some basic Ruby for the time check. You can find all the tests here.