Skip to content

Commit

Permalink
test(mama): mock natively instead of sinon
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly committed Jan 30, 2025
1 parent a7540e0 commit 6ea69d2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 198 deletions.
142 changes: 5 additions & 137 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
"@types/node": "^22.0.0",
"@types/pacote": "^11.1.8",
"@types/semver": "^7.5.8",
"@types/sinon": "^17.0.3",
"c8": "^10.1.2",
"glob": "^11.0.0",
"pkg-ok": "^3.0.0",
"sinon": "^18.0.0",
"tsx": "^4.16.2",
"typescript": "^5.5.3"
},
Expand Down
100 changes: 41 additions & 59 deletions workspaces/mama/test/ManifestManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
import fs from "node:fs/promises";
import path from "node:path";
import assert from "node:assert";
import { describe, it, test } from "node:test";
import { describe, it, test, type TestContext } from "node:test";

// Import Third-party Dependencies
import type {
PackageJSON,
WorkspacesPackageJSON
} from "@nodesecure/npm-types";
import hash from "object-hash";
import sinon from "sinon";

// Import Internal Dependencies
import { ManifestManager } from "../src/index.js";
Expand Down Expand Up @@ -39,90 +38,73 @@ describe("ManifestManager", () => {

describe("static fromPackageJSON()", () => {
test(`Given a location equal to process.cwd(),
it should read and parse the JSON from filesystem`, async() => {
const readFile = sinon
.stub(fs, "readFile")
.resolves(JSON.stringify(kMinimalPackageJSON));
it should read and parse the JSON from filesystem`, async(t) => {
const readfile = t.mock.method(fs, "readFile", async() => JSON.stringify(kMinimalPackageJSON));

try {
const location = process.cwd();
const mama = await ManifestManager.fromPackageJSON(
location
);
const location = process.cwd();
const mama = await ManifestManager.fromPackageJSON(
location
);

assert.ok(readFile.calledOnce);
assert.ok(mama instanceof ManifestManager);
assert.equal(readfile.mock.callCount(), 1);
assert.ok(mama instanceof ManifestManager);

assert.strictEqual(
mama.isWorkspace,
false
);
assert.strictEqual(
mama.spec,
`${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}`
);
}
finally {
readFile.restore();
}
assert.strictEqual(
mama.isWorkspace,
false
);
assert.strictEqual(
mama.spec,
`${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}`
);
});

test(`Given a location equal to process.cwd() + package.json,
it should read and parse the JSON from filesystem`, async() => {
const readFile = sinon
.stub(fs, "readFile")
.resolves(JSON.stringify(kMinimalPackageJSON));
it should read and parse the JSON from filesystem`, async(t) => {
const readFile = t.mock.method(fs, "readFile", async() => JSON.stringify(kMinimalPackageJSON));

try {
const location = path.join(process.cwd(), "package.json");
const mama = await ManifestManager.fromPackageJSON(
location
);
const location = path.join(process.cwd(), "package.json");
const mama = await ManifestManager.fromPackageJSON(
location
);

assert.ok(readFile.calledOnce);
assert.ok(mama instanceof ManifestManager);
assert.equal(readFile.mock.callCount(), 1);
assert.ok(mama instanceof ManifestManager);

assert.strictEqual(
mama.isWorkspace,
false
);
assert.strictEqual(
mama.spec,
`${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}`
);
}
finally {
readFile.restore();
}
assert.strictEqual(
mama.isWorkspace,
false
);
assert.strictEqual(
mama.spec,
`${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}`
);
});

test("Given an invalid JSON, it should throw a custom Error with the parsing error as a cause", async(t) => {
test("Given an invalid JSON, it should throw a custom Error with the parsing error as a cause", async(t: TestContext) => {
const location = process.cwd();
const expectedLocation = path.join(location, "package.json");

const readFile = sinon
.stub(fs, "readFile")
.resolves(`{ foo: NaN }`);
t.after(() => readFile.restore());
// TODO: add t.plan(5) when available in LTS version of test_runner
const readFile = t.mock.method(fs, "readFile", async() => `{ foo: NaN }`);
t.plan(5);

try {
await ManifestManager.fromPackageJSON(
process.cwd()
);
}
catch (error) {
assert.strictEqual(error.name, "Error");
assert.strictEqual(
t.assert.strictEqual(error.name, "Error");
t.assert.strictEqual(
error.message,
`Failed to parse package.json located at: ${expectedLocation}`
);

assert.ok("cause" in error);
assert.strictEqual(error.cause.name, "SyntaxError");
t.assert.ok("cause" in error);
t.assert.strictEqual(error.cause.name, "SyntaxError");
}

assert.ok(readFile.calledOnce);
t.assert.equal(readFile.mock.callCount(), 1);
});

test("Given the location argument is not a string, it must throw a TypeError", async() => {
Expand Down

0 comments on commit 6ea69d2

Please sign in to comment.