Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to ESM #501

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions integration-test/fake-clock-integration-test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"type": "git",
"url": "git+https://github.com/sinonjs/fake-timers.git"
},
"type": "module",
"bugs": {
"mail": "[email protected]",
"url": "https://github.com/sinonjs/fake-timers/issues"
Expand Down
31 changes: 17 additions & 14 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 =
Expand Down Expand Up @@ -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;
38 changes: 15 additions & 23 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const {
import {
addTimerReturnsObject,
assert,
FakeTimers,
Expand All @@ -19,7 +17,7 @@ const {
sinon,
utilPromisify,
utilPromisifyAvailable,
} = require("./helpers/setup-tests");
} from "./helpers/setup-tests.js";

let timersModule, timersPromisesModule;

Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -251,6 +249,7 @@ describe("FakeTimers", function () {
});

describe("use of eval when not in node", function () {
let evalCalled;
before(function () {
if (addTimerReturnsObject) {
this.skip();
Expand All @@ -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 () {
Expand All @@ -286,6 +281,7 @@ describe("FakeTimers", function () {
});

describe("use of eval in node", function () {
let evalCalled = false;
before(function () {
if (!addTimerReturnsObject) {
this.skip();
Expand All @@ -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 () {
Expand Down
65 changes: 22 additions & 43 deletions test/helpers/setup-tests.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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";
4 changes: 1 addition & 3 deletions test/issue-1852-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion test/issue-187-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion test/issue-207-test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/issue-2086-test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/issue-2449-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion test/issue-276-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion test/issue-315-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
4 changes: 2 additions & 2 deletions test/issue-347-test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
Loading
Loading