Skip to content

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

DEV: Simplify qunit target selection (#22591)

David Taylor9c915345ea0e
diff --git a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js
index 8041b5141bd..5f461030810 100644
--- a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js
+++ b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js
@@ -1,7 +1,8 @@
const dynamicJsTemplate = document.querySelector("#dynamic-test-js");
const params = new URLSearchParams(document.location.search);
-const skipPlugins = params.get("qunit_skip_plugins");
+const target = params.get("target");
+const skipPlugins = !target || target === "core";
(async function setup() {
for (const element of dynamicJsTemplate.content.childNodes) {
diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js
index 6f1d61ac570..a16401e2ac9 100644
--- a/app/assets/javascripts/discourse/tests/setup-tests.js
+++ b/app/assets/javascripts/discourse/tests/setup-tests.js
@@ -125,31 +125,39 @@ function setupToolbar() {
)
);
- QUnit.config.urlConfig.push({
- id: "qunit_skip_core",
- label: "Skip Core",
- value: "1",
- });
+ const pluginNames = new Set();
+
+ document
+ .querySelector("#dynamic-test-js")
+ ?.content.querySelectorAll("script[data-discourse-plugin]")
+ .forEach((script) => pluginNames.add(script.dataset.discoursePlugin));
QUnit.config.urlConfig.push({
- id: "qunit_skip_plugins",
- label: "Skip Plugins",
- value: "1",
+ id: "target",
+ label: "Target",
+ value: ["core", "plugins", "all", "-----", ...Array.from(pluginNames)],
});
- const pluginNames = new Set();
+ QUnit.begin(() => {
+ const select = document.querySelector(
+ `#qunit-testrunner-toolbar [name=target]`
+ );
- Object.keys(requirejs.entries).forEach((moduleName) => {
- const found = moduleName.match(/\/plugins\/([\w-]+)\//);
- if (found && moduleName.match(/\-test/)) {
- pluginNames.add(found[1]);
+ const testingThemeId = parseInt(
+ document.querySelector("script[data-theme-id]")?.dataset.themeId,
+ 10
+ );
+ if (testingThemeId) {
+ select.innerHTML = `<option selected>theme id ${testingThemeId}</option>`;
+ select.disabled = true;
+ return;
}
- });
- QUnit.config.urlConfig.push({
- id: "qunit_single_plugin",
- label: "Plugin",
- value: Array.from(pluginNames),
+ select.value ||= "core";
+ select.querySelector("option:not([value])").remove();
+ select.querySelector("option[value=-----]").disabled = true;
+ select.querySelector("option[value=all]").innerText =
+ "all (not recommended)";
});
// Abort tests when the qunit controls are clicked
@@ -346,20 +354,9 @@ export default function setupTests(config) {
QUnit.config.autostart = false;
}
- let skipCore =
- getUrlParameter("qunit_single_plugin") ||
- getUrlParameter("qunit_skip_core") === "1";
-
- let singlePlugin = getUrlParameter("qunit_single_plugin");
- let skipPlugins = !singlePlugin && getUrlParameter("qunit_skip_plugins");
-
- if (skipCore && !getUrlParameter("qunit_skip_core")) {
- replaceUrlParameter("qunit_skip_core", "1");
- }
+ handleLegacyParameters();
- if (!skipPlugins && getUrlParameter("qunit_skip_plugins")) {
- replaceUrlParameter("qunit_skip_plugins", null);
- }
+ const target = getUrlParameter("target") || "core";
const shouldLoadModule = (name) => {
if (!/\-test/.test(name)) {
@@ -370,13 +367,15 @@ export default function setupTests(config) {
const isCore = !isPlugin;
const pluginName = name.match(/\/plugins\/([\w-]+)\//)?.[1];
- if (skipCore && isCore) {
- return false;
- } else if (skipPlugins && isPlugin) {
+ const loadCore = target === "core" || target === "all";
+ const loadAllPlugins = target === "plugins" || target === "all";
+
+ if (isCore && !loadCore) {
return false;
- } else if (singlePlugin && singlePlugin !== pluginName) {
+ } else if (isPlugin && !(loadAllPlugins || pluginName === target)) {
return false;
}
+
return true;
};
@@ -402,28 +401,6 @@ function getUrlParameter(name) {
return queryParams.get(name);
}
-function replaceUrlParameter(name, value) {
- const queryParams = new URLSearchParams(window.location.search);
- if (value === null) {
- queryParams.delete(name);
- } else {
- queryParams.set(name, value);
- }
- history.replaceState(null, null, "?" + queryParams.toString());
-
- QUnit.begin(() => {
- QUnit.config[name] = value;
- const formElement = document.querySelector(
- `#qunit-testrunner-toolbar [name=${name}]`
- );
- if (formElement?.type === "checkbox") {
- formElement.checked = !!value;
- } else if (formElement) {
- formElement.value = value;
- }
- });
-}
-
function patchFailedAssertion() {
const oldPushResult = QUnit.assert.pushResult;
@@ -439,3 +416,19 @@ function patchFailedAssertion() {
oldPushResult.call(this, resultInfo);
};
}
+
+function handleLegacyParameters() {
+ for (const param of [
+ "qunit_single_plugin",
+ "qunit_skip_core",
+ "qunit_skip_plugins",
+ ]) {
+ if (getUrlParameter(param)) {
+ QUnit.begin(() => {
+ throw new Error(
+ `${param} is no longer supported. Use the 'target' parameter instead`
+ );
+ });
+ }
+ }
+}
diff --git a/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js b/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js
index c16a0c17680..fd8ffc4f209 100644
--- a/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js
+++ b/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js
@@ -25,7 +25,8 @@ document.addEventListener("discourse-booted", () => {
}
const params = new URLSearchParams(window.location.search);
- const skipCore = params.get("qunit_skip_core") === "1";
+ const target = params.get("target");
+ const testingCore = !target || target === "core";
const disableAutoStart = params.get("qunit_disable_auto_start") === "1";
Ember.ENV.LOG_STACKTRACE_ON_DEPRECATION = false;
@@ -60,7 +61,7 @@ document.addEventListener("discourse-booted", () => {
setupTestContainer: false,
loadTests: false,
startTests: !disableAutoStart,
- setupEmberOnerrorValidation: !skipCore,
+ setupEmberOnerrorValidation: testingCore,
setupTestIsolationValidation: true,
});
});
diff --git a/lib/tasks/plugin.rake b/lib/tasks/plugin.rake
index ea42029c73f..9711141902a 100644
--- a/lib/tasks/plugin.rake
+++ b/lib/tasks/plugin.rake
@@ -209,14 +209,17 @@ task "plugin:qunit", %i[plugin timeout] do |t, args|
rake = "#{Rails.root}/bin/rake"
cmd = "LOAD_PLUGINS=1 "
- cmd += "QUNIT_SKIP_CORE=1 "
- if args[:plugin] == "*"
- puts "Running qunit tests for all plugins"
- else
- puts "Running qunit tests for #{args[:plugin]}"
- cmd += "QUNIT_SINGLE_PLUGIN='#{args[:plugin]}' "
- end
+ target =
+ if args[:plugin] == "*"
+ puts "Running qunit tests for all plugins"
+ "plugins"
+ else
+ puts "Running qunit tests for #{args[:plugin]}"
+ args[:plugin]
+ end
+
+ cmd += "TARGET='#{target}' "
cmd += "#{rake} qunit:test"
cmd += "[#{args[:timeout]}]" if args[:timeout]
diff --git a/lib/tasks/qunit.rake b/lib/tasks/qunit.rake
index 0c4beee249b..e03f103e057 100644
--- a/lib/tasks/qunit.rake
+++ b/lib/tasks/qunit.rake
@@ -69,6 +69,7 @@ task "qunit:test", %i[timeout qunit_path filter] do |_, args|
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