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

This commit is contained in:
David Taylor 2022-08-18 16:19:46 +01:00 committed by GitHub
parent 707034bc75
commit c63580b226
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
};
}