UNPKG

next-affected

Version:

CLI tool to list Next.js pages affected by changes

125 lines (124 loc) 5.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const config_1 = require("./config"); const git_1 = require("./git"); const graph_1 = require("./graph"); const run_1 = require("./run"); // Mocking external dependencies jest.mock("path", () => ({ resolve: jest.fn(), relative: jest.fn(), })); jest.mock("./config", () => ({ loadConfig: jest.fn(), })); jest.mock("./git", () => ({ getChangedFiles: jest.fn(), })); jest.mock("./graph", () => ({ findAffectedPages: jest.fn(), getDependencyGraph: jest.fn(), })); // Now use jest.mocked to infer the mock type const mockedPath = jest.mocked(path_1.default); const mockedLoadConfig = jest.mocked(config_1.loadConfig); const mockedFindAffectedPages = jest.mocked(graph_1.findAffectedPages); const mockedGetDependencyGraph = jest.mocked(graph_1.getDependencyGraph); // Mocking external dependencies jest.mock("path", () => ({ resolve: jest.fn(), relative: jest.fn(), })); jest.mock("./config", () => ({ loadConfig: jest.fn(), })); jest.mock("./git", () => ({ getChangedFiles: jest.fn(), })); jest.mock("./graph", () => ({ findAffectedPages: jest.fn(), getDependencyGraph: jest.fn(), })); describe("runNextAffected", () => { const options = { project: "testProject", base: "main", head: "HEAD", depth: 2, verbose: false, }; beforeAll(() => { jest.spyOn(global.console, "log").mockImplementation(() => jest.fn()); jest.spyOn(process.stdout, "write").mockImplementation(() => true); }); beforeEach(() => { jest.clearAllMocks(); mockedPath.resolve.mockReturnValue("/resolved/path"); mockedPath.relative.mockReturnValue("relative/path"); mockedLoadConfig.mockReturnValue({ pagesDirectories: ["pages"], excludedExtensions: [".css"], excludedPaths: [], }); mockedGetDependencyGraph.mockResolvedValue({ moduleA: [], moduleB: [], }); mockedFindAffectedPages.mockReturnValue(["/page1", "/page2"]); }); it("should resolve project directory and load configuration", async () => { git_1.getChangedFiles.mockReturnValue(["file1.js", "file2.js"]); await (0, run_1.runNextAffected)(undefined, options); expect(path_1.default.resolve).toHaveBeenCalledWith(process.cwd(), options.project); expect(config_1.loadConfig).toHaveBeenCalledWith("/resolved/path"); }); it("should process affected pages when componentPath is provided", async () => { await (0, run_1.runNextAffected)("component/path", options); expect(path_1.default.resolve).toHaveBeenCalledWith(process.cwd(), "component/path"); expect(graph_1.findAffectedPages).toHaveBeenCalledWith(expect.any(Object), // dependency graph "relative/path", "/resolved/path", expect.any(Object), // config options.depth, options.verbose, expect.any(Function) // processedModules callback ); }); it("should process affected files when base option is provided", async () => { git_1.getChangedFiles.mockReturnValue(["file1.js", "file2.js"]); await (0, run_1.runNextAffected)(undefined, { ...options, base: "main" }); expect(git_1.getChangedFiles).toHaveBeenCalledWith({ base: "main", head: "HEAD", includeUncommitted: false, onlyUncommitted: false, projectDir: "/resolved/path", }); expect(graph_1.findAffectedPages).toHaveBeenCalledWith(expect.any(Object), // dependency graph "file1.js", "/resolved/path", expect.any(Object), // config options.depth, options.verbose, expect.any(Function) // processedModules callback ); expect(graph_1.findAffectedPages).toHaveBeenCalledWith(expect.any(Object), // dependency graph "file2.js", "/resolved/path", expect.any(Object), // config options.depth, options.verbose, expect.any(Function) // processedModules callback ); }); it("should log affected pages when found", async () => { console.log = jest.fn(); graph_1.findAffectedPages.mockReturnValue(["/page1", "/page2"]); await (0, run_1.runNextAffected)("component/path", options); expect(console.log).toHaveBeenCalledWith("\nAffected Pages:"); expect(console.log).toHaveBeenCalledWith("/page1"); expect(console.log).toHaveBeenCalledWith("/page2"); }); it("should handle errors and log the error message", async () => { const error = new Error("Test error"); graph_1.findAffectedPages.mockImplementation(() => { throw error; }); console.error = jest.fn(); await (0, run_1.runNextAffected)("component/path", options); expect(console.error).toHaveBeenCalledWith("Error:", error.message); expect(console.error).toHaveBeenCalledWith(error); }); });