From 7980c4183228577d861d3c36ac7cb14cb3fd9081 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Wed, 27 Jul 2022 22:54:22 +0200 Subject: [PATCH] DEV: Fix fake-timer issues (#17681) Occasionally some code (e.g. live-reload) would try to clear a timer that was set up before fake timers were installed. That would lead to issues and warnings. Enabling `shouldClearNativeTimers` option fixes it. --- .../discourse/tests/helpers/qunit-helpers.js | 1 + .../discourse/tests/unit/lib/formatter-test.js | 18 ++++++++++-------- .../unit/models/topic-tracking-state-test.js | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index 3846497dd78..84989cb4b02 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -107,6 +107,7 @@ export function fakeTime(timeString, timezone = null, advanceTime = false) { return sinon.useFakeTimers({ now: now.valueOf(), shouldAdvanceTime: advanceTime, + shouldClearNativeTimers: true, }); } diff --git a/app/assets/javascripts/discourse/tests/unit/lib/formatter-test.js b/app/assets/javascripts/discourse/tests/unit/lib/formatter-test.js index d9570d31e03..cb5c111f9d9 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/formatter-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/formatter-test.js @@ -7,8 +7,10 @@ import { relativeAge, updateRelativeAge, } from "discourse/lib/formatter"; -import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; -import sinon from "sinon"; +import { + discourseModule, + fakeTime, +} from "discourse/tests/helpers/qunit-helpers"; import { test } from "qunit"; import domFromString from "discourse-common/lib/dom-from-string"; @@ -44,14 +46,14 @@ function strip(html) { discourseModule("Unit | Utility | formatter", function (hooks) { hooks.beforeEach(function () { - this.clock = sinon.useFakeTimers(new Date(2012, 11, 31, 12, 0).getTime()); + this.clock = fakeTime("2012-12-31 12:00"); }); hooks.afterEach(function () { this.clock.restore(); }); - test("formating medium length dates", function (assert) { + test("formatting medium length dates", function (assert) { let shortDateYear = shortDateTester("MMM D, 'YY"); assert.strictEqual( @@ -133,7 +135,7 @@ discourseModule("Unit | Utility | formatter", function (hooks) { ); this.clock.restore(); - this.clock = sinon.useFakeTimers(new Date(2012, 0, 9, 12, 0).getTime()); // Jan 9, 2012 + this.clock = fakeTime("2012-01-09 12:00"); assert.strictEqual( strip(formatDays(8, { format: "medium" })), @@ -145,7 +147,7 @@ discourseModule("Unit | Utility | formatter", function (hooks) { ); }); - test("formating tiny dates", function (assert) { + test("formatting tiny dates", function (assert) { let shortDateYear = shortDateTester("MMM 'YY"); assert.strictEqual(formatMins(0), "1m"); @@ -207,7 +209,7 @@ discourseModule("Unit | Utility | formatter", function (hooks) { this.siteSettings.relative_date_duration = 14; this.clock.restore(); - this.clock = sinon.useFakeTimers(new Date(2012, 0, 12, 12, 0).getTime()); // Jan 12, 2012 + this.clock = fakeTime("2012-01-12 12:00"); assert.strictEqual(formatDays(11), "11d"); assert.strictEqual(formatDays(14), "14d"); @@ -215,7 +217,7 @@ discourseModule("Unit | Utility | formatter", function (hooks) { assert.strictEqual(formatDays(366), shortDateYear(366)); this.clock.restore(); - this.clock = sinon.useFakeTimers(new Date(2012, 0, 20, 12, 0).getTime()); // Jan 20, 2012 + this.clock = fakeTime("2012-01-20 12:00"); assert.strictEqual(formatDays(14), "14d"); assert.strictEqual(formatDays(15), shortDate(15)); diff --git a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js index e3f24b1950c..4ae475f5d87 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js @@ -5,6 +5,7 @@ import Category from "discourse/models/category"; import MessageBus from "message-bus-client"; import { discourseModule, + fakeTime, publishToMessageBus, } from "discourse/tests/helpers/qunit-helpers"; import { NotificationLevels } from "discourse/lib/notification-levels"; @@ -16,7 +17,7 @@ import sinon from "sinon"; discourseModule("Unit | Model | topic-tracking-state", function (hooks) { hooks.beforeEach(function () { - this.clock = sinon.useFakeTimers(new Date(2012, 11, 31, 12, 0).getTime()); + this.clock = fakeTime("2012-12-31 12:00"); }); hooks.afterEach(function () {