next-affected
Version:
CLI tool to list Next.js pages affected by changes
96 lines (95 loc) • 4.36 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const config_1 = require("../modules/config"); // Adjust import path accordingly
jest.mock("fs");
jest.mock("path");
describe("initConfig", () => {
const defaultConfig = {
pagesDirectories: ["pages", "src/pages", "app", "src/app"],
excludedExtensions: [".css", ".scss", ".less", ".svg", ".png", ".jpg"],
excludedPaths: [],
};
it("should not create the config file if it already exists", () => {
fs.existsSync.mockReturnValue(true);
const consoleSpy = jest.spyOn(console, "log");
(0, config_1.initConfig)();
expect(fs.existsSync).toHaveBeenCalledWith("next-affected.config.json");
expect(consoleSpy).toHaveBeenCalledWith("next-affected.config.json already exists.");
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
it("should create the config file if it does not exist", () => {
fs.existsSync.mockReturnValue(false);
const consoleSpy = jest.spyOn(console, "log");
(0, config_1.initConfig)();
expect(fs.existsSync).toHaveBeenCalledWith("next-affected.config.json");
expect(fs.writeFileSync).toHaveBeenCalledWith("next-affected.config.json", JSON.stringify(defaultConfig, null, 2));
expect(consoleSpy).toHaveBeenCalledWith("Created next-affected.config.json with default settings.");
});
});
describe("loadConfig", () => {
const mockProjectDir = "/path/to/project";
const configFileName = "next-affected.config.json";
const configFilePath = path_1.default.join(mockProjectDir, configFileName);
const defaultConfig = {
pagesDirectories: ["pages", "src/pages", "app", "src/app"],
excludedExtensions: [".css", ".scss", ".less", ".svg", ".png", ".jpg"],
excludedPaths: [],
};
beforeEach(() => {
jest.clearAllMocks();
});
it("should load the config file if it exists", () => {
const mockConfigContent = JSON.stringify(defaultConfig);
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(mockConfigContent);
const config = (0, config_1.loadConfig)(mockProjectDir);
expect(fs.existsSync).toHaveBeenCalledWith(configFilePath);
expect(fs.readFileSync).toHaveBeenCalledWith(configFilePath, "utf-8");
expect(config).toEqual(defaultConfig);
});
it("should return the default config if the config file does not exist", () => {
fs.existsSync.mockReturnValue(false);
const config = (0, config_1.loadConfig)(mockProjectDir);
expect(fs.existsSync).toHaveBeenCalledWith(configFilePath);
expect(fs.readFileSync).not.toHaveBeenCalled();
expect(config).toEqual(defaultConfig);
});
});