Skip to content

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

DEV: Add `withSilencedDeprecationsAsync` for async functions (#19057)

David Taylor6d6d5a200fd3
diff --git a/app/assets/javascripts/discourse-common/addon/lib/deprecated.js b/app/assets/javascripts/discourse-common/addon/lib/deprecated.js
index 1e6bdd1baa0..4752732e3c0 100644
--- a/app/assets/javascripts/discourse-common/addon/lib/deprecated.js
+++ b/app/assets/javascripts/discourse-common/addon/lib/deprecated.js
@@ -60,18 +60,37 @@ export function registerDeprecationHandler(callback) {
/**
* Silence one or more deprecations while running `callback`
+ * @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
+ * @param {function} callback The function to call while deprecations are silenced.
+ */
+export function withSilencedDeprecations(deprecationIds, callback) {
+ const idArray = [].concat(deprecationIds);
+ try {
+ idArray.forEach((id) => disabledDeprecations.add(id));
+ const result = callback();
+ if (result instanceof Promise) {
+ throw new Error(
+ "withSilencedDeprecations callback returned a promise. Use withSilencedDeprecationsAsync instead."
+ );
+ }
+ return result;
+ } finally {
+ idArray.forEach((id) => disabledDeprecations.delete(id));
+ }
+}
+
+/**
+ * Silence one or more deprecations while running an async `callback`
* @async
* @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
- * @param {function} callback The function to call while deprecations are silenced. Can be asynchronous.
+ * @param {function} callback The asynchronous function to call while deprecations are silenced.
*/
-export async function withSilencedDeprecations(deprecationIds, callback) {
+export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
const idArray = [].concat(deprecationIds);
- let result;
try {
idArray.forEach((id) => disabledDeprecations.add(id));
- result = callback();
+ return await callback();
} finally {
idArray.forEach((id) => disabledDeprecations.delete(id));
- return result;
}
}
diff --git a/app/assets/javascripts/discourse/tests/integration/components/user-selector-test.js b/app/assets/javascripts/discourse/tests/integration/components/user-selector-test.js
index 7a474c36869..e0484728125 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/user-selector-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/user-selector-test.js
@@ -3,7 +3,7 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers";
import { query } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars";
-import { withSilencedDeprecations } from "discourse-common/lib/deprecated";
+import { withSilencedDeprecationsAsync } from "discourse-common/lib/deprecated";
function paste(element, text) {
let e = new Event("paste");
@@ -17,7 +17,7 @@ module("Integration | Component | user-selector", function (hooks) {
test("pasting a list of usernames", async function (assert) {
this.set("usernames", "evil,trout");
- await withSilencedDeprecations(
+ await withSilencedDeprecationsAsync(
"discourse.user-selector-component",
async () => {
await render(
@@ -51,7 +51,7 @@ module("Integration | Component | user-selector", function (hooks) {
this.set("usernames", "mark");
this.set("excludedUsernames", ["jeff", "sam", "robin"]);
- await withSilencedDeprecations(
+ await withSilencedDeprecationsAsync(
"discourse.user-selector-component",
async () => {
await render(
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 c64a8d1ba60..a6dd4aece1f 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/deprecated-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/deprecated-test.js
@@ -1,6 +1,7 @@
import {
default as deprecated,
withSilencedDeprecations,
+ withSilencedDeprecationsAsync,
} from "discourse-common/lib/deprecated";
import DeprecationCounter from "discourse/tests/helpers/deprecation-counter";
import { module, test } from "qunit";
@@ -175,4 +176,33 @@ module("Unit | Utility | deprecated", function (hooks) {
"counter is not incremented"
);
});
+
+ test("can silence deprecations with async callback in tests", async function (assert) {
+ await withSilencedDeprecationsAsync("discourse.one", async () => {
+ await Promise.resolve();
+ deprecated("message", { id: "discourse.one" });
+ });
+ assert.strictEqual(
+ this.warnStub.callCount,
+ 0,
+ "console.warn is not called"
+ );
+ assert.strictEqual(
+ this.counterStub.callCount,
+ 0,
+ "counter is not incremented"
+ );
+
+ deprecated("message", { id: "discourse.one" });
+ assert.strictEqual(
+ this.warnStub.callCount,
+ 1,
+ "console.warn is called outside the silenced function"
+ );
+ assert.strictEqual(
+ this.counterStub.callCount,
+ 1,
+ "counter is incremented outside the silenced function"
+ );
+ });
});