Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Skip loading plugin JS when running only core tests #18047

Merged
merged 4 commits into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -17,6 +17,7 @@ lib/javascripts/messageformat.js
lib/highlight_js/
plugins/**/lib/javascripts/locale
public/
!/app/assets/javascripts/discourse/public
vendor/
app/assets/javascripts/discourse/tests/fixtures
spec/
Expand Down
18 changes: 12 additions & 6 deletions app/assets/javascripts/discourse/lib/bootstrap-json/index.js
Expand Up @@ -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>`;
}
},

Expand Down
Expand Up @@ -27,4 +27,4 @@ document.body.insertAdjacentHTML(
`
);

require('discourse/tests/test-boot-ember-cli');
require("discourse/tests/test-boot-ember-cli");
@@ -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);
}
@@ -0,0 +1 @@
require("discourse/tests/test-boot-ember-cli");
25 changes: 16 additions & 9 deletions app/assets/javascripts/discourse/tests/index.html
Expand Up @@ -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>
7 changes: 7 additions & 0 deletions app/assets/javascripts/discourse/tests/test-boot-ember-cli.js
Expand Up @@ -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(
Expand Down