Skip to content

Commit

Permalink
test: replace esmock with sinon (#117)
Browse files Browse the repository at this point in the history
* test: replace esmock with sinon

* import `fs` statically
  • Loading branch information
fasttime authored May 17, 2024
1 parent 61a385e commit 3987d0f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package-lock=false
node-options=--loader=esmock
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 25 additions & 25 deletions tests/utils/npm-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3987d0f

Please sign in to comment.