2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00

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

Strengthens deprecation handling in tests by enforcing errors for
unhandled deprecations.

* Enforces error throwing for unhandled deprecations via stricter
DeprecationWorkflow.shouldThrow(..., true) usage in test handlers
* Fix the autocomplete deprecation warnings that were introduced while
the deprecations were not throwing errors.
This commit is contained in:
Sérgio Saquetim 2025-09-22 22:45:59 -03:00 committed by GitHub
parent 14438e92d8
commit 8f0b790917
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 75 additions and 11 deletions

View file

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

View file

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

View file

@ -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) {

View file

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