Skip to content

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

DEV: Add hint when qunit assertion fails and app is unsettled (#17984)

David Taylorc63580b22638
diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js
index f9c4f3c5103..f8e37d3aa5a 100644
--- a/app/assets/javascripts/discourse/tests/setup-tests.js
+++ b/app/assets/javascripts/discourse/tests/setup-tests.js
@@ -13,7 +13,12 @@ import pretender, {
} from "discourse/tests/helpers/create-pretender";
import { resetSettings } from "discourse/tests/helpers/site-settings";
import { getOwner, setDefaultOwner } from "discourse-common/lib/get-owner";
-import { setApplication, setResolver } from "@ember/test-helpers";
+import {
+ getSettledState,
+ isSettled,
+ setApplication,
+ setResolver,
+} from "@ember/test-helpers";
import { setupS3CDN, setupURL } from "discourse-common/lib/get-url";
import Application from "../app";
import MessageBus from "message-bus-client";
@@ -372,6 +377,7 @@ export default function setupTests(config) {
setupToolbar();
reportMemoryUsageAfterTests();
+ patchFailedAssertion();
}
function getUrlParameter(name) {
@@ -400,3 +406,19 @@ function replaceUrlParameter(name, value) {
}
});
}
+
+function patchFailedAssertion() {
+ const oldPushResult = QUnit.assert.pushResult;
+
+ QUnit.assert.pushResult = function (resultInfo) {
+ if (!resultInfo.result && !isSettled()) {
+ // eslint-disable-next-line no-console
+ console.warn(
+ "ℹ️ Hint: when the assertion failed, the Ember runloop was not in a settled state. Maybe you missed an `await` further up the test? Or maybe you need to manually add `await settled()` before your assertion?",
+ getSettledState()
+ );
+ }
+
+ oldPushResult.call(this, resultInfo);
+ };
+}