This Week in #dev (Apr 7, 2023)

thoughtbot
Edited by Matheus Richard

Welcome to another edition of This Week in #dev, a series of posts where we bring some of the most interesting Slack conversations to the public. Today we’re talking about eliminating dead code, obsolete methods and mysterious test failures on newest Rails.

Tree Shaking

Sarah Lima learned about the term “Tree Shaking,” which refers to the removal of dead code in JavaScript. The term and concept were popularized by the ES2015 module bundler Rollup.

URI.escape: Obsolete and Removed

Summer ☀️ shares that URI.escape was deprecated in Ruby 1.9.2 but popular gems were still using it. However, ten years later in Ruby 3.0, it was finally removed. This can often cause errors in Ruby 2→3 upgrades.

New Rails, New Errors

If you upgrade to latest Rails, Sean Doyle says, you might see errors similar to these in your tests:

expected to find text "can't be blank" in "Validate\nSubject can’t be blank Content Create Message"

That happens because recent PR in Rails replaced ' with in some default Active Model translation configurations.

What We Recommend

Mike Burns’ advice here is to prevent this errors altogether by using using i18n helpers in tests to avoid asserting on bare strings:

expect(page).to have_text(I18n.t("errors.messages.blank"))

If you’re dealing with model specs, Matheus Richard and Summer ☀️ recommend leveraging ActiveModel::Errors to assert on validation errors:

# Expect a specific type of validation error for an attribute, such as :blank
expect(model.errors).to be_added(:name, :error_type)
expect(model.errors).to be_of_kind(:name, :error_type)

# Expect any validation error for an attribute
expect(model.errors).to include(:name)
expect(model.errors).to have_key(:name)

Thanks

This edition was brought to you by: Matheus Richard, Mike Burns, Sarah Lima, Summer ☀️ and Sean Doyle. Thanks to all contributors! 🎉