How to test Zeitwerk?

I just had production crash where after deploy, my app simply stopped working.

tracking down the logs led me to this error:

‘App 3531 output: Error: The application encountered the following error: expected file /home/deploy/Skydive-jumpstart/releases/20210322114743/app/dashboards/tutorials_dashboard.rb to define constant TutorialsDashboard, but didn’t (Zeitwerk::NameError)’

which was easy to fix.

The problem is - all my tests passed before I deployed.

I get (and appreciate) Rails being ‘kinder’ during development - but it shouldn’t be hiding bugs that make the entire app fall over in my testing

How do I write a test along the lines of

describe "Zeitwerk" do
  it "manages to load classes without falling over"
end

and why isn’t something like this in the default testing setup?

Yes, you can run bin/rails zeitwerk:check to see if all is good. Some people add that to their CI actually.

However, which was the problem? Did that dashboard have test coverage?

thanks for that.

and nope - the dashboard didn’t have coverage. It was an accidentally created blank file. No code at all in it - so zeitwerk couldn’t find the expected class.

and by the power of stack overflow, here is my new rspec test:

require 'rails_helper'
require 'rake'

RSpec.describe "zeitwerk", type: :system do

  describe "loads classes" do
    it "doesn't fall over" do
    	Rails.application.load_tasks
    	Rake::Task['zeitwerk:check'].invoke
    end
  end

end
3 Likes