Skip to content

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

DEV: Ensure unhandled deprecation throws errors in core tests. (#34903)

Sérgio Saquetim8f0b790917c9
diff --git a/app/assets/javascripts/discourse/app/lib/autocomplete.js b/app/assets/javascripts/discourse/app/lib/autocomplete.js
index 95d5cb63cd5..77590e030e1 100644
--- a/app/assets/javascripts/discourse/app/lib/autocomplete.js
+++ b/app/assets/javascripts/discourse/app/lib/autocomplete.js
@@ -3,7 +3,7 @@ import { createPopper } from "@popperjs/core";
import $ from "jquery";
import discourseDebounce from "discourse/lib/debounce";
import deprecated from "discourse/lib/deprecated";
-import { INPUT_DELAY } from "discourse/lib/environment";
+import { INPUT_DELAY, isTesting } from "discourse/lib/environment";
import { iconHTML } from "discourse/lib/icon-library";
import discourseLater from "discourse/lib/later";
import { isDocumentRTL } from "discourse/lib/text-direction";
@@ -21,6 +21,7 @@ export const CANCELLED_STATUS = "__CANCELLED";
const ALLOWED_LETTERS_REGEXP = /[\s[{(/+]/;
let _autoCompletePopper, _inputTimeout;
+let skipDeprecationWarningInTests = false;
const keys = {
backSpace: 8,
@@ -48,14 +49,34 @@ const keys = {
z: 90,
};
+/**
+ * Disables deprecation warning output during QUnit tests.
+ *
+ * USE ONLY FOR TESTING PURPOSES
+ */
+export function disableDeprecationWarningInTests() {
+ skipDeprecationWarningInTests = true;
+}
+
+/**
+ * Enables deprecation warning output during QUnit tests.
+ *
+ * USE ONLY FOR TESTING PURPOSES
+ */
+export function enableDeprecationWarningInTests() {
+ skipDeprecationWarningInTests = false;
+}
+
export default function (options) {
- deprecated(
- "$.fn.autocomplete is deprecated and will be removed in a future release. Please use the DMultiSelect component or the DAutocomplete modifier instead.",
- {
- id: "discourse.jquery-autocomplete",
- since: "3.6.0.beta1-dev",
- }
- );
+ if (!isTesting() || !skipDeprecationWarningInTests) {
+ deprecated(
+ "$.fn.autocomplete is deprecated and will be removed in a future release. Please use the DMultiSelect component or the DAutocomplete modifier instead.",
+ {
+ id: "discourse.jquery-autocomplete",
+ since: "3.6.0.beta1-dev",
+ }
+ );
+ }
if (this.length === 0) {
return;
diff --git a/app/assets/javascripts/discourse/tests/helpers/raise-on-deprecation.js b/app/assets/javascripts/discourse/tests/helpers/raise-on-deprecation.js
index 2a2e8aac71c..1ed0c5bb771 100644
--- a/app/assets/javascripts/discourse/tests/helpers/raise-on-deprecation.js
+++ b/app/assets/javascripts/discourse/tests/helpers/raise-on-deprecation.js
@@ -4,6 +4,7 @@ import DeprecationWorkflow from "discourse/deprecation-workflow";
import { registerDeprecationHandler as registerDiscourseDeprecationHandler } from "discourse/lib/deprecated";
let disabled = false;
+let disabledQUnitResult = false;
export function configureRaiseOnDeprecation() {
if (window.EmberENV.RAISE_ON_DEPRECATION !== undefined) {
@@ -13,7 +14,7 @@ export function configureRaiseOnDeprecation() {
registerDeprecationHandler((message, options, next) => {
if (
disabled ||
- !DeprecationWorkflow.shouldThrow(options.id) ||
+ !DeprecationWorkflow.shouldThrow(options.id, true) ||
options.id.startsWith("ember-metal.")
) {
return next(message, options);
@@ -22,7 +23,7 @@ export function configureRaiseOnDeprecation() {
});
registerDiscourseDeprecationHandler((message, options) => {
- if (disabled || !DeprecationWorkflow.shouldThrow(options?.id)) {
+ if (disabled || !DeprecationWorkflow.shouldThrow(options?.id, true)) {
return;
}
raiseDeprecationError(message, options);
@@ -31,7 +32,7 @@ export function configureRaiseOnDeprecation() {
function raiseDeprecationError(message, options) {
message = `DEPRECATION IN CORE TEST: ${message} (deprecation id: ${options.id})\n\nCore test runs must be deprecation-free. Use ember-deprecation-workflow to silence unresolved deprecations.`;
- if (QUnit.config.current) {
+ if (QUnit.config.current && !disabledQUnitResult) {
QUnit.assert.pushResult({
result: false,
message,
@@ -47,3 +48,11 @@ export function disableRaiseOnDeprecation() {
export function enableRaiseOnDeprecation() {
disabled = false;
}
+
+export function disableRaiseOnDeprecationQUnitResult() {
+ disabledQUnitResult = true;
+}
+
+export function enableRaiseOnDeprecationQUnitResult() {
+ disabledQUnitResult = false;
+}
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js b/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js
index 103d28a5ae5..b38a39b6aba 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/autocomplete-test.js
@@ -2,6 +2,10 @@ import { triggerKeyEvent } from "@ember/test-helpers";
import { setupTest } from "ember-qunit";
import $ from "jquery";
import { module, test } from "qunit";
+import {
+ disableDeprecationWarningInTests,
+ enableDeprecationWarningInTests,
+} from "discourse/lib/autocomplete";
import { escapeExpression, setCaretPosition } from "discourse/lib/utilities";
import {
simulateKey,
@@ -27,6 +31,10 @@ module("Unit | Utility | autocomplete", function (hooks) {
return _element;
}
+ hooks.beforeEach(() => {
+ disableDeprecationWarningInTests();
+ });
+
hooks.afterEach(() => {
if (!_element) {
return;
@@ -35,6 +43,8 @@ module("Unit | Utility | autocomplete", function (hooks) {
$e.autocomplete({ cancel: true });
$e.autocomplete("destroy");
_element.remove();
+
+ enableDeprecationWarningInTests();
});
test("Autocomplete can complete really short terms correctly", async function (assert) {
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/deprecated-test.js b/app/assets/javascripts/discourse/tests/unit/lib/deprecated-test.js
index 7d17f14da35..559f0edc3c6 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/deprecated-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/deprecated-test.js
@@ -9,7 +9,9 @@ import deprecated, {
import DeprecationCounter from "discourse/tests/helpers/deprecation-counter";
import {
disableRaiseOnDeprecation,
+ disableRaiseOnDeprecationQUnitResult,
enableRaiseOnDeprecation,
+ enableRaiseOnDeprecationQUnitResult,
} from "discourse/tests/helpers/raise-on-deprecation";
module("Unit | Utility | deprecated", function (hooks) {
@@ -25,6 +27,8 @@ module("Unit | Utility | deprecated", function (hooks) {
});
hooks.afterEach(function () {
+ this.warnStub.restore();
+ this.counterStub.restore();
enableRaiseOnDeprecation();
});
@@ -351,3 +355,23 @@ module("Unit | Utility | deprecated", function (hooks) {
);
});
});
+
+module("Unit | Utility | deprecated | raise-on-deprecation", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ disableRaiseOnDeprecationQUnitResult();
+ this.warnStub = Sinon.stub(console, "warn");
+ });
+
+ hooks.afterEach(function () {
+ enableRaiseOnDeprecationQUnitResult();
+ this.warnStub.restore();
+ });
+
+ test("unhandled deprecations raises an error in tests", function (assert) {
+ assert.throws(() => {
+ deprecated("My message");
+ }, "the error was raised");
+ });
+});