class RailsJsdom::Generators::InstallGenerator
Sets an app up for rails-jsdom interaction tests:
bin/rails generate rails_jsdom:install
Pass --skip-npm to add the Node packages to package.json without running npm install.
Constants
- PACKAGES
-
The Node packages the app needs. Each range allows newer minor and patch releases but never a new major version.
- RELEASE_AGE_DAYS
-
How many days old a package version must be before npm installs it.
- RELEASE_AGE_NPM
-
The first npm version that understands
min-release-age.
Public Instance Methods
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 41 def add_node_packages package = existing_package || { "private" => true } package["devDependencies"] = PACKAGES.merge(package.fetch("devDependencies", {})) create_file "package.json", "#{JSON.pretty_generate(package)}\n", force: true end
Adds jsdom and esbuild to the development dependencies in package.json, creating the file if the app has none. Versions the app already lists are kept.
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 75 def check_cable_adapter say_status :warning, "Set `adapter: test` for the test environment in config/cable.yml so pages receive Turbo Stream broadcasts", :yellow unless test_cable_adapter? end
Warns when the test environment does not use the ActionCable test adapter, which Turbo Stream broadcasts need.
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 58 def check_npm_version say_status :warning, "npm #{npm_version} ignores min-release-age, so it would install brand-new releases. Upgrade to npm #{RELEASE_AGE_NPM} or later.", :yellow if npm_version < RELEASE_AGE_NPM end
Warns when the app’s npm is older than 11.10, which ignores min-release-age.
# File lib/generators/rails_jsdom/install/install_generator.rb, line 34 def create_interactions_directory create_file "test/interactions/.keep", "" end
Creates test/interactions, where interaction tests live.
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 51 def delay_new_releases settings = npm_settings create_file ".npmrc", "#{settings}min-release-age=#{RELEASE_AGE_DAYS}\n", force: true unless settings.match?(/^min-release-age\s*=/) end
Sets min-release-age in .npmrc, so npm installs only package versions published at least RELEASE_AGE_DAYS days ago. A compromised release of jsdom, esbuild or anything they depend on is usually found and removed within that time. An existing min-release-age is kept.
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 68 def ignore_node_modules gitignore = path(".gitignore") append_to_file ".gitignore", "\n/node_modules\n" if File.exist?(gitignore) && !File.read(gitignore).include?("node_modules") end
Adds /node_modules to .gitignore, unless it is already ignored.
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 63 def install_node_packages run "npm install" unless options.skip_npm? end
Runs npm install, unless --skip-npm is given.
Source
# File lib/generators/rails_jsdom/install/install_generator.rb, line 29 def require_library inject_into_file "test/test_helper.rb", "require \"rails_jsdom\"\n", after: %r{require ["']rails/test_help["']\n} end
Requires the library in test/test_helper.rb, after rails/test_help.