Skip to content

Commit

Permalink
[INTERNAL] Add tests for NodePackageDependencies changes, adjust erro…
Browse files Browse the repository at this point in the history
…r message
  • Loading branch information
matz3 committed Aug 24, 2023
1 parent 15a589d commit ce260fd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/graph/providers/NodePackageDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class NodePackageDependencies {

const modulePath = path.dirname(rootPkg.path);
if (!rootPkg.packageJson.name) {
throw new Error(`Missing 'name' attribute in package.json at ${modulePath}`);
throw new Error(`Missing or empty 'name' attribute in package.json at ${modulePath}`);
}
if (!rootPkg.packageJson.version) {
throw new Error(`Missing 'version' attribute in package.json at ${modulePath}`);
throw new Error(`Missing or empty 'version' attribute in package.json at ${modulePath}`);
}

return {
Expand Down
54 changes: 54 additions & 0 deletions test/lib/graph/providers/NodePackageDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from "ava";
import sinonGlobal from "sinon";
import esmock from "esmock";

test.beforeEach(async (t) => {
const sinon = t.context.sinon = sinonGlobal.createSandbox();

t.context.readPackageUp = sinon.stub();

t.context.NodePackageDependencies = await esmock("../../../../lib/graph/providers/NodePackageDependencies.js", {
"read-pkg-up": {
readPackageUp: t.context.readPackageUp
}
});
});

test.afterEach.always((t) => {
t.context.sinon.restore();
});

test("getRootNode should reject with error when 'name' is empty/missing in package.json", async (t) => {
const {NodePackageDependencies, readPackageUp} = t.context;

const resolver = new NodePackageDependencies({cwd: "cwd"});

readPackageUp.resolves({
path: "/path/to/root/package.json",
packageJson: {
name: ""
}
});

await t.throwsAsync(() => resolver.getRootNode(), {
message: "Missing or empty 'name' attribute in package.json at /path/to/root"
});
});

test("getRootNode should reject with error when 'version' is empty/missing in package.json", async (t) => {
const {NodePackageDependencies, readPackageUp} = t.context;

const resolver = new NodePackageDependencies({cwd: "cwd"});

readPackageUp.resolves({
path: "/path/to/root/package.json",
packageJson: {
name: "test-package-name",
version: ""
}
});

await t.throwsAsync(() => resolver.getRootNode(), {
message: "Missing or empty 'version' attribute in package.json at /path/to/root"
});
});

0 comments on commit ce260fd

Please sign in to comment.