A platform for community discussion. Free, open, simple.
DEV: Skip loading plugin JS when running only core tests (#18047)
David Taylor3b1a46ff3768
diff --git a/.prettierignore b/.prettierignoreindex fff97dce442..b33a5618e4d 100644--- a/.prettierignore+++ b/.prettierignore@@ -17,6 +17,7 @@ lib/javascripts/messageformat.jslib/highlight_js/plugins/**/lib/javascripts/localepublic/+!/app/assets/javascripts/discourse/publicvendor/app/assets/javascripts/discourse/tests/fixturesspec/diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.jsindex 0eb9ce0b3de..3249cbbc6eb 100644--- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js+++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js@@ -388,24 +388,30 @@ module.exports = {for (const { name, hasJs } of pluginInfos) {if (hasJs) {- scripts.push(`plugins/${name}.js`);+ scripts.push({ src: `plugins/${name}.js`, name });}if (fs.existsSync(`../plugins/${name}_extras.js.erb`)) {- scripts.push(`plugins/${name}_extras.js`);+ scripts.push({ src: `plugins/${name}_extras.js`, name });}}} else {- scripts.push("discourse/tests/active-plugins.js");+ scripts.push({+ src: "discourse/tests/active-plugins.js",+ name: "_all",+ });}- scripts.push("admin-plugins.js");+ scripts.push({ src: "admin-plugins.js", name: "_admin" });return scripts- .map((s) => `<script src="${config.rootURL}assets/${s}"></script>`)+ .map(+ ({ src, name }) =>+ `<script src="${config.rootURL}assets/${src}" data-discourse-plugin="${name}"></script>`+ ).join("\n");} else if (shouldLoadPluginTestJs() && type === "test-plugin-tests-js") {- return `<script id="plugin-test-script" src="${config.rootURL}assets/discourse/tests/plugin-tests.js"></script>`;+ return `<script id="plugin-test-script" src="${config.rootURL}assets/discourse/tests/plugin-tests.js" data-discourse-plugin="_all"></script>`;}},diff --git a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-listen-boot.js b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-listen-boot.jsindex 48a7c880f6b..bc903572c7b 100644--- a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-listen-boot.js+++ b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-listen-boot.js@@ -27,4 +27,4 @@ document.body.insertAdjacentHTML(`);-require('discourse/tests/test-boot-ember-cli');+require("discourse/tests/test-boot-ember-cli");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.jsnew file mode 100644index 00000000000..fc0237c6225--- /dev/null+++ b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js@@ -0,0 +1,28 @@+const dynamicJsTemplate = document.querySelector("#dynamic-test-js");++const params = new URLSearchParams(document.location.search);+const skipPlugins = params.get("qunit_skip_plugins");++for (const element of dynamicJsTemplate.content.childNodes) {+ if (skipPlugins && element.dataset?.discoursePlugin) {+ continue;+ }++ if (+ element.tagName === "SCRIPT" &&+ element.innerHTML.includes("EmberENV.TESTS_FILE_LOADED")+ ) {+ // Inline script introduced by ember-cli. Incompatible with CSP and our custom plugin JS loading system+ // https://github.com/ember-cli/ember-cli/blob/04a38fda2c/lib/utilities/ember-app-utils.js#L131+ // We re-implement in test-boot-ember-cli.js+ continue;+ }++ const clone = element.cloneNode(true);++ if (clone.tagName === "SCRIPT") {+ clone.async = false;+ }++ document.querySelector("discourse-dynamic-test-js").appendChild(clone);+}diff --git a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-trigger-ember-cli-boot.js b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-trigger-ember-cli-boot.jsnew file mode 100644index 00000000000..65f629d8a02--- /dev/null+++ b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-trigger-ember-cli-boot.js@@ -0,0 +1 @@+require("discourse/tests/test-boot-ember-cli");diff --git a/app/assets/javascripts/discourse/tests/index.html b/app/assets/javascripts/discourse/tests/index.htmlindex e8c2e10b163..03d82fa2db3 100644--- a/app/assets/javascripts/discourse/tests/index.html+++ b/app/assets/javascripts/discourse/tests/index.html@@ -50,15 +50,22 @@<script src="{{rootURL}}assets/discourse-markdown.js"></script><script src="{{rootURL}}assets/admin.js"></script><script src="{{rootURL}}assets/wizard.js"></script>- {{content-for "test-plugin-js"}}- <script src="{{rootURL}}assets/test-helpers.js"></script>- <script src="{{rootURL}}assets/core-tests.js"></script>- {{content-for "test-plugin-tests-js"}}- <script>- require("discourse/tests/test-boot-ember-cli");- </script>- <script src="{{rootURL}}assets/scripts/discourse-boot.js"></script>- {{content-for "body-footer"}} {{content-for "test-body-footer"}}+ <template id="dynamic-test-js">+ {{content-for "test-plugin-js"}}+ <script defer src="{{rootURL}}assets/test-helpers.js"></script>+ <script defer src="{{rootURL}}assets/core-tests.js"></script>+ {{content-for "test-plugin-tests-js"}}+ <script defer src="{{rootURL}}assets/scripts/discourse-test-trigger-ember-cli-boot.js"></script>+ <script defer src="{{rootURL}}assets/scripts/discourse-boot.js"></script>+ {{content-for "body-footer"}} {{content-for "test-body-footer"}}+ </template>++ <discourse-dynamic-test-js>+ </discourse-dynamic-test-js>++ <!-- This script takes the <template>, filters plugin assets as required, then appends to discourse-dynamic-test-js -->+ <script src="{{rootURL}}assets/scripts/discourse-test-load-dynamic-js.js"></script>+</body></html>diff --git a/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js b/app/assets/javascripts/discourse/tests/test-boot-ember-cli.jsindex c11fbcfeb57..eabbec1ef7c 100644--- a/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js+++ b/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js@@ -8,6 +8,13 @@ import { setup } from "qunit-dom";setEnvironment("testing");document.addEventListener("discourse-booted", () => {+ // eslint-disable-next-line no-undef+ if (!EmberENV.TESTS_FILE_LOADED) {+ throw new Error(+ 'The tests file was not loaded. Make sure your tests index.html includes "assets/tests.js".'+ );+ }+const script = document.getElementById("plugin-test-script");if (script && !requirejs.entries["discourse/tests/plugin-tests"]) {throw new Error(