Skip to content

Commit

Permalink
Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tobysmith568 committed Jun 22, 2024
1 parent 35572e7 commit 99abff5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import { Dependency, LicenseContent } from "../../../src/lib/internal/resolveLic
import { PackageJson } from "../../../src/lib/utils/packageJson.utils";
import { join } from "path";
import { doesFileExist, readFile } from "../../../src/lib/utils/file.utils";
import logger from "../../../src/lib/utils/console.utils";

jest.mock("@npmcli/arborist", () => ({
__esModule: true,
default: jest.fn(),
}));

jest.mock("../../../src/lib/utils/file.utils");
jest.mock("../../../src/lib/utils/console.utils");

jest.mock("../../../src/lib/internal/resolveLicenseContent", () => ({
resolveLicenseContent: jest.fn(),
}));

describe("resolveNpmDependencies", () => {
const mockedLogger = jest.mocked(logger);
const mockedReadFile = jest.mocked(readFile);
const mockedDoesFileExist = jest.mocked(doesFileExist);

Expand Down Expand Up @@ -258,6 +261,22 @@ describe("resolveNpmDependencies", () => {
expect(replacements3).toBe(replacements);
});

it.each([new Error("Something went wrong"), "Something went wrong"])(
"should warning log if resolveLicenseContent throws an error",
async error => {
when(mockedResolveLicenseContent)
.calledWith(child1Realpath, expect.anything(), expect.anything())
.mockRejectedValue(error);

await resolveDependenciesForNpmProject("/some/path/package.json", new Map());

expect(mockedLogger.warn).toHaveBeenCalledTimes(1);
expect(mockedLogger.warn).toHaveBeenCalledWith(
`Unable to determine license content for ${child1Name}@${child1Version} with error:\nSomething went wrong\n`,
);
},
);

describe("when no options are provided", () => {
it("should include non-dev dependencies in the result", async () => {
const licensesMap = new Map<LicenseContent, Dependency[]>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { resolveLicenseContent } from "../../../src/lib/internal/resolveLicenseC
import { when } from "jest-when";
import { doesFileExist, readFile } from "../../../src/lib/utils/file.utils";
import { PackageJson } from "../../../src/lib/utils/packageJson.utils";
import logger from "../../../src/lib/utils/console.utils";
import { join } from "path";
import { Dependency, LicenseContent } from "../../../src/lib/internal/resolveLicenses";

Expand All @@ -17,6 +18,7 @@ jest.mock("../../../src/lib/utils/pnpmCli.utils", () => ({
}));

jest.mock("../../../src/lib/utils/file.utils");
jest.mock("../../../src/lib/utils/console.utils");

jest.mock("../../../src/lib/internal/resolveLicenseContent", () => ({
resolveLicenseContent: jest.fn(),
Expand All @@ -40,6 +42,7 @@ describe("resolveDependenciesForPnpmProject", () => {
paths: ["/some/path/dependency3"],
};

const mockedLogger = jest.mocked(logger);
const mockedReadFile = jest.mocked(readFile);
const mockedDoesFileExist = jest.mocked(doesFileExist);
const mockedGetPnpmVersion = jest.mocked(getPnpmVersion);
Expand Down Expand Up @@ -201,6 +204,26 @@ describe("resolveDependenciesForPnpmProject", () => {
).toBeDefined();
});

it.each([new Error("Something went wrong"), "Something went wrong"])(
"should warning log if resolveLicenseContent throws an error",
async error => {
mockedGetPnpmVersion.mockResolvedValue(pnpmVersion);
mockedGetPnpmProjectDependencies.mockResolvedValue([dependency1, dependency2, dependency3]);

when(mockedResolveLicenseContent)
.calledWith(dependency1.paths[0], expect.anything(), expect.anything())
.mockRejectedValue(error);

const licensesMap = new Map<LicenseContent, Dependency[]>();

await resolveDependenciesForPnpmProject("/some/path/package.json", licensesMap);

expect(mockedLogger.warn).toHaveBeenCalledWith(
`Unable to determine license content for ${dependency1.name}@1.0.0 with error:\nSomething went wrong\n`,
);
},
);

describe("when the dependency is in the exclude list", () => {
it("should not call resolveLicenseContent", async () => {
mockedGetPnpmVersion.mockResolvedValue(pnpmVersion);
Expand Down

0 comments on commit 99abff5

Please sign in to comment.