DEV: Simplify qunit target selection (#22591)

Previously we had three query parameters to control which tests would be run. The default was to run all core/plugin tests together, which would almost always lead to errors and does not match the way we run tests in CI.

This commit removes the three old parameters (skip_core, skip_plugins and single_plugin), and introduces a new 'target' parameter. This can have a value of 'core', 'plugins', 'all', or a specific plugin name. The default is 'core'. Attempting to use the old parameters will raise an error.
This commit is contained in:
David Taylor 2023-07-13 14:20:00 +01:00 committed by GitHub
parent dfe94ba118
commit 9c915345ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 70 deletions

View File

@ -1,7 +1,8 @@
const dynamicJsTemplate = document.querySelector("#dynamic-test-js"); const dynamicJsTemplate = document.querySelector("#dynamic-test-js");
const params = new URLSearchParams(document.location.search); 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() { (async function setup() {
for (const element of dynamicJsTemplate.content.childNodes) { for (const element of dynamicJsTemplate.content.childNodes) {

View File

@ -125,31 +125,39 @@ function setupToolbar() {
) )
); );
QUnit.config.urlConfig.push({
id: "qunit_skip_core",
label: "Skip Core",
value: "1",
});
QUnit.config.urlConfig.push({
id: "qunit_skip_plugins",
label: "Skip Plugins",
value: "1",
});
const pluginNames = new Set(); const pluginNames = new Set();
Object.keys(requirejs.entries).forEach((moduleName) => { document
const found = moduleName.match(/\/plugins\/([\w-]+)\//); .querySelector("#dynamic-test-js")
if (found && moduleName.match(/\-test/)) { ?.content.querySelectorAll("script[data-discourse-plugin]")
pluginNames.add(found[1]); .forEach((script) => pluginNames.add(script.dataset.discoursePlugin));
}
});
QUnit.config.urlConfig.push({ QUnit.config.urlConfig.push({
id: "qunit_single_plugin", id: "target",
label: "Plugin", label: "Target",
value: Array.from(pluginNames), value: ["core", "plugins", "all", "-----", ...Array.from(pluginNames)],
});
QUnit.begin(() => {
const select = document.querySelector(
`#qunit-testrunner-toolbar [name=target]`
);
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;
}
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 // Abort tests when the qunit controls are clicked
@ -346,20 +354,9 @@ export default function setupTests(config) {
QUnit.config.autostart = false; QUnit.config.autostart = false;
} }
let skipCore = handleLegacyParameters();
getUrlParameter("qunit_single_plugin") ||
getUrlParameter("qunit_skip_core") === "1";
let singlePlugin = getUrlParameter("qunit_single_plugin"); const target = getUrlParameter("target") || "core";
let skipPlugins = !singlePlugin && getUrlParameter("qunit_skip_plugins");
if (skipCore && !getUrlParameter("qunit_skip_core")) {
replaceUrlParameter("qunit_skip_core", "1");
}
if (!skipPlugins && getUrlParameter("qunit_skip_plugins")) {
replaceUrlParameter("qunit_skip_plugins", null);
}
const shouldLoadModule = (name) => { const shouldLoadModule = (name) => {
if (!/\-test/.test(name)) { if (!/\-test/.test(name)) {
@ -370,13 +367,15 @@ export default function setupTests(config) {
const isCore = !isPlugin; const isCore = !isPlugin;
const pluginName = name.match(/\/plugins\/([\w-]+)\//)?.[1]; const pluginName = name.match(/\/plugins\/([\w-]+)\//)?.[1];
if (skipCore && isCore) { const loadCore = target === "core" || target === "all";
const loadAllPlugins = target === "plugins" || target === "all";
if (isCore && !loadCore) {
return false; return false;
} else if (skipPlugins && isPlugin) { } else if (isPlugin && !(loadAllPlugins || pluginName === target)) {
return false;
} else if (singlePlugin && singlePlugin !== pluginName) {
return false; return false;
} }
return true; return true;
}; };
@ -402,28 +401,6 @@ function getUrlParameter(name) {
return queryParams.get(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() { function patchFailedAssertion() {
const oldPushResult = QUnit.assert.pushResult; const oldPushResult = QUnit.assert.pushResult;
@ -439,3 +416,19 @@ function patchFailedAssertion() {
oldPushResult.call(this, resultInfo); 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`
);
});
}
}
}

View File

@ -25,7 +25,8 @@ document.addEventListener("discourse-booted", () => {
} }
const params = new URLSearchParams(window.location.search); 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"; const disableAutoStart = params.get("qunit_disable_auto_start") === "1";
Ember.ENV.LOG_STACKTRACE_ON_DEPRECATION = false; Ember.ENV.LOG_STACKTRACE_ON_DEPRECATION = false;
@ -60,7 +61,7 @@ document.addEventListener("discourse-booted", () => {
setupTestContainer: false, setupTestContainer: false,
loadTests: false, loadTests: false,
startTests: !disableAutoStart, startTests: !disableAutoStart,
setupEmberOnerrorValidation: !skipCore, setupEmberOnerrorValidation: testingCore,
setupTestIsolationValidation: true, setupTestIsolationValidation: true,
}); });
}); });

View File

@ -209,14 +209,17 @@ task "plugin:qunit", %i[plugin timeout] do |t, args|
rake = "#{Rails.root}/bin/rake" rake = "#{Rails.root}/bin/rake"
cmd = "LOAD_PLUGINS=1 " cmd = "LOAD_PLUGINS=1 "
cmd += "QUNIT_SKIP_CORE=1 "
if args[:plugin] == "*" target =
puts "Running qunit tests for all plugins" if args[:plugin] == "*"
else puts "Running qunit tests for all plugins"
puts "Running qunit tests for #{args[:plugin]}" "plugins"
cmd += "QUNIT_SINGLE_PLUGIN='#{args[:plugin]}' " else
end puts "Running qunit tests for #{args[:plugin]}"
args[:plugin]
end
cmd += "TARGET='#{target}' "
cmd += "#{rake} qunit:test" cmd += "#{rake} qunit:test"
cmd += "[#{args[:timeout]}]" if args[:timeout] cmd += "[#{args[:timeout]}]" if args[:timeout]

View File

@ -69,6 +69,7 @@ task "qunit:test", %i[timeout qunit_path filter] do |_, args|
theme_name theme_name
theme_url theme_url
theme_id theme_id
target
].each { |arg| options[arg] = ENV[arg.upcase] if ENV[arg.upcase].present? } ].each { |arg| options[arg] = ENV[arg.upcase] if ENV[arg.upcase].present? }
options["report_requests"] = "1" if report_requests options["report_requests"] = "1" if report_requests