diff --git a/.npmrc b/.npmrc index 30866b50..43c97e71 100644 --- a/.npmrc +++ b/.npmrc @@ -1,2 +1 @@ package-lock=false -node-options=--loader=esmock diff --git a/package.json b/package.json index 9eec50ca..f1d21125 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "eslint": "^9.1.1", "eslint-config-eslint": "^10.0.0", "eslint-release": "^3.2.0", - "esmock": "^2.5.8", "lint-staged": "^12.1.2", "memfs": "^3.4.0", "sinon": "^12.0.1", diff --git a/tests/utils/npm-utils.spec.js b/tests/utils/npm-utils.spec.js index 1cb89837..80daf728 100644 --- a/tests/utils/npm-utils.spec.js +++ b/tests/utils/npm-utils.spec.js @@ -13,25 +13,28 @@ import { installSyncSaveDev, fetchPeerDependencies, checkDeps, - checkDevDeps + checkDevDeps, + checkPackageJson } from "../../lib/utils/npm-utils.js"; import { defineInMemoryFs } from "../_utils/in-memory-fs.js"; -import esmock from "esmock"; import { assert, describe, afterEach, it } from "vitest"; +import fs from "fs"; //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** - * Import `npm-utils` with the in-memory file system. + * Replace native fs methods with the in-memory file system methods used by `npm-utils`. * @param {Object} files The file definitions. - * @returns {Object} `npm-utils`. + * @returns {void} */ -async function requireNpmUtilsWithInMemoryFileSystem(files) { - const fs = defineInMemoryFs({ files }); +async function useInMemoryFileSystem(files) { + const inMemoryFs = defineInMemoryFs({ files }); - return await esmock("../../lib/utils/npm-utils.js", { fs }); + sinon.replace(fs, "readFileSync", inMemoryFs.readFileSync); + sinon.replace(fs, "existsSync", inMemoryFs.existsSync); + sinon.replace(fs, "statSync", inMemoryFs.statSync); } //------------------------------------------------------------------------------ @@ -68,21 +71,21 @@ describe("npmUtils", () => { }); it("should handle missing devDependencies key", async () => { - const { checkDevDeps: stubcheckDevDeps } = await requireNpmUtilsWithInMemoryFileSystem({ + await useInMemoryFileSystem({ "package.json": JSON.stringify({ private: true, dependencies: {} }) }); // Should not throw. - stubcheckDevDeps(["some-package"]); + checkDevDeps(["some-package"]); }); it("should throw with message when parsing invalid package.json", async () => { - const { checkDevDeps: stubcheckDevDeps } = await requireNpmUtilsWithInMemoryFileSystem({ + await useInMemoryFileSystem({ "package.json": '{ "not: "valid json" }' }); assert.throws(() => { - stubcheckDevDeps(["some-package"]); + checkDevDeps(["some-package"]); }, /JSON/u); }); }); @@ -118,38 +121,38 @@ describe("npmUtils", () => { }); it("should handle missing dependencies key", async () => { - const { checkDeps: stubbedcheckDeps } = await requireNpmUtilsWithInMemoryFileSystem({ + await useInMemoryFileSystem({ "package.json": JSON.stringify({ private: true, devDependencies: {} }) }); // Should not throw. - stubbedcheckDeps(["some-package"]); + checkDeps(["some-package"]); }); it("should throw with message when parsing invalid package.json", async () => { - const { checkDeps: stubbedcheckDeps } = await requireNpmUtilsWithInMemoryFileSystem({ + await useInMemoryFileSystem({ "package.json": '{ "not: "valid json" }' }); assert.throws(() => { - stubbedcheckDeps(["some-package"]); + checkDeps(["some-package"]); }, /JSON/u); }); }); describe("checkPackageJson()", () => { it("should return true if package.json exists", async () => { - const { checkPackageJson: stubbedcheckPackageJson } = await requireNpmUtilsWithInMemoryFileSystem({ + await useInMemoryFileSystem({ "package.json": '{ "file": "contents" }' }); - assert.strictEqual(stubbedcheckPackageJson(), true); + assert.strictEqual(checkPackageJson(), true); }); it("should return false if package.json does not exist", async () => { - const { checkPackageJson: stubbedcheckPackageJson } = await requireNpmUtilsWithInMemoryFileSystem({}); + await useInMemoryFileSystem({}); - assert.strictEqual(stubbedcheckPackageJson(), false); + assert.strictEqual(checkPackageJson(), false); }); }); @@ -188,14 +191,11 @@ describe("npmUtils", () => { it("should log an error message if npm throws ENOENT error", async () => { const logErrorStub = sinon.spy(); const npmUtilsStub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } }); + const log = await import("../../lib/utils/logging.js"); - const { installSyncSaveDev: stubinstallSyncSaveDev } = await esmock("../../lib/utils/npm-utils.js", { - "../../lib/utils/logging.js": { - error: logErrorStub - } - }); + sinon.replaceGetter(log, "error", () => logErrorStub); - stubinstallSyncSaveDev("some-package"); + installSyncSaveDev("some-package"); assert(logErrorStub.calledOnce);