DEV: fix setting isTesting (#23901)

This started out as a seemingly benign refactor to replace the
`require` for `withPluginApi` to an actual import. However, it
broke the test in seemingly random places.

It turns out that in serveral places, we are calling `isTesting()`
in module scope and assigning the result to a constant. For example
we do that in the composer service to disable checking drafts when
testing.

This is problematic because `isTesting` doesn't really set until
the `discourse-bootstrap` initializer is run, and so any modules
that are evaluated before then will have locked in the wrong value
for `isTesting()`.

If we are going to use and treat `isTesting()` like a constant then
we will have to make sure we set it sufficiently early before any
code-loading happens.
This commit is contained in:
Godfrey Chan 2023-10-11 14:20:02 -07:00 committed by GitHub
parent e3e73a3091
commit 210d13e3cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 4 additions and 6 deletions

View File

@ -2,10 +2,11 @@ import Application from "@ember/application";
import "./global-compat";
import "./loader-shims";
import require from "require";
import { normalizeEmberEventHandling } from "discourse/lib/ember-events";
import { registerDiscourseImplicitInjections } from "discourse/lib/implicit-injections";
import { withPluginApi } from "discourse/lib/plugin-api";
import { isTesting } from "discourse-common/config/environment";
import { buildResolver } from "discourse-common/resolver";
import { normalizeEmberEventHandling } from "./lib/ember-events";
const _pluginCallbacks = [];
let _unhandledThemeErrors = [];
@ -147,8 +148,6 @@ function loadInitializers(app) {
}
// Plugins that are registered via `<script>` tags.
const { withPluginApi } = require("discourse/lib/plugin-api");
for (let [i, callback] of _pluginCallbacks.entries()) {
app.instanceInitializer({
name: `_discourse_plugin_${i}`,

View File

@ -1 +1,2 @@
require("discourse-common/config/environment").setEnvironment("testing");
require("discourse/tests/test-boot-ember-cli");

View File

@ -1 +1,2 @@
require("discourse-common/config/environment").setEnvironment("testing");
require("discourse/tests/test-boot-ember-cli");

View File

@ -4,11 +4,8 @@ import { start } from "ember-qunit";
import * as QUnit from "qunit";
import { setup } from "qunit-dom";
import setupTests from "discourse/tests/setup-tests";
import { setEnvironment } from "discourse-common/config/environment";
import config from "../config/environment";
setEnvironment("testing");
document.addEventListener("discourse-booted", () => {
// eslint-disable-next-line no-undef
if (!EmberENV.TESTS_FILE_LOADED) {