ruby-on-railsminitest

How do you run `bin/rails test` without triggering yarn compilation every time?


If I run tests with bin/rails test it triggers yarn to run twice, once for esbuild and once for css. If I run all the same tests with bin/rails test test/**/**.rb or bin/rails test test/ the tests run instantly.

Why is this happening when I'm not running System Tests)?

Versions: Rails 7.1.1, Ruby 3.2.2, Minitest 5.20.0
App created with rails new my_app --javascript esbuild --css bootstrap


Additional question - How do I figure this out for myself next time?

My test helper doesn't seem to be calling rails test:prepare or anything else that triggers yarn:

# test/test_helper.rb
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"

module ActiveSupport
  class TestCase
    # Run tests in parallel with specified workers
    parallelize(workers: :number_of_processors)

    # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
    fixtures :all

    # Add more helper methods to be used by all tests here...
  end
end

I've tried to go through each required file in test_helper.rb and can't find anywhere else that yarn/assets/precompilation is being told to happen:

I also tried bundle open minitest on the off chance something is configured there but I poked around a bit and couldn't see anything relevant.

Where is the yarn/asset-compilation "magic" configured for tests?


Solution

  • "My test helper doesn't seem to be calling rails test:prepare"

    When you call rails test without any arguments it calls test:prepare for you Source

    run_prepare_task if self.args.none?(EXACT_TEST_ARGUMENT_PATTERN)
    

    and Source

    Rails::Command::RakeCommand.perform("test:prepare", [], {})
    

    As Noted in the Upgrade Guide

    When running tests via bin/rails test, the rake test:prepare task will run before tests run. If you've enhanced the test:prepare task, your enhancements will run before your tests. tailwindcss-rails, jsbundling-rails, and cssbundling-rails enhance this task, as do other third party gems.

    You can see that this indeed builds yarn Cssbundling and Jsbundling