From cce77da925f4c6b0da44c49a233228845e870cd6 Mon Sep 17 00:00:00 2001 From: arun-357 Date: Sun, 14 Jun 2026 19:53:05 +0530 Subject: [PATCH] chore: convert lib/utils.js to ESM --- lib/cli/cli.mjs | 2 +- lib/cli/collect-files.js | 2 +- lib/cli/config.js | 2 +- lib/cli/options.mjs | 2 +- lib/cli/run.js | 2 +- lib/cli/watch-run.js | 2 +- lib/errors.mjs | 2 +- lib/hook.mjs | 2 +- lib/mocha.js | 2 +- lib/nodejs/parallel-buffered-runner.js | 2 +- lib/nodejs/serializer.mjs | 2 +- lib/plugin-loader.mjs | 2 +- lib/reporters/base.mjs | 2 +- lib/reporters/doc.mjs | 2 +- lib/reporters/html.mjs | 2 +- lib/reporters/json.mjs | 2 +- lib/reporters/markdown.mjs | 2 +- lib/reporters/xunit.mjs | 2 +- lib/runnable.mjs | 2 +- lib/runner.js | 2 +- lib/suite.mjs | 2 +- lib/test.mjs | 2 +- lib/{utils.js => utils.mjs} | 124 +++++++++--------- test/node-unit/mocha.spec.js | 2 +- .../parallel-buffered-runner.spec.js | 2 +- test/node-unit/stack-trace-filter.spec.js | 2 +- test/node-unit/utils.spec.js | 2 +- test/reporters/json.spec.mjs | 17 ++- test/unit/mocha.spec.js | 16 ++- test/unit/utils.spec.js | 2 +- 30 files changed, 109 insertions(+), 102 deletions(-) rename lib/{utils.js => utils.mjs} (90%) diff --git a/lib/cli/cli.mjs b/lib/cli/cli.mjs index 37e9e60952..ca14c20e28 100755 --- a/lib/cli/cli.mjs +++ b/lib/cli/cli.mjs @@ -13,7 +13,7 @@ import { fileURLToPath } from "node:url"; import { loadOptions, YARGS_PARSER_CONFIG } from "./options.mjs"; import { run, init } from "./commands.mjs"; import pc from "picocolors"; -import { logSymbols } from "../utils.js"; +import { logSymbols } from "../utils.mjs"; import packageJson from "../../package.json" with { type: "json" }; diff --git a/lib/cli/collect-files.js b/lib/cli/collect-files.js index 592b7234ae..019c5f4e21 100644 --- a/lib/cli/collect-files.js +++ b/lib/cli/collect-files.js @@ -6,7 +6,7 @@ const debug = require("debug")("mocha:cli:run:helpers"); const { minimatch } = require("minimatch"); const { NO_FILES_MATCH_PATTERN } = require("../error-constants.mjs").constants; const { lookupFiles } = require("./lookup-files.mjs"); -const { castArray } = require("../utils"); +const { castArray } = require("../utils.mjs"); /** * Exports a function that collects test files from CLI parameters. diff --git a/lib/cli/config.js b/lib/cli/config.js index 678e06c775..0a553eb40f 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -12,7 +12,7 @@ const path = require("node:path"); const debug = require("debug")("mocha:cli:config"); const findUp = require("find-up"); const { createUnparsableFileError } = require("../errors.mjs"); -const utils = require("../utils"); +const utils = require("../utils.mjs"); /** * These are the valid config files, in order of precedence; diff --git a/lib/cli/options.mjs b/lib/cli/options.mjs index 144297dba7..77268d06d5 100644 --- a/lib/cli/options.mjs +++ b/lib/cli/options.mjs @@ -27,7 +27,7 @@ import { createInvalidArgumentTypeError, createUnsupportedError, } from "../errors.mjs"; -import { isNumeric } from "../utils.js"; +import { isNumeric } from "../utils.mjs"; /** * The `yargs-parser` namespace diff --git a/lib/cli/run.js b/lib/cli/run.js index bdca79b44c..8e1d8e336f 100644 --- a/lib/cli/run.js +++ b/lib/cli/run.js @@ -25,7 +25,7 @@ const { ONE_AND_DONES, ONE_AND_DONE_ARGS } = require("./one-and-dones.mjs"); const debug = require("debug")("mocha:cli:run"); const defaults = require("../mocharc.json"); const { types, aliases } = require("./run-option-metadata.mjs"); -const { isCI, logSymbols } = require("../utils"); +const { isCI, logSymbols } = require("../utils.mjs"); /** * Logical option groups diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js index 495d1e2877..b689ab149f 100644 --- a/lib/cli/watch-run.js +++ b/lib/cli/watch-run.js @@ -8,7 +8,7 @@ const isPathInside = require("is-path-inside"); const { minimatch } = require("minimatch"); const Context = require("../context.mjs").Context; const collectFiles = require("./collect-files"); -const { logSymbols } = require("../utils"); +const { logSymbols } = require("../utils.mjs"); /** * @typedef {import('chokidar').FSWatcher} FSWatcher diff --git a/lib/errors.mjs b/lib/errors.mjs index 3f3ae05677..d3b7e52099 100644 --- a/lib/errors.mjs +++ b/lib/errors.mjs @@ -7,7 +7,7 @@ import { format } from "node:util"; import { constants } from "./error-constants.mjs"; -import { isCI } from "./utils.js"; +import { isCI } from "./utils.mjs"; /** * Contains error codes and factory functions to create throwable error objects diff --git a/lib/hook.mjs b/lib/hook.mjs index 776a1b9931..43a0a384c4 100644 --- a/lib/hook.mjs +++ b/lib/hook.mjs @@ -1,5 +1,5 @@ import { Runnable } from "./runnable.mjs"; -import utils from "./utils.js"; +import * as utils from "./utils.mjs"; const { MOCHA_ID_PROP_NAME } = utils.constants; diff --git a/lib/mocha.js b/lib/mocha.js index 0de11dca34..3635a18960 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -9,7 +9,7 @@ var { escapeRegExp } = require("./utils/regexp.mjs"); var path = require("node:path"); var builtinReporters = require("./reporters"); -var utils = require("./utils"); +var utils = require("./utils.mjs"); var mocharc = require("./mocharc.json"); var { Suite } = require("./suite.mjs"); var esmUtils = require("./nodejs/esm-utils"); diff --git a/lib/nodejs/parallel-buffered-runner.js b/lib/nodejs/parallel-buffered-runner.js index 9d5e1b7201..c215da5a36 100644 --- a/lib/nodejs/parallel-buffered-runner.js +++ b/lib/nodejs/parallel-buffered-runner.js @@ -18,7 +18,7 @@ const { EVENT_RUN_BEGIN, EVENT_RUN_END } = Runner.constants; const debug = require("debug")("mocha:parallel:parallel-buffered-runner"); const { BufferedWorkerPool } = require("./buffered-worker-pool"); const { setInterval, clearInterval } = global; -const { createMap, constants } = require("../utils"); +const { createMap, constants } = require("../utils.mjs"); const { MOCHA_ID_PROP_NAME } = constants; const { createFatalError } = require("../errors.mjs"); diff --git a/lib/nodejs/serializer.mjs b/lib/nodejs/serializer.mjs index ee6a84acf3..fbcadaffa4 100644 --- a/lib/nodejs/serializer.mjs +++ b/lib/nodejs/serializer.mjs @@ -9,7 +9,7 @@ * @typedef {import('../types.d.ts').SerializedWorkerResult} SerializedWorkerResult */ -import { type, breakCircularDeps } from "../utils.js"; +import { type, breakCircularDeps } from "../utils.mjs"; import { createInvalidArgumentTypeError } from "../errors.mjs"; import d from "debug"; diff --git a/lib/plugin-loader.mjs b/lib/plugin-loader.mjs index 0bf903c02f..d160f81ee4 100644 --- a/lib/plugin-loader.mjs +++ b/lib/plugin-loader.mjs @@ -18,7 +18,7 @@ import { createInvalidPluginDefinitionError, createInvalidPluginImplementationError, } from "./errors.mjs"; -import { castArray } from "./utils.js"; +import { castArray } from "./utils.mjs"; const debug = debugModule("mocha:plugin-loader"); /** diff --git a/lib/reporters/base.mjs b/lib/reporters/base.mjs index c4f3ff8852..9d26e0e87a 100644 --- a/lib/reporters/base.mjs +++ b/lib/reporters/base.mjs @@ -13,7 +13,7 @@ import * as diff from "diff"; import milliseconds from "ms"; -import utils from "../utils.js"; +import * as utils from "../utils.mjs"; import supportsColor from "supports-color"; import Runner from "../runner.js"; diff --git a/lib/reporters/doc.mjs b/lib/reporters/doc.mjs index 5a2a8039c8..1ebe2b206e 100644 --- a/lib/reporters/doc.mjs +++ b/lib/reporters/doc.mjs @@ -10,7 +10,7 @@ */ import { Base } from "./base.mjs"; -import utils from "../utils.js"; +import * as utils from "../utils.mjs"; import Runner from "../runner.js"; var constants = Runner.constants; var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; diff --git a/lib/reporters/html.mjs b/lib/reporters/html.mjs index 2917cde7f8..a83e817ba7 100644 --- a/lib/reporters/html.mjs +++ b/lib/reporters/html.mjs @@ -10,7 +10,7 @@ */ import { Base } from "./base.mjs"; -import utils from "../utils.js"; +import * as utils from "../utils.mjs"; import { escapeRegExp } from "../utils/regexp.mjs"; import Runner from "../runner.js"; var constants = Runner.constants; diff --git a/lib/reporters/json.mjs b/lib/reporters/json.mjs index e2dedf9c88..cac4f41c16 100644 --- a/lib/reporters/json.mjs +++ b/lib/reporters/json.mjs @@ -13,7 +13,7 @@ import { Base } from "./base.mjs"; import fs from "node:fs"; import path from "node:path"; import * as errors from "../errors.mjs"; -import utils from "../utils.js"; +import * as utils from "../utils.mjs"; import Runner from "../runner.js"; const createUnsupportedError = errors.createUnsupportedError; var constants = Runner.constants; diff --git a/lib/reporters/markdown.mjs b/lib/reporters/markdown.mjs index b6047aafee..fb047617be 100644 --- a/lib/reporters/markdown.mjs +++ b/lib/reporters/markdown.mjs @@ -10,7 +10,7 @@ */ import { Base } from "./base.mjs"; -import utils from "../utils.js"; +import * as utils from "../utils.mjs"; import Runner from "../runner.js"; var constants = Runner.constants; var EVENT_RUN_END = constants.EVENT_RUN_END; diff --git a/lib/reporters/xunit.mjs b/lib/reporters/xunit.mjs index 802bbf26c2..70f7a83a33 100644 --- a/lib/reporters/xunit.mjs +++ b/lib/reporters/xunit.mjs @@ -11,7 +11,7 @@ */ import { Base } from "./base.mjs"; -import utils from "../utils.js"; +import * as utils from "../utils.mjs"; import fs from "node:fs"; import path from "node:path"; import * as errors from "../errors.mjs"; diff --git a/lib/runnable.mjs b/lib/runnable.mjs index 0d83917fb7..fc743da05e 100644 --- a/lib/runnable.mjs +++ b/lib/runnable.mjs @@ -2,7 +2,7 @@ import { EventEmitter } from "node:events"; import { PendingError } from "./pending.mjs"; import debugModule from "debug"; import milliseconds from "ms"; -import utils from "./utils.js"; +import * as utils from "./utils.mjs"; import { createInvalidExceptionError, createMultipleDoneError, diff --git a/lib/runner.js b/lib/runner.js index dfa5f37375..c944f5ce77 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -10,7 +10,7 @@ */ var EventEmitter = require("node:events").EventEmitter; var { PendingError } = require("./pending.mjs"); -var utils = require("./utils"); +var utils = require("./utils.mjs"); var debug = require("debug")("mocha:runner"); var { Runnable } = require("./runnable.mjs"); var { Suite } = require("./suite.mjs"); diff --git a/lib/suite.mjs b/lib/suite.mjs index 50b5e49459..073b5d7e8f 100644 --- a/lib/suite.mjs +++ b/lib/suite.mjs @@ -8,7 +8,7 @@ */ import { EventEmitter } from "node:events"; import { Hook } from "./hook.mjs"; -import utils from "./utils.js"; +import * as utils from "./utils.mjs"; import debugModule from "debug"; import milliseconds from "ms"; import * as errors from "./errors.mjs"; diff --git a/lib/test.mjs b/lib/test.mjs index 2152002720..60fc9ac385 100644 --- a/lib/test.mjs +++ b/lib/test.mjs @@ -1,5 +1,5 @@ import { Runnable } from "./runnable.mjs"; -import utils from "./utils.js"; +import * as utils from "./utils.mjs"; import { createInvalidArgumentTypeError } from "./errors.mjs"; const { isString } = utils; diff --git a/lib/utils.js b/lib/utils.mjs similarity index 90% rename from lib/utils.js rename to lib/utils.mjs index c2129fecf0..942d2de82d 100644 --- a/lib/utils.js +++ b/lib/utils.mjs @@ -1,5 +1,3 @@ -"use strict"; - /** * Various utility functions used throughout Mocha's codebase. * @module utils @@ -8,10 +6,12 @@ /** * Module dependencies. */ -var path = require("node:path"); -var he = require("he"); -var pc = require("picocolors"); -var isUnicodeSupported = require("is-unicode-supported")(); +import path from "node:path"; +import he from "he"; +import pc from "picocolors"; +import isUnicodeSupportedCheck from "is-unicode-supported"; + +const isUnicodeSupported = isUnicodeSupportedCheck(); const MOCHA_ID_PROP_NAME = "__mocha_id__"; @@ -22,9 +22,9 @@ const MOCHA_ID_PROP_NAME = "__mocha_id__"; * @param {string} html * @return {string} */ -exports.escape = function (html) { +export function escape(html) { return he.encode(String(html), { useNamedReferences: false }); -}; +} /** * Test if the given obj is type of string. @@ -33,9 +33,9 @@ exports.escape = function (html) { * @param {Object} obj * @return {boolean} */ -exports.isString = function (obj) { +export function isString(obj) { return typeof obj === "string"; -}; +} /** * Compute a slug from the given `str`. @@ -44,13 +44,13 @@ exports.isString = function (obj) { * @param {string} str * @return {string} */ -exports.slug = function (str) { +export function slug(str) { return str .toLowerCase() .replace(/\s+/g, "-") .replace(/[^-\w]/g, "") .replace(/-{2,}/g, "-"); -}; +} /** * Strip the function definition from `str`, and re-indent for pre whitespace. @@ -58,7 +58,7 @@ exports.slug = function (str) { * @param {string} str * @return {string} */ -exports.clean = function (str) { +export function clean(str) { str = str .replace(/\r\n?|[\n\u2028\u2029]/g, "\n") .replace(/^\uFEFF/, "") @@ -78,7 +78,7 @@ exports.clean = function (str) { str = str.replace(re, ""); return str.trim(); -}; +} /** * If a value could have properties, and has none, this function is called, @@ -131,7 +131,7 @@ function emptyRepresentation(value, typeHint) { * canonicalType(async function() {}) // 'asyncfunction' * canonicalType(Object.create(null)) // 'null-prototype' */ -var canonicalType = (exports.canonicalType = function canonicalType(value) { +export function canonicalType(value) { if (value === undefined) { return "undefined"; } else if (value === null) { @@ -146,7 +146,7 @@ var canonicalType = (exports.canonicalType = function canonicalType(value) { .call(value) .replace(/^\[.+\s(.+?)]$/, "$1") .toLowerCase(); -}); +} /** * @@ -168,7 +168,7 @@ var canonicalType = (exports.canonicalType = function canonicalType(value) { * type(global) // 'object' * type(new String('foo') // 'string' */ -exports.type = function type(value) { +export function type(value) { // Null is special if (value === null) return "null"; const primitives = new Set([ @@ -187,7 +187,7 @@ exports.type = function type(value) { if (Array.isArray(value)) return "array"; return _type; -}; +} /** * Stringify `value`. Different behavior depending on type of value: @@ -196,15 +196,15 @@ exports.type = function type(value) { * - If `value` is not an object, function or array, return result of `value.toString()` wrapped in double-quotes. * - If `value` is an *empty* object, function, or array, return result of function * {@link emptyRepresentation}. - * - If `value` has properties, call {@link exports.canonicalize} on it, then return result of + * - If `value` has properties, call {@link canonicalize} on it, then return result of * JSON.stringify(). * * @private - * @see exports.type + * @see type * @param {*} value * @return {string} */ -exports.stringify = function (value) { +export function stringify(value) { var typeHint = canonicalType(value); if (!~["object", "array", "function", "null-prototype"].indexOf(typeHint)) { @@ -231,15 +231,15 @@ exports.stringify = function (value) { for (var prop in value) { if (Object.prototype.hasOwnProperty.call(value, prop)) { - return jsonStringify( - exports.canonicalize(value, null, typeHint), - 2, - ).replace(/,(\n|$)/g, "$1"); + return jsonStringify(canonicalize(value, null, typeHint), 2).replace( + /,(\n|$)/g, + "$1", + ); } } return emptyRepresentation(value, typeHint); -}; +} /** * like JSON.stringify but more sense. @@ -338,13 +338,13 @@ function jsonStringify(object, spaces, depth) { * - is an empty `Array`, `Object`, or `Function`, return the result of calling `emptyRepresentation()` * * @private - * @see {@link exports.stringify} + * @see {@link stringify} * @param {*} value Thing to inspect. May or may not have properties. * @param {Array} [stack=[]] Stack of seen values * @param {string} [typeHint] Type hint * @return {(Object|Array|Function|string|undefined)} */ -exports.canonicalize = function canonicalize(value, stack, typeHint) { +export function canonicalize(value, stack, typeHint) { var canonicalizedObj; var prop; @@ -371,7 +371,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) { case "array": withStack(value, function () { canonicalizedObj = value.map(function (item) { - return exports.canonicalize(item, stack); + return canonicalize(item, stack); }); }); break; @@ -397,7 +397,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) { Object.keys(value) .sort() .forEach(function (key) { - canonicalizedObj[key] = exports.canonicalize(value[key], stack); + canonicalizedObj[key] = canonicalize(value[key], stack); }); }); break; @@ -413,7 +413,7 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) { } return canonicalizedObj; -}; +} /** * @summary @@ -424,15 +424,15 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) { * (i.e: strip Mocha and internal node functions from stack trace). * @returns {Function} */ -exports.stackTraceFilter = function () { +export function stackTraceFilter() { // TODO: Replace with `process.browser` var is = typeof document === "undefined" ? { node: true } : { browser: true }; var slash = path.sep; - var cwd; + var cwdPath; if (is.node) { - cwd = exports.cwd() + slash; + cwdPath = cwd() + slash; } else { - cwd = ( + cwdPath = ( typeof location === "undefined" ? window.location : location ).href.replace(/\/[^/]*$/, "/"); slash = "/"; @@ -471,7 +471,7 @@ exports.stackTraceFilter = function () { // Clean up cwd(absolute) if (/:\d+:\d+\)?$/.test(line)) { - line = line.replace("(" + cwd, "("); + line = line.replace("(" + cwdPath, "("); } list.push(line); @@ -480,7 +480,7 @@ exports.stackTraceFilter = function () { return stack.join("\n"); }; -}; +} /** * Crude, but effective. @@ -488,13 +488,13 @@ exports.stackTraceFilter = function () { * @param {*} value * @returns {boolean} Whether or not `value` is a Promise */ -exports.isPromise = function isPromise(value) { +export function isPromise(value) { return ( typeof value === "object" && value !== null && typeof value.then === "function" ); -}; +} /** * Clamps a numeric value to an inclusive range. @@ -503,15 +503,15 @@ exports.isPromise = function isPromise(value) { * @param {number[]} range - Two element array specifying [min, max] range. * @returns {number} clamped value */ -exports.clamp = function clamp(value, range) { +export function clamp(value, range) { return Math.min(Math.max(value, range[0]), range[1]); -}; +} /** * It's a noop. * @public */ -exports.noop = function () {}; +export function noop() {} /** * Creates a map-like object. @@ -528,12 +528,12 @@ exports.noop = function () {}; * @param {...*} [obj] - Arguments to `Object.assign()`. * @returns {Object} An object with no prototype, having `...obj` properties */ -exports.createMap = function () { +export function createMap() { return Object.assign.apply( null, [Object.create(null)].concat(Array.prototype.slice.call(arguments)), ); -}; +} /** * Creates a read-only map-like object. @@ -547,12 +547,12 @@ exports.createMap = function () { * @returns {Object} A frozen object with no prototype, having `...obj` properties * @throws {TypeError} if argument is not a non-empty object. */ -exports.defineConstants = function (obj) { +export function defineConstants(obj) { if (canonicalType(obj) !== "object" || !Object.keys(obj).length) { throw new TypeError("Invalid argument; expected a non-empty object"); } - return Object.freeze(exports.createMap(obj)); -}; + return Object.freeze(createMap(obj)); +} /** * Returns current working directory @@ -560,9 +560,9 @@ exports.defineConstants = function (obj) { * Wrapper around `process.cwd()` for isolation * @private */ -exports.cwd = function cwd() { +export function cwd() { return process.cwd(); -}; +} /** * Returns `true` if Mocha is running in a browser. @@ -570,9 +570,9 @@ exports.cwd = function cwd() { * @returns {boolean} * @private */ -exports.isBrowser = function isBrowser() { +export function isBrowser() { return Boolean(process.browser); -}; +} /* * Casts `value` to an array; useful for optionally accepting array parameters @@ -585,7 +585,7 @@ exports.isBrowser = function isBrowser() { * @param {*} value - Something to cast to an Array * @returns {Array<*>} */ -exports.castArray = function castArray(value) { +export function castArray(value) { if (value === undefined) { return []; } @@ -599,9 +599,9 @@ exports.castArray = function castArray(value) { return Array.from(value); } return [value]; -}; +} -exports.constants = exports.defineConstants({ +export const constants = defineConstants({ MOCHA_ID_PROP_NAME, }); @@ -614,7 +614,7 @@ const uniqueIDBase = * Trivial copy of nanoid/non-secure * @returns {string} Unique identifier */ -exports.uniqueID = () => { +export const uniqueID = () => { let id = ""; for (let i = 0; i < 21; i++) { id += uniqueIDBase[(Math.random() * 64) | 0]; @@ -622,8 +622,8 @@ exports.uniqueID = () => { return id; }; -exports.assignNewMochaID = (obj) => { - const id = exports.uniqueID(); +export const assignNewMochaID = (obj) => { + const id = uniqueID(); Object.defineProperty(obj, MOCHA_ID_PROP_NAME, { get() { return id; @@ -637,7 +637,7 @@ exports.assignNewMochaID = (obj) => { * @param {*} [obj] - Object * @returns {string|void} */ -exports.getMochaID = (obj) => +export const getMochaID = (obj) => obj && typeof obj === "object" ? obj[MOCHA_ID_PROP_NAME] : undefined; /** @@ -646,7 +646,7 @@ exports.getMochaID = (obj) => * @param inputObj {*} * @returns {*} */ -exports.breakCircularDeps = (inputObj) => { +export const breakCircularDeps = (inputObj) => { const seen = new Set(); function _breakCircularDeps(obj) { @@ -678,7 +678,7 @@ exports.breakCircularDeps = (inputObj) => { /** * Checks if provided input can be parsed as a JavaScript Number. */ -exports.isNumeric = (input) => { +export const isNumeric = (input) => { return !isNaN(parseFloat(input)); }; @@ -692,11 +692,11 @@ exports.isNumeric = (input) => { * CircleCI: https://circleci.com/docs/reference/variables/#built-in-environment-variables * Bitbucket: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/ */ -exports.isCI = () => { +export const isCI = () => { return !!process.env.CI; }; -exports.logSymbols = { +export const logSymbols = { info: pc.blue(isUnicodeSupported ? "ℹ" : "i"), success: pc.green(isUnicodeSupported ? "✔" : "√"), warning: pc.yellow(isUnicodeSupported ? "⚠" : "‼"), diff --git a/test/node-unit/mocha.spec.js b/test/node-unit/mocha.spec.js index fb12b5a791..143c8aeb8d 100644 --- a/test/node-unit/mocha.spec.js +++ b/test/node-unit/mocha.spec.js @@ -64,7 +64,7 @@ describe("Mocha", function () { Mocha = rewiremock.proxy( () => require("../../lib/mocha"), (r) => ({ - "../../lib/utils.js": r.with(stubs.utils).callThrough(), + "../../lib/utils.mjs": r.with(stubs.utils).callThrough(), "../../lib/suite.mjs": { Suite: stubs.Suite }, "../../lib/nodejs/parallel-buffered-runner.js": stubs.ParallelBufferedRunner, diff --git a/test/node-unit/parallel-buffered-runner.spec.js b/test/node-unit/parallel-buffered-runner.spec.js index 9813c5a170..34e4e38366 100644 --- a/test/node-unit/parallel-buffered-runner.spec.js +++ b/test/node-unit/parallel-buffered-runner.spec.js @@ -11,7 +11,7 @@ const rewiremock = require("rewiremock/node"); const { Suite } = require("../../lib/suite.mjs"); const Runner = require("../../lib/runner"); const sinon = require("sinon"); -const { constants } = require("../../lib/utils"); +const { constants } = require("../../lib/utils.mjs"); const { MOCHA_ID_PROP_NAME } = constants; describe("parallel-buffered-runner", function () { diff --git a/test/node-unit/stack-trace-filter.spec.js b/test/node-unit/stack-trace-filter.spec.js index c9e98b54a4..b9ee702274 100644 --- a/test/node-unit/stack-trace-filter.spec.js +++ b/test/node-unit/stack-trace-filter.spec.js @@ -1,7 +1,7 @@ "use strict"; var path = require("node:path"); -var utils = require("../../lib/utils"); +var utils = require("../../lib/utils.mjs"); describe("stackTraceFilter()", function () { describe("on node", function () { diff --git a/test/node-unit/utils.spec.js b/test/node-unit/utils.spec.js index 2ff4e1880f..704caa13ce 100644 --- a/test/node-unit/utils.spec.js +++ b/test/node-unit/utils.spec.js @@ -7,7 +7,7 @@ describe("utils", function () { beforeEach(function () { // add deps to be mocked as needed to second parameter - utils = rewiremock.proxy("../../lib/utils", {}); + utils = rewiremock.proxy("../../lib/utils.mjs", {}); }); describe("function", function () { diff --git a/test/reporters/json.spec.mjs b/test/reporters/json.spec.mjs index 097cef48b2..6d94401954 100644 --- a/test/reporters/json.spec.mjs +++ b/test/reporters/json.spec.mjs @@ -1,7 +1,6 @@ import fs from "node:fs"; import sinon from "sinon"; import { JSONReporter } from "../../lib/reporters/json.mjs"; -import utils from "../../lib/utils.js"; import { Mocha, Suite, Runner, Test } from "../../index.mjs"; describe("JSON reporter", function () { @@ -217,12 +216,16 @@ describe("JSON reporter", function () { }); it('should throw "unsupported error" in browser', function () { - sinon.stub(utils, "isBrowser").callsFake(() => true); - expect( - () => new JSONReporter(runner, options), - "to throw", - "file output not supported in browser", - ); + process.browser = true; + try { + expect( + () => new JSONReporter(runner, options), + "to throw", + "file output not supported in browser", + ); + } finally { + delete process.browser; + } }); }); }); diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index a2efc50539..d4bff413dd 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -3,7 +3,6 @@ var sinon = require("sinon"); var EventEmitter = require("node:events").EventEmitter; var Mocha = require("../../lib/mocha"); -var utils = require("../../lib/utils"); describe("Mocha", function () { /** @@ -76,9 +75,6 @@ describe("Mocha", function () { }; Suite = sinon.stub(Mocha, "Suite").returns(suite); Suite.constants = {}; - - sinon.stub(utils, "isString"); - sinon.stub(utils, "noop"); }); afterEach(function () { @@ -1001,7 +997,11 @@ describe("Mocha", function () { describe("parallelMode()", function () { describe("when `Mocha` is running in a browser", function () { beforeEach(function () { - sinon.stub(utils, "isBrowser").returns(true); + process.browser = true; + }); + + afterEach(function () { + delete process.browser; }); it("should throw", function () { @@ -1019,7 +1019,11 @@ describe("Mocha", function () { describe("unloadFile()", function () { describe("when run in a browser", function () { beforeEach(function () { - sinon.stub(utils, "isBrowser").returns(true); + process.browser = true; + }); + + afterEach(function () { + delete process.browser; }); it("should throw", function () { diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index cbe3b262b2..c38f80eeea 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -1,7 +1,7 @@ /* global BigInt */ "use strict"; -var utils = require("../../lib/utils"); +var utils = require("../../lib/utils.mjs"); var sinon = require("sinon"); describe("lib/utils", function () {