Back to Repositories

Testing CSS Unit Stripping Functionality in Bourbon

This test suite validates the strip-unit functionality in Bourbon’s library, ensuring proper handling of CSS unit stripping across different measurement types. The tests verify the library’s ability to remove units from pixel, em, rem, and percentage values while preserving the numeric portions.

Test Coverage Overview

The test suite provides comprehensive coverage of the strip-unit function across multiple CSS measurement units.

Key areas tested include:
  • Pixel (px) unit stripping
  • Em unit handling
  • Rem unit processing
  • Percentage value conversion
Edge cases are addressed through distinct contexts for each unit type, ensuring reliable unit stripping regardless of input format.

Implementation Analysis

The testing approach utilizes RSpec’s context-based structure to organize related test cases logically. The implementation leverages custom matchers (have_rule) to verify CSS rule generation, with each test focusing on a specific unit type.

The pattern demonstrates effective use of RSpec’s before(:all) hook for test setup and shared parser support functionality.

Technical Details

Testing tools and configuration:
  • RSpec testing framework
  • Custom ParserSupport module for file parsing
  • Specialized matchers for CSS rule validation
  • Shared spec helper configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases, clear context separation, and consistent structure across different unit types.

Notable practices include:
  • Descriptive context blocks for each unit type
  • Consistent test case naming
  • Efficient test setup using before blocks
  • Clear expected value assertions

thoughtbot/bourbon

spec/bourbon/library/strip_unit_spec.rb

            
require "spec_helper"

describe "strip-unit" do
  before(:all) do
    ParserSupport.parse_file("library/strip-unit")
  end

  context "called with px" do
    it "strips units" do
      expect(".px").to have_rule("width: 10")
    end
  end

  context "called with em" do
    it "strips units" do
      expect(".em").to have_rule("width: 2")
    end
  end

  context "called with rem" do
    it "strips units" do
      expect(".rem").to have_rule("width: 1.5")
    end
  end

  context "called with percent" do
    it "strips units" do
      expect(".percent").to have_rule("width: 20")
    end
  end
end