From 14270521ec4ffc98453603b5422758c2915795ea Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Sat, 10 Feb 2024 15:41:56 +0100 Subject: [PATCH] Renames and async import --- .../fake-clock-integration-test.js | 31 +++------ package.json | 1 + src/fake-timers-src.js | 31 +++++---- test/fake-timers-test.js | 38 +++++------ test/helpers/setup-tests.js | 65 +++++++------------ test/issue-1852-test.js | 4 +- test/issue-187-test.js | 2 +- test/issue-207-test.js | 2 +- test/issue-2086-test.js | 2 +- test/issue-2449-test.js | 2 +- test/issue-276-test.js | 2 +- test/issue-315-test.js | 2 +- test/issue-347-test.js | 4 +- test/issue-368-test.js | 6 +- test/issue-437-test.js | 4 +- test/issue-59-test.js | 2 +- test/issue-67-test.js | 4 +- test/issue-73-test.js | 4 +- 18 files changed, 78 insertions(+), 128 deletions(-) diff --git a/integration-test/fake-clock-integration-test.js b/integration-test/fake-clock-integration-test.js index cebfeea1..8d1eb131 100644 --- a/integration-test/fake-clock-integration-test.js +++ b/integration-test/fake-clock-integration-test.js @@ -1,25 +1,7 @@ -"use strict"; - -let jsdom; - -if (typeof require === "function" && typeof module === "object") { - try { - jsdom = require("jsdom"); - } catch (e) { - // ignored - } -} - -if (!jsdom) { - // eslint-disable-next-line no-console - console.warn("JSDOM is not supported in the current environment."); - - return; -} - -const assert = require("@sinonjs/referee-sinon").assert; -const FakeTimers = require("../src/fake-timers-src"); -const sinon = require("@sinonjs/referee-sinon").sinon; +import * as jsdom from "jsdom"; +import { assert } from "@sinonjs/referee-sinon"; +import * as FakeTimers from "../src/fake-timers-src.js"; +import { sinon } from "@sinonjs/referee-sinon"; describe("withGlobal", function () { let jsdomGlobal, withGlobal, timers; @@ -33,7 +15,10 @@ describe("withGlobal", function () { }); it("matches the normal FakeTimers API", function () { - assert.equals(Object.keys(withGlobal), Object.keys(FakeTimers)); + assert.equals( + Object.keys(withGlobal).sort(), + Object.keys(FakeTimers).sort(), + ); }); it("should support basic setTimeout", function () { diff --git a/package.json b/package.json index 288e892d..714d6fbd 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "type": "git", "url": "git+https://github.com/sinonjs/fake-timers.git" }, + "type": "module", "bugs": { "mail": "christian@cjohansen.no", "url": "https://github.com/sinonjs/fake-timers/issues" diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js index b2f7e8d6..7249b77b 100644 --- a/src/fake-timers-src.js +++ b/src/fake-timers-src.js @@ -1,19 +1,24 @@ -"use strict"; +const globalObject = (await import("@sinonjs/commons")).global; -const globalObject = require("@sinonjs/commons").global; -let timersModule, timersPromisesModule; -if (typeof require === "function" && typeof module === "object") { - try { - timersModule = require("timers"); - } catch (e) { - // ignored +async function tryImportModules() { + if (typeof module === "object") { + try { + timersModule = import("node:timers"); + } catch (e) { + // ignored + } } try { timersPromisesModule = require("timers/promises"); } catch (e) { // ignored } + try { + utilPromisify = (await import("util")).promisify; + } catch (err) {} } +let timersModule, timersPromisesModule, utilPromisify; +await tryImportModules(); /** * @typedef {object} IdleDeadline @@ -145,7 +150,7 @@ if (typeof require === "function" && typeof module === "object") { * @param {*} _global Namespace to mock (e.g. `window`) * @returns {FakeTimers} */ -function withGlobal(_global) { +export function withGlobal(_global) { const maxTimeout = Math.pow(2, 31) - 1; //see https://heycam.github.io/webidl/#abstract-opdef-converttoint const idCounterStart = 1e12; // arbitrarily large number to avoid collisions with native timer IDs const NOOP = function () { @@ -172,7 +177,6 @@ function withGlobal(_global) { isPresent.hrtime && typeof _global.process.hrtime.bigint === "function"; isPresent.nextTick = _global.process && typeof _global.process.nextTick === "function"; - const utilPromisify = _global.process && require("util").promisify; isPresent.performance = _global.performance && typeof _global.performance.now === "function"; const hasPerformancePrototype = @@ -2131,7 +2135,6 @@ function withGlobal(_global) { /** @type {FakeTimers} */ const defaultImplementation = withGlobal(globalObject); -exports.timers = defaultImplementation.timers; -exports.createClock = defaultImplementation.createClock; -exports.install = defaultImplementation.install; -exports.withGlobal = withGlobal; +export const timers = defaultImplementation.timers; +export const createClock = defaultImplementation.createClock; +export const install = defaultImplementation.install; diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 16019b24..a7bf418f 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -1,6 +1,4 @@ -"use strict"; - -const { +import { addTimerReturnsObject, assert, FakeTimers, @@ -19,7 +17,7 @@ const { sinon, utilPromisify, utilPromisifyAvailable, -} = require("./helpers/setup-tests"); +} from "./helpers/setup-tests.js"; let timersModule, timersPromisesModule; @@ -52,13 +50,13 @@ if (typeof require === "function" && typeof module === "object") { describe("FakeTimers", function () { describe("setTimeout", function () { + let evalCalled = false; beforeEach(function () { this.clock = FakeTimers.createClock(); - FakeTimers.evalCalled = false; }); afterEach(function () { - delete FakeTimers.evalCalled; + evalCalled = false; }); it("throws if no arguments", function () { @@ -107,20 +105,20 @@ describe("FakeTimers", function () { it("parses numeric string times", function () { this.clock.setTimeout(function () { - FakeTimers.evalCalled = true; + evalCalled = true; }, "10"); this.clock.tick(10); - assert(FakeTimers.evalCalled); + assert(evalCalled); }); it("parses no-numeric string times", function () { this.clock.setTimeout(function () { - FakeTimers.evalCalled = true; + evalCalled = true; }, "string"); this.clock.tick(10); - assert(FakeTimers.evalCalled); + assert(evalCalled); }); it("passes setTimeout parameters", function () { @@ -251,6 +249,7 @@ describe("FakeTimers", function () { }); describe("use of eval when not in node", function () { + let evalCalled; before(function () { if (addTimerReturnsObject) { this.skip(); @@ -259,18 +258,14 @@ describe("FakeTimers", function () { beforeEach(function () { this.clock = FakeTimers.createClock(); - FakeTimers.evalCalled = false; - }); - - afterEach(function () { - delete FakeTimers.evalCalled; + evalCalled = false; }); it("evals non-function callbacks", function () { - this.clock.setTimeout("FakeTimers.evalCalled = true", 10); + this.clock.setTimeout("evalCalled = true", 10); this.clock.tick(10); - assert(FakeTimers.evalCalled); + assert(evalCalled); }); it("only evals on global scope", function () { @@ -286,6 +281,7 @@ describe("FakeTimers", function () { }); describe("use of eval in node", function () { + let evalCalled = false; before(function () { if (!addTimerReturnsObject) { this.skip(); @@ -294,15 +290,11 @@ describe("FakeTimers", function () { beforeEach(function () { this.clock = FakeTimers.createClock(); - FakeTimers.evalCalled = false; - }); - - afterEach(function () { - delete FakeTimers.evalCalled; + evalCalled = false; }); it("does not eval non-function callbacks", function () { - const notTypeofFunction = "FakeTimers.evalCalled = true"; + const notTypeofFunction = "evalCalled = true"; assert.exception( function () { diff --git a/test/helpers/setup-tests.js b/test/helpers/setup-tests.js index 2cc0f566..abd220b0 100644 --- a/test/helpers/setup-tests.js +++ b/test/helpers/setup-tests.js @@ -1,12 +1,12 @@ -"use strict"; - /* * FIXME This is an interim hack to break a circular dependency between FakeTimers, * nise and sinon. * * 1. Load FakeTimers first, without defining global, verifying the ReferenceError is gone. */ -const FakeTimers = require("../../src/fake-timers-src"); +import * as FakeTimersModule from "../../src/fake-timers-src.js"; + +export const FakeTimers = FakeTimersModule; /* * 2. Define global, if missing. @@ -18,55 +18,34 @@ if (typeof global === "undefined") { /* * 3. Load sinon with global defined. */ -const assert = require("@sinonjs/referee-sinon").assert; -const refute = require("@sinonjs/referee-sinon").refute; -const sinon = require("@sinonjs/referee-sinon").sinon; +export { assert } from "@sinonjs/referee-sinon"; +export { refute } from "@sinonjs/referee-sinon"; +export { sinon } from "@sinonjs/referee-sinon"; -const globalObject = typeof global !== "undefined" ? global : window; +export const globalObject = typeof global !== "undefined" ? global : window; globalObject.FakeTimers = FakeTimers; // For testing eval -const GlobalDate = Date; +export const GlobalDate = Date; -const NOOP = function NOOP() { +export const NOOP = function NOOP() { return undefined; }; -const nextTickPresent = +export const nextTickPresent = global.process && typeof global.process.nextTick === "function"; -const queueMicrotaskPresent = typeof global.queueMicrotask === "function"; -const hrtimePresent = +export const queueMicrotaskPresent = + typeof global.queueMicrotask === "function"; +export const hrtimePresent = global.process && typeof global.process.hrtime === "function"; -const hrtimeBigintPresent = +export const hrtimeBigintPresent = hrtimePresent && typeof global.process.hrtime.bigint === "function"; -const performanceNowPresent = +export const performanceNowPresent = global.performance && typeof global.performance.now === "function"; -const performanceMarkPresent = +export const performanceMarkPresent = global.performance && typeof global.performance.mark === "function"; -const setImmediatePresent = +export const setImmediatePresent = global.setImmediate && typeof global.setImmediate === "function"; -const utilPromisify = global.process && require("util").promisify; -const promisePresent = typeof global.Promise !== "undefined"; -const utilPromisifyAvailable = promisePresent && utilPromisify; -const timeoutResult = global.setTimeout(NOOP, 0); -const addTimerReturnsObject = typeof timeoutResult === "object"; - -module.exports = { - FakeTimers, - assert, - refute, - sinon, - globalObject, - GlobalDate, - NOOP, - nextTickPresent, - queueMicrotaskPresent, - hrtimePresent, - hrtimeBigintPresent, - performanceNowPresent, - performanceMarkPresent, - setImmediatePresent, - utilPromisify, - promisePresent, - utilPromisifyAvailable, - timeoutResult, - addTimerReturnsObject, -}; +export const utilPromisify = global.process && (await import("util")).promisify; +export const promisePresent = typeof global.Promise !== "undefined"; +export const utilPromisifyAvailable = promisePresent && utilPromisify; +export const timeoutResult = global.setTimeout(NOOP, 0); +export const addTimerReturnsObject = typeof timeoutResult === "object"; diff --git a/test/issue-1852-test.js b/test/issue-1852-test.js index fff0813f..02ebf807 100644 --- a/test/issue-1852-test.js +++ b/test/issue-1852-test.js @@ -1,6 +1,4 @@ -"use strict"; - -const { FakeTimers, assert } = require("./helpers/setup-tests"); +import { FakeTimers, assert } from "./helpers/setup-tests.js"; describe("issue sinon#1852", function () { it("throws when creating a clock and global has no Date", function () { diff --git a/test/issue-187-test.js b/test/issue-187-test.js index ce458a52..cfe37a81 100644 --- a/test/issue-187-test.js +++ b/test/issue-187-test.js @@ -1,6 +1,6 @@ "use strict"; -const { sinon, FakeTimers, assert, NOOP } = require("./helpers/setup-tests"); +import { sinon, FakeTimers, assert, NOOP } from "./helpers/setup-tests.js"; describe("#187 - Support timeout.refresh in node environments", function () { it("calls the stub again after refreshing the timeout", function () { diff --git a/test/issue-207-test.js b/test/issue-207-test.js index 6fc98a8d..abddee59 100644 --- a/test/issue-207-test.js +++ b/test/issue-207-test.js @@ -1,6 +1,6 @@ "use strict"; -const { FakeTimers, assert, hrtimePresent } = require("./helpers/setup-tests"); +import { FakeTimers, assert, hrtimePresent } from "./helpers/setup-tests.js"; describe("issue #207 - nanosecond round-off errors on high-res timer", function () { let clock; diff --git a/test/issue-2086-test.js b/test/issue-2086-test.js index 852a8367..20dc8e10 100644 --- a/test/issue-2086-test.js +++ b/test/issue-2086-test.js @@ -1,6 +1,6 @@ "use strict"; -const { FakeTimers, assert } = require("./helpers/setup-tests"); +import { FakeTimers, assert } from "./helpers/setup-tests.js"; describe("issue #sinonjs/2086 - don't install setImmediate in unsupported environment", function () { let clock; diff --git a/test/issue-2449-test.js b/test/issue-2449-test.js index cf32d04f..85954e43 100644 --- a/test/issue-2449-test.js +++ b/test/issue-2449-test.js @@ -1,6 +1,6 @@ "use strict"; -const { sinon, FakeTimers, assert, refute } = require("./helpers/setup-tests"); +import { sinon, FakeTimers, assert, refute } from "./helpers/setup-tests.js"; describe("issue #2449: permanent loss of native functions", function () { it("should not fake faked timers", function () { diff --git a/test/issue-276-test.js b/test/issue-276-test.js index 3b7f8457..ebe0b41f 100644 --- a/test/issue-276-test.js +++ b/test/issue-276-test.js @@ -1,6 +1,6 @@ "use strict"; -const { FakeTimers, assert } = require("./helpers/setup-tests"); +import { FakeTimers, assert } from "./helpers/setup-tests.js"; describe("#276 - remove config.target", function () { it("should throw on using `config.target`", function () { diff --git a/test/issue-315-test.js b/test/issue-315-test.js index 67a9dedb..300e57f8 100644 --- a/test/issue-315-test.js +++ b/test/issue-315-test.js @@ -1,6 +1,6 @@ "use strict"; -const { sinon, FakeTimers, assert } = require("./helpers/setup-tests"); +import { sinon, FakeTimers, assert } from "./helpers/setup-tests.js"; describe("issue #315 - praseInt if delay is not a number", function () { it("should successfully execute the timer", function () { diff --git a/test/issue-347-test.js b/test/issue-347-test.js index 6ba84d83..5f6676de 100644 --- a/test/issue-347-test.js +++ b/test/issue-347-test.js @@ -1,12 +1,12 @@ "use strict"; -const { +import { FakeTimers, assert, utilPromisifyAvailable, utilPromisify, setImmediatePresent, -} = require("./helpers/setup-tests"); +} from "./helpers/setup-tests.js"; describe("#347 - Support util.promisify once installed", function () { before(function () { diff --git a/test/issue-368-test.js b/test/issue-368-test.js index fe62a46c..5d08a3b4 100644 --- a/test/issue-368-test.js +++ b/test/issue-368-test.js @@ -1,11 +1,9 @@ -"use strict"; - -const { +import { sinon, FakeTimers, assert, addTimerReturnsObject, -} = require("./helpers/setup-tests"); +} from "./helpers/setup-tests.js"; describe("#368 - timeout.refresh setTimeout arguments", function () { before(function () { diff --git a/test/issue-437-test.js b/test/issue-437-test.js index 43d9d4c4..f2632d9c 100644 --- a/test/issue-437-test.js +++ b/test/issue-437-test.js @@ -1,6 +1,4 @@ -"use strict"; - -const { FakeTimers, assert } = require("./helpers/setup-tests"); +import { FakeTimers, assert } from "./helpers/setup-tests.js"; describe("issue #437", function () { it("should save methods of subclass instance", function () { diff --git a/test/issue-59-test.js b/test/issue-59-test.js index c8e30ef9..bd5feac4 100644 --- a/test/issue-59-test.js +++ b/test/issue-59-test.js @@ -1,6 +1,6 @@ "use strict"; -const { sinon, FakeTimers, assert, NOOP } = require("./helpers/setup-tests"); +import { sinon, FakeTimers, assert, NOOP } from "./helpers/setup-tests.js"; describe("issue #59", function () { it("should install and uninstall the clock on a custom target", function () { diff --git a/test/issue-67-test.js b/test/issue-67-test.js index 6d30617f..3687ea68 100644 --- a/test/issue-67-test.js +++ b/test/issue-67-test.js @@ -1,6 +1,4 @@ -"use strict"; - -const { sinon, FakeTimers, assert } = require("./helpers/setup-tests"); +import { sinon, FakeTimers, assert } from "./helpers/setup-tests.js"; describe("issue #67", function () { // see https://nodejs.org/api/timers.html diff --git a/test/issue-73-test.js b/test/issue-73-test.js index 41e22b6b..df282c60 100644 --- a/test/issue-73-test.js +++ b/test/issue-73-test.js @@ -1,6 +1,4 @@ -"use strict"; - -const { FakeTimers, assert } = require("./helpers/setup-tests"); +import { FakeTimers, assert } from "./helpers/setup-tests.js"; describe("issue #73", function () { it("should install with date object", function () {