Skip to content

Commit 3987d0f

Browse files
authored
test: replace esmock with sinon (#117)
* test: replace esmock with sinon * import `fs` statically
1 parent 61a385e commit 3987d0f

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

.npmrc

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
package-lock=false
2-
node-options=--loader=esmock

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"eslint": "^9.1.1",
4949
"eslint-config-eslint": "^10.0.0",
5050
"eslint-release": "^3.2.0",
51-
"esmock": "^2.5.8",
5251
"lint-staged": "^12.1.2",
5352
"memfs": "^3.4.0",
5453
"sinon": "^12.0.1",

tests/utils/npm-utils.spec.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,28 @@ import {
1313
installSyncSaveDev,
1414
fetchPeerDependencies,
1515
checkDeps,
16-
checkDevDeps
16+
checkDevDeps,
17+
checkPackageJson
1718
} from "../../lib/utils/npm-utils.js";
1819
import { defineInMemoryFs } from "../_utils/in-memory-fs.js";
19-
import esmock from "esmock";
2020
import { assert, describe, afterEach, it } from "vitest";
21+
import fs from "fs";
2122

2223
//------------------------------------------------------------------------------
2324
// Helpers
2425
//------------------------------------------------------------------------------
2526

2627
/**
27-
* Import `npm-utils` with the in-memory file system.
28+
* Replace native fs methods with the in-memory file system methods used by `npm-utils`.
2829
* @param {Object} files The file definitions.
29-
* @returns {Object} `npm-utils`.
30+
* @returns {void}
3031
*/
31-
async function requireNpmUtilsWithInMemoryFileSystem(files) {
32-
const fs = defineInMemoryFs({ files });
32+
async function useInMemoryFileSystem(files) {
33+
const inMemoryFs = defineInMemoryFs({ files });
3334

34-
return await esmock("../../lib/utils/npm-utils.js", { fs });
35+
sinon.replace(fs, "readFileSync", inMemoryFs.readFileSync);
36+
sinon.replace(fs, "existsSync", inMemoryFs.existsSync);
37+
sinon.replace(fs, "statSync", inMemoryFs.statSync);
3538
}
3639

3740
//------------------------------------------------------------------------------
@@ -68,21 +71,21 @@ describe("npmUtils", () => {
6871
});
6972

7073
it("should handle missing devDependencies key", async () => {
71-
const { checkDevDeps: stubcheckDevDeps } = await requireNpmUtilsWithInMemoryFileSystem({
74+
await useInMemoryFileSystem({
7275
"package.json": JSON.stringify({ private: true, dependencies: {} })
7376
});
7477

7578
// Should not throw.
76-
stubcheckDevDeps(["some-package"]);
79+
checkDevDeps(["some-package"]);
7780
});
7881

7982
it("should throw with message when parsing invalid package.json", async () => {
80-
const { checkDevDeps: stubcheckDevDeps } = await requireNpmUtilsWithInMemoryFileSystem({
83+
await useInMemoryFileSystem({
8184
"package.json": '{ "not: "valid json" }'
8285
});
8386

8487
assert.throws(() => {
85-
stubcheckDevDeps(["some-package"]);
88+
checkDevDeps(["some-package"]);
8689
}, /JSON/u);
8790
});
8891
});
@@ -118,38 +121,38 @@ describe("npmUtils", () => {
118121
});
119122

120123
it("should handle missing dependencies key", async () => {
121-
const { checkDeps: stubbedcheckDeps } = await requireNpmUtilsWithInMemoryFileSystem({
124+
await useInMemoryFileSystem({
122125
"package.json": JSON.stringify({ private: true, devDependencies: {} })
123126
});
124127

125128
// Should not throw.
126-
stubbedcheckDeps(["some-package"]);
129+
checkDeps(["some-package"]);
127130
});
128131

129132
it("should throw with message when parsing invalid package.json", async () => {
130-
const { checkDeps: stubbedcheckDeps } = await requireNpmUtilsWithInMemoryFileSystem({
133+
await useInMemoryFileSystem({
131134
"package.json": '{ "not: "valid json" }'
132135
});
133136

134137
assert.throws(() => {
135-
stubbedcheckDeps(["some-package"]);
138+
checkDeps(["some-package"]);
136139
}, /JSON/u);
137140
});
138141
});
139142

140143
describe("checkPackageJson()", () => {
141144
it("should return true if package.json exists", async () => {
142-
const { checkPackageJson: stubbedcheckPackageJson } = await requireNpmUtilsWithInMemoryFileSystem({
145+
await useInMemoryFileSystem({
143146
"package.json": '{ "file": "contents" }'
144147
});
145148

146-
assert.strictEqual(stubbedcheckPackageJson(), true);
149+
assert.strictEqual(checkPackageJson(), true);
147150
});
148151

149152
it("should return false if package.json does not exist", async () => {
150-
const { checkPackageJson: stubbedcheckPackageJson } = await requireNpmUtilsWithInMemoryFileSystem({});
153+
await useInMemoryFileSystem({});
151154

152-
assert.strictEqual(stubbedcheckPackageJson(), false);
155+
assert.strictEqual(checkPackageJson(), false);
153156
});
154157
});
155158

@@ -188,14 +191,11 @@ describe("npmUtils", () => {
188191
it("should log an error message if npm throws ENOENT error", async () => {
189192
const logErrorStub = sinon.spy();
190193
const npmUtilsStub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } });
194+
const log = await import("../../lib/utils/logging.js");
191195

192-
const { installSyncSaveDev: stubinstallSyncSaveDev } = await esmock("../../lib/utils/npm-utils.js", {
193-
"../../lib/utils/logging.js": {
194-
error: logErrorStub
195-
}
196-
});
196+
sinon.replaceGetter(log, "error", () => logErrorStub);
197197

198-
stubinstallSyncSaveDev("some-package");
198+
installSyncSaveDev("some-package");
199199

200200
assert(logErrorStub.calledOnce);
201201

0 commit comments

Comments
 (0)