Skip to content

A platform for community discussion. Free, open, simple.

DEV: Refactor all js testing into bin/qunit (#35785)

Sam89d9ac195bdb
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index d9518683082..7be7ae4c83b 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -42,6 +42,7 @@ jobs:
TESTEM_FIREFOX_PATH: ${{ (matrix.browser == 'Firefox Evergreen') && '/opt/firefox-evergreen/firefox' }}
EMBER_ENV: development
LOAD_PLUGINS: ${{ (matrix.target == 'plugins' || matrix.target == 'chat') && '1' || '0' }}
+ ACTIONS_STEP_DEBUG: ${{ secrets.ACTIONS_STEP_DEBUG }}
strategy:
fail-fast: false
diff --git a/AI-AGENTS.md b/AI-AGENTS.md
index 72e7d1fc033..d8caa3d4e9e 100644
--- a/AI-AGENTS.md
+++ b/AI-AGENTS.md
@@ -48,25 +48,10 @@ Discourse is large with long history. Understand context before changes.
bin/rspec [spec/path/file_spec.rb[:123]]
LOAD_PLUGINS=1 bin/rspec # Plugin tests
-# JavaScript tests
-bin/rake qunit:test # RUN all non plugin tests
-LOAD_PLUGINS=1 TARGET=all FILTER='fill filter here' bin/rake qunit:test # RUN specific tests based on filter
-
-Exmaple filters JavaScript tests:
-
- emoji-test.js
- ...
- acceptance("Emoji" ..
- test("cooked correctly")
- ...
- Filter string is: "Acceptance: Emoji: cooked correctly"
-
- user-test.js
- ...
- module("Unit | Model | user" ..
- test("staff")
- ...
- Filter string is: "Unit | Model | user: staff"
+# JavaScript tests - bin/qunit
+bin/qunit --help # detailed help
+bin/qunit path/to/test-file.js # Run all tests in file
+bin/qunit path/to/tests/directory # Run all tests in directory
# Linting
bin/lint path/to/file path/to/another/file
diff --git a/bin/qunit b/bin/qunit
new file mode 100755
index 00000000000..ccd210c9226
--- /dev/null
+++ b/bin/qunit
@@ -0,0 +1,626 @@
+#!/usr/bin/env ruby
+# frozen_string_literal: true
+require "optparse"
+require "shellwords"
+require "json"
+require "fileutils"
+require "socket"
+require "uri"
+require "net/http"
+require_relative "../lib/chrome_installed_checker"
+
+class QunitRunner
+ def initialize(args)
+ @file_paths = []
+ @verbose = ENV["ACTIONS_STEP_DEBUG"] == "true"
+ @dry_run = false
+ @rails_port = ENV["UNICORN_PORT"]&.to_i || 3000
+
+ @standalone_mode = false
+
+ @parallel = ENV["QUNIT_PARALLEL"]
+ @seed = ENV["QUNIT_SEED"]
+ @theme_name = ENV["THEME_NAME"]
+ @theme_url = ENV["THEME_URL"]
+ @theme_id = ENV["THEME_ID"]
+ @qunit_path = nil
+ @rails_env = ENV["QUNIT_RAILS_ENV"] || "test"
+ @plugin_targets = []
+
+ @headless = true
+
+ parse_options(args)
+ end
+
+ def run
+ if @standalone_mode
+ with_standalone_rails { run_tests }
+ else
+ run_tests
+ end
+ end
+
+ private
+
+ def parse_options(args)
+ parser =
+ OptionParser.new do |opts|
+ opts.banner = <<~BANNER
+ Usage: bin/qunit [options] [files|directories]
+
+ Runs the Discourse QUnit suite. By default, tests will be run against the running Rails server and existing JS assets.
+ Add --standalone to spin up an isolated Rails server and run a standalone asset build.
+ BANNER
+
+ opts.separator ""
+ opts.separator "Options:"
+
+ opts.on("-f", "--filter FILTER", "Run tests matching this filter") do |filter|
+ @filter = filter
+ end
+
+ opts.on("--dry-run", "Show what would run without executing") { @dry_run = true }
+
+ opts.on(
+ "--rails-port PORT",
+ Integer,
+ "Rails server port (default: 3000) (env: UNICORN_PORT)",
+ ) { |port| @rails_port = port }
+
+ opts.on("--standalone", "Use standalone mode (spins up own test server)") do
+ @standalone_mode = true
+ end
+
+ opts.on(
+ "--parallel N",
+ Integer,
+ "Run tests in parallel (N workers) (env: QUNIT_PARALLEL)",
+ ) { |n| @parallel = n.to_s }
+
+ opts.on("--seed SEED", "Random seed for test order (env: QUNIT_SEED)") do |seed|
+ @seed = seed
+ end
+
+ opts.on(
+ "--target TARGET",
+ "Target: 'core', 'plugins' (all plugins), or comma separated plugin names",
+ ) { |target| @target = target }
+
+ opts.on("--module MODULE", "Run specific module") { |mod| @module = mod }
+
+ opts.on("--theme-name NAME", "Theme name for theme tests (env: THEME_NAME)") do |name|
+ @theme_name = name
+ end
+ opts.on("--theme-url URL", "Theme URL for theme tests (env: THEME_URL)") do
+ @theme_url = url
+ end
+ opts.on("--theme-id ID", "Theme ID for theme tests (env: THEME_ID)") { |id| @theme_id = id }
+ opts.on("--qunit-path PATH", "QUnit path (e.g., /theme-qunit)") do |path|
+ @qunit_path = path
+ end
+
+ opts.on("--rails-env ENV", "Rails environment (default: test) (env: RAILS_ENV)") do |env|
+ @rails_env = env
+ end
+
+ opts.on("--report-requests", "Report HTTP requests during tests") do
+ @report_requests = true
+ end
+
+ opts.on("--[no-]headless", "Run tests in headless mode (env: QUNIT_HEADLESS)") do |h|
+ @headless = h
+ end
+
+ opts.on("-v", "--verbose", "Verbose output") { @verbose = true }
+
+ opts.on("-h", "--help", "Show this help message") do
+ puts opts
+ exit 0
+ end
+
+ opts.separator <<~NOTES
+ \nExamples:
+ bin/qunit frontend/discourse/tests/unit/models/user-test.js
+ bin/qunit --standalone --target plugins # Run all plugin tests
+ bin/qunit --standalone --target chat # Run single plugin tests
+ bin/qunit --standalone plugins/chat/test/javascripts/unit/lib/chat-audio-test.js
+ bin/qunit --standalone --filter "Acceptance: Emoji"
+ NOTES
+ end
+
+ begin
+ parser.parse!(args)
+ rescue OptionParser::InvalidOption => e
+ warn "Error: #{e.message}"
+ puts ""
+ puts parser
+ exit 1
+ end
+
+ ensure_supported_target!
+
+ @file_paths = args
+
+ detect_plugin_targets!
+ expand_directory_paths!
+ end
+
+ def detect_plugin_targets!
+ if resolved_target.include?(",")
+ @plugin_targets = resolved_target.split(",").map(&:strip)
+ puts "Running tests for #{@plugin_targets.join(", ")}" if @verbose
+ elsif resolved_target == "plugins"
+ @plugin_targets = find_all_plugins_with_tests
+ puts "Discovered #{@plugin_targets.length} plugins with tests" if @verbose
+ end
+ end
+
+ def find_all_plugins_with_tests
+ plugins_dir = File.expand_path("../plugins", __dir__)
+ return [] unless Dir.exist?(plugins_dir)
+
+ Dir
+ .children(plugins_dir)
+ .sort
+ .select do |plugin_dir_name|
+ plugin_path = File.join(plugins_dir, plugin_dir_name)
+ next false unless File.directory?(plugin_path)
+
+ # Check if plugin has tests
+ test_dirs = %w[test/javascripts test].map { |subdir| File.join(plugin_path, subdir) }
+ test_dirs.any? do |test_dir|
+ Dir.exist?(test_dir) && Dir.glob(File.join(test_dir, "**", "*-test.{js,gjs}")).any?
+ end
+ end
+ .map { |plugin_dir_name| resolve_plugin_namespace(plugin_dir_name) }
+ end
+
+ def expand_directory_paths!
+ expanded = []
+
+ @file_paths.each do |path|
+ if File.directory?(path)
+ test_files = Dir.glob(File.join(path, "**", "*-test.{js,gjs}"))
+ if test_files.empty?
+ puts "Warning: No test files found in directory: #{path}" if @verbose
+ else
+ expanded.concat(test_files)
+ end
+ elsif File.file?(path)
+ expanded << path
+ else
+ puts "Warning: Path not found: #{path}"
+ end
+ end
+
+ @file_paths = expanded.uniq
+ end
+
+ def run_tests
+ ensure_file_paths_exist!
+
+ if @file_paths && @verbose
+ puts "Files to test: #{@file_paths.length}"
+ puts " #{@file_paths.join("\n ")}"
+ end
+
+ target = resolved_target
+ puts "Target: #{target}" if @verbose
+
+ file_path_filter = build_file_path_filter(@file_paths)
+
+ run_ember_exam(file_path_filter: file_path_filter, filter: @filter)
+ end
+
+ def run_ember_exam(filter: nil, file_path_filter: nil)
+ check_dev_environment!
+
+ puts "\nRunning tests against Rails on localhost:#{@rails_port}"
+ puts " Using existing build in frontend/discourse/dist" unless @standalone_mode
+
+ if @plugin_targets.any?
+ puts " Plugins: #{@plugin_targets.join(", ")}"
+ elsif file_path_filter
+ puts " Filtered by file path" if file_path_filter
+ end
+ puts " Filter: #{filter}" if filter
+ puts " Load plugins: #{should_load_plugins?}"
+ puts ""
+
+ dir = frontend_dir
+
+ unless Dir.exist?(dir)
+ puts "Error: frontend/discourse directory not found at #{dir}"
+ exit 1
+ end
+
+ env = {}
+ env["LOAD_PLUGINS"] = should_load_plugins? ? "1" : "0"
+ env["REPORT_REQUESTS"] = "1" if @report_requests
+ env["UNICORN_PORT"] = @rails_port.to_s
+ env["TESTEM_DEFAULT_BROWSER"] = ENV["TESTEM_DEFAULT_BROWSER"] || detect_browser
+ env["PLUGIN_TARGETS"] = @plugin_targets.join(",") if @plugin_targets.any?
+ env["QUNIT_HEADLESS"] = "0" if !@headless
+
+ parallel = !@plugin_targets.any? && present_string(@parallel)
+ reuse_build = ENV["QUNIT_REUSE_BUILD"] == "1" || !@standalone_mode
+
+ query = build_query
+
+ args = []
+
+ if @qunit_path
+ run_theme_build(unless_build_reused: reuse_build)
+ env["THEME_TEST_PAGES"] = build_theme_test_pages(query)
+ args += %w[pnpm testem ci -f testem.js]
+ args += ["--parallel", parallel.to_s] if parallel
+ else
+ args = %w[pnpm ember exam]
+ args += ["--query", query] if query && !query.empty?
+ args += ["--filter", filter] if filter
+ args += ["--file-path", file_path_filter] if file_path_filter
+ args += ["--load-balance", "--parallel", parallel.to_s] if parallel
+ args += %w[--path dist] if reuse_build
+ args << "--write-execution-file" if ENV["QUNIT_WRITE_EXECUTION_FILE"]
+ end
+
+ puts <<~MESSAGE if @verbose || @dry_run
+ Executing: #{JSON.pretty_generate(args)}
+ with env: #{JSON.pretty_generate(env)}
+ MESSAGE
+
+ if @dry_run
+ puts "[dry-run] tests not executed"
+ exit 0
+ end
+
+ system(env, *args, chdir: dir)
+ exit $?.exitstatus
+ end
+
+ def convert_to_ember_relative_path(file_path)
+ path = File.expand_path(file_path)
+
+ if path.match?(%r{(?:^|/)plugins/})
+ relative = path.split("/plugins/").last
+ plugin_dir, remainder = relative.split("/", 2)
+ plugin_dir ||= relative
+ namespaced_plugin = resolve_plugin_namespace(plugin_dir)
+ remainder_path = normalize_plugin_test_path(remainder)
+
+ parts = ["discourse", "plugins", namespaced_plugin]
+ parts << remainder_path unless remainder_path.empty?
+ return parts.join("/")
+ end
+
+ if path.include?("/frontend/discourse/tests/")
+ return path.split("/frontend/discourse/").last
+ elsif path.include?("/tests/")
+ return path.split("/tests/").last.prepend("tests/")
+ end
+
+ parts = path.split("/")
+ if idx = parts.index("tests")
+ return parts[idx..-1].join("/")
+ end
+
+ file_path
+ end
+
+ def ensure_file_paths_exist!
+ @file_paths.each do |file_path|
+ next if File.exist?(file_path)
+
+ puts "Error: File not found: #{file_path}"
+ exit 1
+ end
+ end
+
+ def check_dev_environment!
+ if @dry_run
+ puts "[dry-run] skipping dev environment check"
+ return
+ end
+
+ rails_ok = check_server(@rails_port, "Rails")
+
+ unless rails_ok
+ puts "\n? Dev environment not ready!\n"
+ puts "Expected Rails to be running on #{@rails_port}, but it is not responding."
+ puts
+ puts "Start with: bin/rails server, or use --standalone mode to spin up automatically"
+ puts ""
+ exit 1
+ end
+ end
+
+ def check_server(port, name)
+ uri = URI("http://localhost:#{port}")
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.open_timeout = 2
+ http.read_timeout = 2
+
+ begin
+ response = http.get("/")
+ if response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
+ puts "? #{name} server responding on :#{port}" if @verbose
+ true
+ else
+ puts "? #{name} server on :#{port} returned #{response.code}"
+ false
+ end
+ rescue Errno::ECONNREFUSED
+ puts "? #{name} server not responding on :#{port} (connection refused)"
+ false
+ rescue => e
+ puts "? #{name} server on :#{port}: #{e.message}" if @verbose
+ false
+ end
+ end
+
+ def with_standalone_rails(&block)
+ puts "Running in standalone mode (spinning up test server)..." if @verbose
+
+ ensure_pnpm_installed!
+ install_node_dependencies!
+
+ @rails_port = next_available_port(60_098)
+ unicorn_env = build_unicorn_environment(@rails_port)
+ unicorn_pid = nil
+
+ begin
+ if @dry_run
+ puts "[dry-run] skipping unicorn startup"
+ else
+ unicorn_pid =
+ Process.spawn(unicorn_env, File.join(rails_root, "bin", "unicorn"), pgroup: true)
+ end
+ wait_for_rails_server(@rails_port)
+ yield
+ ensure
+ stop_unicorn(unicorn_pid, unicorn_env["UNICORN_PID_PATH"]) if unicorn_pid
+ end
+ end
+
+ def ensure_pnpm_installed!
+ return if system("command -v pnpm >/dev/null")
+ abort "pnpm is not installed. See https://pnpm.io/installation"
+ end
+
+ def install_node_dependencies!
+ if @dry_run
+ puts "[dry-run] skipping pnpm install"
+ return
+ end
+ system("pnpm", "install", exception: true)
+ end
+
+ def detect_browser
+ ChromeInstalledChecker.run
+ rescue ChromeInstalledChecker::ChromeError => err
+ abort err.message
+ end
+
+ def next_available_port(start_port)
+ port = (ENV["TEST_SERVER_PORT"] || start_port).to_i
+ port += 1 while !port_available?(port)
+ port
+ end
+
+ def port_available?(port)
+ server = TCPServer.open(port)
+ server.close
+ true
+ rescue Errno::EADDRINUSE
+ false
+ end
+
+ def build_unicorn_environment(unicorn_port)
+ pid_path = File.join(rails_root, "tmp/pids", "unicorn_test_#{unicorn_port}.pid")
+ {
+ "RAILS_ENV" => @rails_env || "test",
+ "SKIP_ENFORCE_HOSTNAME" => "1",
+ "UNICORN_PID_PATH" => pid_path,
+ "UNICORN_PORT" => unicorn_port.to_s,
+ "UNICORN_SIDEKIQS" => "0",
+ "DISCOURSE_SKIP_CSS_WATCHER" => "1",
+ "UNICORN_LISTENER" => "127.0.0.1:#{unicorn_port}",
+ "LOGSTASH_UNICORN_URI" => nil,
+ "UNICORN_WORKERS" => "1",
+ "UNICORN_TIMEOUT" => "90",
+ "LOAD_PLUGINS" => should_load_plugins? ? "1" : "0",
+ }
+ end
+
+ def wait_for_rails_server(port)
+ if @dry_run
+ puts "[dry-run] skipping Rails server warm-up"
+ return
+ end
+ uri = URI("http://localhost:#{port}/srv/status")
+ puts "Warming up Rails server"
+
+ deadline = Time.now + 60
+ begin
+ Net::HTTP.get(uri)
+ rescue Errno::ECONNREFUSED,
+ Errno::EADDRNOTAVAIL,
+ Net::ReadTimeout,
+ Net::HTTPBadResponse,
+ EOFError
+ if Time.now <= deadline
+ sleep 1
+ retry
+ end
+
+ puts "Timed out. Cannot connect to forked server!"
+ exit 1
+ end
+
+ puts "Rails server is warmed up"
+ end
+
+ def run_theme_build(unless_build_reused: false)
+ if @dry_run
+ puts "[dry-run] skipping ember build for themes"
+ return
+ end
+
+ return if unless_build_reused
+
+ system("pnpm", "ember", "build", chdir: frontend_dir, exception: true)
+ end
+
+ def build_theme_test_pages(query)
+ pages =
+ if ENV["THEME_IDS"] && !ENV["THEME_IDS"].empty?
+ ENV["THEME_IDS"]
+ .split("|")
+ .map { |theme_id| "#{@qunit_path}?#{query}&testem=1&id=#{theme_id}" }
+ else
+ ["#{@qunit_path}?#{query}&testem=1"]
+ end
+
+ pages.shuffle.join(",")
+ end
+
+ def build_file_path_filter(paths)
+ return nil if paths.empty?
+
+ relative_paths = paths.map { |path| convert_to_ember_relative_path(path) }
+ relative_paths.join(",")
+ end
+
+ def build_query
+ params = {}
+ params["seed"] = resolved_seed
+ params["module"] = @module if @module
+ params["theme_name"] = @theme_name if @theme_name
+ params["theme_url"] = @theme_url if @theme_url
+ params["theme_id"] = @theme_id if @theme_id
+ params["target"] = resolved_target if resolved_target && !@plugin_targets.any?
+ params["report_requests"] = "1" if @report_requests
+
+ encode_query_string(params)
+ end
+
+ def stop_unicorn(pid, pid_path)
+ if @dry_run
+ puts "[dry-run] skipping unicorn shutdown"
+ return
+ end
+ Process.kill("-KILL", pid)
+ rescue Errno::ESRCH
+ ensure
+ FileUtils.rm_f(pid_path) if pid_path
+ end
+
+ def rails_root
+ @rails_root ||= File.expand_path("..", __dir__)
+ end
+
+ def frontend_dir
+ @frontend_dir ||= File.expand_path("../frontend/discourse", __dir__)
+ end
+
+ def resolved_target
+ @resolved_target ||=
+ begin
+ implicit_target = (target_from_paths(@file_paths) if @file_paths.any?)
+
+ if @target && implicit_target && @target != implicit_target
+ puts "Files are for '#{implicit_target}', but target was configured as '#{@target}'"
+ exit 1
+ end
+
+ implicit_target || @target || "core"
+ end
+ end
+
+ def resolved_seed
+ @resolved_seed ||= present_string(@seed) || Random.new.seed
+ end
+
+ def should_load_plugins?
+ resolved_target != "core"
+ end
+
+ def target_from_paths(paths)
+ targets = paths.map { |path| target_from_path(path) }.uniq
+ if targets.length > 1
+ puts "Error: Cannot mix multiple plugin/core targets when running specific files"
+ exit 1
+ end
+ targets.first
+ end
+
+ def target_from_path(path)
+ match = path.match(%r{(?:^|/)plugins/([^/]+)/})
+ return "core" unless match
+
+ resolve_plugin_namespace(match[1])
+ end
+
+ def plugin_path?(path)
+ path.match?(%r{(?:^|/)plugins/})
+ end
+
+ def encode_query_string(params = {})
+ cleaned = params.compact
+ return nil if cleaned.empty?
+
+ URI.encode_www_form(cleaned)
+ end
+
+ def present_string(value)
+ return nil if value.nil?
+
+ str = value.to_s
+ str.empty? ? nil : str
+ end
+
+ def resolve_plugin_namespace(directory_name)
+ @plugin_namespace_cache ||= {}
+ return @plugin_namespace_cache[directory_name] if @plugin_namespace_cache.key?(directory_name)
+
+ plugin_rb = File.expand_path("../plugins/#{directory_name}/plugin.rb", __dir__)
+
+ if File.exist?(plugin_rb)
+ File.foreach(plugin_rb) do |line|
+ next unless line.start_with?("#")
+ attribute, value = line[1..].split(":", 2)
+ next unless attribute&.strip == "name"
+
+ plugin_name = value&.strip
+ if plugin_name && !plugin_name.empty?
+ @plugin_namespace_cache[directory_name] = plugin_name
+ return plugin_name
+ end
+ end
+ end
+
+ @plugin_namespace_cache[directory_name] = directory_name
+ end
+
+ def normalize_plugin_test_path(path)
+ path.sub(%r{\Atests?/javascripts/}, "")
+ end
+
+ def plugin_names_from_filter(filter)
+ return [] unless filter
+
+ filter.scan(%r{plugins/([^/]+)/}).flatten.uniq.map { |dir| resolve_plugin_namespace(dir) }
+ end
+
+ def ensure_supported_target!
+ value = present_string(@target)
+ return unless value
+
+ normalized = value.downcase
+ if normalized == "all"
+ abort "Error: Target 'all' is no longer supported. Use 'core' or 'plugins'."
+ end
+ end
+end
+
+QunitRunner.new(ARGV).run
diff --git a/frontend/discourse/testem.js b/frontend/discourse/testem.js
index 86bc8748a8d..a5493b432a6 100644
--- a/frontend/discourse/testem.js
+++ b/frontend/discourse/testem.js
@@ -152,7 +152,7 @@ module.exports = {
Chromium: [
// --no-sandbox is needed when running Chromium inside a container or when explicitly requested
sandboxDisabled ? "--no-sandbox" : null,
- "--headless=new",
+ process.env.QUNIT_HEADLESS === "0" ? null : "--headless=new",
"--disable-dev-shm-usage",
"--disable-software-rasterizer",
"--disable-search-engine-choice-screen",
@@ -165,7 +165,7 @@ module.exports = {
Chrome: [
// --no-sandbox is needed when running Chrome inside a container or when explicitly requested
sandboxDisabled ? "--no-sandbox" : null,
- "--headless=new",
+ process.env.QUNIT_HEADLESS === "0" ? null : "--headless=new",
"--disable-dev-shm-usage",
"--disable-software-rasterizer",
"--disable-search-engine-choice-screen",
@@ -191,7 +191,7 @@ fetch(`${target}/about.json`).catch(() => {
// eslint-disable-next-line no-console
console.error(
colors.red(
- `Error connecting to Rails server on ${target}. Is it running? Use 'bin/rake qunit:test' or 'plugin:qunit' to start automatically.`
+ `Error connecting to Rails server on ${target}. Is it running? Use 'bin/qunit --standalone' or 'plugin:qunit' to start automatically.`
)
);
});
diff --git a/lib/tasks/docker.rake b/lib/tasks/docker.rake
index 206b00f460e..aab760944ee 100644
--- a/lib/tasks/docker.rake
+++ b/lib/tasks/docker.rake
@@ -300,7 +300,7 @@ task "docker:test" do
unless ENV["RUBY_ONLY"]
unless ENV["SKIP_CORE"]
- @good &&= run_or_fail("CI=1 QUNIT_PARALLEL=#{qunit_concurrency} bin/rake qunit:test")
+ @good &&= run_or_fail("CI=1 QUNIT_PARALLEL=#{qunit_concurrency} bin/qunit --standalone")
end
unless ENV["SKIP_PLUGINS"]
diff --git a/lib/tasks/plugin.rake b/lib/tasks/plugin.rake
index d2e0094d8ba..a18f87b12b3 100644
--- a/lib/tasks/plugin.rake
+++ b/lib/tasks/plugin.rake
@@ -228,24 +228,9 @@ desc "run plugin qunit tests"
task "plugin:qunit", :plugin do |t, args|
args.with_defaults(plugin: "*")
- rake = "#{Rails.root}/bin/rake"
-
- cmd = "LOAD_PLUGINS=1 "
-
- if args[:plugin] == "*"
- plugin_names =
- Dir.glob("plugins/*/test/**/*-test.{gjs,js}").map { |file| file.split("/")[1] }.uniq
- puts "Running qunit tests for all plugins: #{plugin_names.join(", ")}"
- cmd += "PLUGIN_TARGETS='#{plugin_names.join(",")}' "
- else
- puts "Running qunit tests for #{args[:plugin]}"
- cmd += "TARGET='#{args[:plugin]}' "
- end
-
- cmd += "#{rake} qunit:test"
-
- system cmd
- exit $?.exitstatus
+ target = args[:plugin]
+ target = "plugins" if target == "*"
+ exec Rails.root.join("bin/qunit").to_s, "--standalone", "--target", target
end
desc "run all migrations of a plugin"
diff --git a/lib/tasks/qunit.rake b/lib/tasks/qunit.rake
index c546c09e770..2a905c5ff5a 100644
--- a/lib/tasks/qunit.rake
+++ b/lib/tasks/qunit.rake
@@ -2,155 +2,12 @@
desc "Runs the qunit test suite"
task "qunit:test", %i[qunit_path filter] do |_, args|
- require "socket"
- require "chrome_installed_checker"
+ cmd = [Rails.root.join("bin/qunit").to_s, "--standalone"]
+ cmd += ["--qunit-path", args[:qunit_path]] if args[:qunit_path]
- detected_browser =
- begin
- ChromeInstalledChecker.run
- rescue ChromeInstalledChecker::ChromeError => err
- abort err.message
- end
+ filter_arg = args[:filter] || ENV["FILTER"]
+ cmd += ["--filter", filter_arg] if filter_arg
- unless system("command -v pnpm >/dev/null;")
- abort "pnpm is not installed. See https://pnpm.io/installation"
- end
-
- report_requests = ENV["REPORT_REQUESTS"] == "1"
-
- system("pnpm install", exception: true)
-
- # ensure we have this port available
- def port_available?(port)
- server = TCPServer.open port
- server.close
- true
- rescue Errno::EADDRINUSE
- false
- end
-
- if ENV["QUNIT_EMBER_CLI"] == "0"
- puts "The 'legacy' ember environment is discontinued - running tests with ember-cli assets..."
- end
-
- port = ENV["TEST_SERVER_PORT"] || 60_099
- port += 1 while !port_available? port
-
- unicorn_port = 60_098
- unicorn_port += 1 while unicorn_port == port || !port_available?(unicorn_port)
-
- env = {
- "RAILS_ENV" => ENV["QUNIT_RAILS_ENV"] || "test",
- "SKIP_ENFORCE_HOSTNAME" => "1",
- "UNICORN_PID_PATH" => "#{Rails.root}/tmp/pids/unicorn_test_#{unicorn_port}.pid", # So this can run alongside development
- "UNICORN_PORT" => unicorn_port.to_s,
- "UNICORN_SIDEKIQS" => "0",
- "DISCOURSE_SKIP_CSS_WATCHER" => "1",
- "UNICORN_LISTENER" => "127.0.0.1:#{unicorn_port}",
- "LOGSTASH_UNICORN_URI" => nil,
- "UNICORN_WORKERS" => "1",
- "UNICORN_TIMEOUT" => "90",
- }
-
- pid = Process.spawn(env, "#{Rails.root}/bin/unicorn", pgroup: true)
-
- begin
- success = true
- qunit_path = args[:qunit_path]
- filter = args[:filter]
-
- options = { seed: (ENV["QUNIT_SEED"] || Random.new.seed), hidepassed: 1 }
-
- %w[
- module
- filter
- qunit_skip_core
- qunit_single_plugin
- theme_name
- theme_url
- theme_id
- target
- ].each { |arg| options[arg] = ENV[arg.upcase] if ENV[arg.upcase].present? }
-
- options["report_requests"] = "1" if report_requests
-
- query = options.to_query
-
- @now = Time.now
- def elapsed
- Time.now - @now
- end
-
- # wait for server to accept connections
- require "net/http"
- uri = URI("http://localhost:#{unicorn_port}/srv/status")
- puts "Warming up Rails server"
-
- begin
- Net::HTTP.get(uri)
- rescue Errno::ECONNREFUSED,
- Errno::EADDRNOTAVAIL,
- Net::ReadTimeout,
- Net::HTTPBadResponse,
- EOFError
- sleep 1
- retry if elapsed() <= 60
- puts "Timed out. Can not connect to forked server!"
- exit 1
- end
- puts "Rails server is warmed up"
-
- env = {
- "UNICORN_PORT" => unicorn_port.to_s,
- "TESTEM_DEFAULT_BROWSER" => ENV["TESTEM_DEFAULT_BROWSER"].presence || detected_browser,
- }
- cmd = []
-
- parallel = ENV["QUNIT_PARALLEL"]
- reuse_build = ENV["QUNIT_REUSE_BUILD"] == "1"
-
- if qunit_path
- # Bypass `ember test` - it only works properly for the `/tests` path.
- # We have to trigger a `build` manually so that JS is available for rails to serve.
- if !reuse_build
- system("pnpm", "ember", "build", chdir: "#{Rails.root}/frontend/discourse", exception: true)
- end
-
- env["THEME_TEST_PAGES"] = if ENV["THEME_IDS"]
- ENV["THEME_IDS"]
- .split("|")
- .map { |theme_id| "#{qunit_path}?#{query}&testem=1&id=#{theme_id}" }
- .shuffle
- .join(",")
- else
- "#{qunit_path}?#{query}&testem=1"
- end
-
- cmd += %w[pnpm testem ci -f testem.js]
- cmd += ["--parallel", parallel] if parallel
- else
- cmd += ["pnpm", "ember", "exam", "--query", query]
- cmd += ["--load-balance", "--parallel", parallel] if parallel && !ENV["PLUGIN_TARGETS"]
- cmd += ["--filter", filter] if filter
- cmd += %w[--path dist] if reuse_build
- cmd << "--write-execution-file" if ENV["QUNIT_WRITE_EXECUTION_FILE"]
- end
-
- # Print out all env for debugging purposes
- p env
- system(env, *cmd, chdir: "#{Rails.root}/frontend/discourse")
-
- success &&= $?.success?
- ensure
- # was having issues with HUP
- Process.kill "-KILL", pid
- FileUtils.rm("#{Rails.root}/tmp/pids/unicorn_test_#{unicorn_port}.pid")
- end
-
- if success
- puts "\nTests Passed"
- else
- puts "\nTests Failed"
- exit(1)
- end
+ system(ENV, *cmd, chdir: Rails.root)
+ exit $?.exitstatus
end
diff --git a/spec/tasks/qunit_cli_spec.rb b/spec/tasks/qunit_cli_spec.rb
new file mode 100644
index 00000000000..84ba6f41eda
--- /dev/null
+++ b/spec/tasks/qunit_cli_spec.rb
@@ -0,0 +1,149 @@
+# frozen_string_literal: true
+
+describe "bin/qunit" do
+ def run(*args)
+ out, err, status = Open3.capture3("bin/qunit", "--dry-run", *args, chdir: Rails.root.to_s)
+
+ parsed_args, parsed_env =
+ if parsed_result = out.match(/Executing: (?<args>\[.+?\])\nwith env: (?<env>\{.+?\})/m)
+ [JSON.parse(parsed_result[:args]), JSON.parse(parsed_result[:env])]
+ end
+
+ OpenStruct.new(
+ out: out,
+ err: err,
+ status: status.exitstatus,
+ args: parsed_args,
+ env: parsed_env,
+ launched_unicorn: out.include?("[dry-run] skipping unicorn startup"),
+ )
+ end
+
+ let(:core_test_file) do
+ Dir.glob("#{Rails.root}/frontend/discourse/tests/integration/**/*-test.js").first
+ end
+
+ let(:chat_test_file) { Dir.glob("#{Rails.root}/plugins/chat/test/**/*-test.js").first }
+
+ it "runs all core tests by default" do
+ result = run
+ expect(result.status).to eq(0)
+ expect(result.launched_unicorn).to eq(false)
+
+ expect(result.args).to match(
+ [
+ "pnpm",
+ "ember",
+ "exam",
+ "--query",
+ a_string_matching(/\Aseed=\d+&target=core\z/),
+ "--path",
+ "dist",
+ ],
+ )
+ expect(result.env).to match(
+ "UNICORN_PORT" => a_truthy_value,
+ "TESTEM_DEFAULT_BROWSER" => a_truthy_value,
+ "LOAD_PLUGINS" => "0",
+ )
+ end
+
+ it "allows running specific file" do
+ result = run(core_test_file)
+ expect(result.status).to eq(0)
+ expect(result.launched_unicorn).to eq(false)
+
+ expect(result.args).to match(
+ [
+ "pnpm",
+ "ember",
+ "exam",
+ "--query",
+ a_string_matching(/\Aseed=\d+&target=core\z/),
+ "--file-path",
+ core_test_file.sub("#{Rails.root}/frontend/discourse/", ""),
+ "--path",
+ "dist",
+ ],
+ )
+ expect(result.env).to match(
+ "UNICORN_PORT" => a_truthy_value,
+ "TESTEM_DEFAULT_BROWSER" => a_truthy_value,
+ "LOAD_PLUGINS" => "0",
+ )
+ end
+
+ it "allows running all plugin tests" do
+ result = run("--target", "plugins")
+ expect(result.status).to eq(0)
+ expect(result.launched_unicorn).to eq(false)
+
+ expect(result.args).to match(
+ ["pnpm", "ember", "exam", "--query", a_string_matching(/\Aseed=\d+\z/), "--path", "dist"],
+ )
+ expect(result.env).to match(
+ "UNICORN_PORT" => a_truthy_value,
+ "TESTEM_DEFAULT_BROWSER" => a_truthy_value,
+ "LOAD_PLUGINS" => "1",
+ "PLUGIN_TARGETS" => a_string_matching(/,/),
+ )
+ end
+
+ it "allows running tests for multiple plugins" do
+ result = run("--target", "chat,discourse-local-dates")
+ expect(result.status).to eq(0)
+ expect(result.launched_unicorn).to eq(false)
+
+ expect(result.args).to match(
+ ["pnpm", "ember", "exam", "--query", a_string_matching(/\Aseed=\d+\z/), "--path", "dist"],
+ )
+ expect(result.env).to match(
+ "UNICORN_PORT" => a_truthy_value,
+ "TESTEM_DEFAULT_BROWSER" => a_truthy_value,
+ "LOAD_PLUGINS" => "1",
+ "PLUGIN_TARGETS" => "chat,discourse-local-dates",
+ )
+ end
+
+ it "allows running specific plugin test file" do
+ result = run(chat_test_file)
+ expect(result.status).to eq(0)
+ expect(result.launched_unicorn).to eq(false)
+
+ expect(result.args).to match(
+ [
+ "pnpm",
+ "ember",
+ "exam",
+ "--query",
+ a_string_matching(/\Aseed=\d+&target=chat\z/),
+ "--file-path",
+ chat_test_file.sub(
+ "#{Rails.root}/plugins/chat/test/javascripts/",
+ "discourse/plugins/chat/",
+ ),
+ "--path",
+ "dist",
+ ],
+ )
+ expect(result.env).to match(
+ "UNICORN_PORT" => a_truthy_value,
+ "TESTEM_DEFAULT_BROWSER" => a_truthy_value,
+ "LOAD_PLUGINS" => "1",
+ )
+ end
+
+ it "prevents running files from multiple targets" do
+ result = run(core_test_file, chat_test_file)
+ expect(result.status).to eq(1)
+ expect(result.out).to include(
+ "Error: Cannot mix multiple plugin/core targets when running specific files",
+ )
+ end
+
+ it "launches unicorn when using --standalone" do
+ result = run("--standalone")
+ expect(result.status).to eq(0)
+ expect(result.launched_unicorn).to eq(true)
+ end
+end