Skip to content

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

DEV: Load plugin test files only when the runner selects them (#44895)

Jarek Radoszfb49afb2e411
diff --git a/docs/developer-guides/docs/03-code-internals/05-run-test-suites.md b/docs/developer-guides/docs/03-code-internals/05-run-test-suites.md
index 41b88a6ff4a..e5ce2c1973f 100644
--- a/docs/developer-guides/docs/03-code-internals/05-run-test-suites.md
+++ b/docs/developer-guides/docs/03-code-internals/05-run-test-suites.md
@@ -49,6 +49,8 @@ bin/qunit --standalone --no-headless
From the root directory of Discourse:
```sh
+bin/qunit plugins/chat/test/javascripts/unit/lib/chat-audio-test.js # Run one file
+bin/qunit plugins/chat/test/javascripts/unit # Run a directory
bin/rake "plugin:qunit" # Run all plugin qunit tests
bin/rake "plugin:qunit[discourse-chat-integration]" # Run a single plugin's qunit tests
```
diff --git a/frontend/asset-processor/asset-processor-rollup.js b/frontend/asset-processor/asset-processor-rollup.js
index d843fe7fff0..34fce85a743 100644
--- a/frontend/asset-processor/asset-processor-rollup.js
+++ b/frontend/asset-processor/asset-processor-rollup.js
@@ -140,7 +140,8 @@ async function performRollup(modules, opts) {
sourcemap: "hidden",
importAttributesKey: "with",
entryFileNames: `${opts.filenamePrefix ?? ""}[name].[hash:6]${opts.filenameSuffix ?? ""}.js`,
- chunkFileNames: `${opts.filenamePrefix ?? ""}chunk.[hash:6]${opts.filenameSuffix ?? ""}.js`,
+ // One chunk per test file exceeds what a 6-character hash allows in larger plugins.
+ chunkFileNames: `${opts.filenamePrefix ?? ""}chunk.[hash:8]${opts.filenameSuffix ?? ""}.js`,
});
if (opts.pluginName) {
diff --git a/frontend/asset-processor/rollup-virtual-imports.js b/frontend/asset-processor/rollup-virtual-imports.js
index 81360c55cd3..745f26d24bc 100644
--- a/frontend/asset-processor/rollup-virtual-imports.js
+++ b/frontend/asset-processor/rollup-virtual-imports.js
@@ -213,11 +213,33 @@ export default {
labelFor(opts)
);
- // QUnit only finds a test by running the module that registers it, so tests stay eager.
if (entrypointName === "test" || !frontendConfig?.staticModules) {
- const { lines, identifiers } = renderImports(records);
+ const isTest = entrypointName === "test";
+
+ // Evaluating a test file registers its tests, so it loads only when the runner selects it.
+ // Helpers stay eager.
+ const lazy = isTest
+ ? records.filter((record) => record.compatModuleName.endsWith("-test"))
+ : [];
+
+ const { lines, identifiers } = renderImports(
+ records.filter((record) => !lazy.includes(record))
+ );
+
+ for (const record of lazy) {
+ identifiers.set(record, `() => import("./${record.importPath}")`);
+ }
+
+ // The test page loads the other entrypoints anyway. Importing them tells rollup their modules
+ // are present for every test file, so it doesn't split them by which test files use them.
+ const siblings = isTest
+ ? Object.keys(opts.entrypoints ?? {})
+ .filter((name) => name !== "test")
+ .map((name) => `import "virtual:entrypoint:${name}";`)
+ : [];
return [
+ ...siblings,
...lines,
...warnings,
...renderMap("compatModules", records, identifiers),
diff --git a/frontend/asset-processor/rollup-virtual-imports.test.mjs b/frontend/asset-processor/rollup-virtual-imports.test.mjs
index 56cdc429add..a3777cb62e4 100644
--- a/frontend/asset-processor/rollup-virtual-imports.test.mjs
+++ b/frontend/asset-processor/rollup-virtual-imports.test.mjs
@@ -196,7 +196,7 @@ describe("virtual:entrypoint", () => {
"helpers/chat-fixtures.js",
];
- it("eagerly imports every module despite staticModules", () => {
+ it("imports helpers eagerly and test files lazily, despite staticModules", () => {
const output = entrypoint(
TEST_MODULES,
{ frontendConfig: { staticModules: true, exports: {} } },
@@ -205,13 +205,40 @@ describe("virtual:entrypoint", () => {
for (const filename of TEST_MODULES) {
const importPath = filename.replace(/\.\w+$/, "");
- expect(output, filename).toContain(`from "./${importPath}"`);
+
+ if (importPath.endsWith("-test")) {
+ expect(output, filename).not.toContain(`from "./${importPath}"`);
+ expect(output, filename).toContain(
+ `"${importPath}": () => import("./${importPath}"),`
+ );
+ } else {
+ expect(output, filename).toContain(`from "./${importPath}"`);
+ }
}
expect(output).toContain("export default compatModules;");
expect(output).not.toContain("pluginExports");
});
+ it("imports the other entrypoints so their modules are not split by test file", () => {
+ const output = entrypoint(
+ TEST_MODULES,
+ { entrypoints: { main: {}, admin: {}, test: {} } },
+ "test"
+ );
+
+ expect(output).toContain('import "virtual:entrypoint:main";');
+ expect(output).toContain('import "virtual:entrypoint:admin";');
+ expect(output).not.toContain('import "virtual:entrypoint:test";');
+ });
+
+ it("keeps app modules named like tests eager outside the test entrypoint", () => {
+ const output = entrypoint(["discourse/components/ab-test.gjs"]);
+
+ expect(output).toContain('from "./discourse/components/ab-test"');
+ expect(output).not.toContain("import(");
+ });
+
it("still tree-shakes the same modules for a non-test entrypoint", () => {
const output = entrypoint(
TEST_MODULES,
diff --git a/frontend/discourse/tests/test-boot-ember-cli.js b/frontend/discourse/tests/test-boot-ember-cli.js
index 05505ad123f..0f9224c220b 100644
--- a/frontend/discourse/tests/test-boot-ember-cli.js
+++ b/frontend/discourse/tests/test-boot-ember-cli.js
@@ -93,6 +93,25 @@ export async function startTests() {
for (const [key, value] of Object.entries(rawModules)) {
availableModules[key] = value.default;
}
+ } else if (!hasThemeJs) {
+ availableModules = {};
+ for (const name of Object.keys(requirejs.entries)) {
+ if (name.startsWith("discourse/plugins/") && name.endsWith("-test")) {
+ const module = requirejs(name);
+ // Plugin test files are registered as loaders; app modules named "*-test" are namespaces.
+ if (typeof module === "function") {
+ // One broken file must not abort the rest of the run.
+ availableModules[name] = () =>
+ module().catch((error) =>
+ QUnit.module(name, () =>
+ QUnit.test("loads", () => {
+ throw error;
+ })
+ )
+ );
+ }
+ }
+ }
}
await awaitVisualReadiness();