UNPKG

review-copilot

Version:

ReviewCopilot - AI-powered code review assistant with customizable prompts

111 lines (110 loc) 4.03 kB
"use strict"; 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 }); jest.mock('fs/promises', () => ({ writeFile: jest.fn(), })); jest.mock('ora', () => { const spinner = { start: jest.fn().mockReturnThis(), succeed: jest.fn(), fail: jest.fn(), stop: jest.fn(), text: '', }; return () => spinner; }); const init_1 = require("../init"); const fs_1 = __importDefault(require("fs")); const fsPromises = __importStar(require("fs/promises")); const path_1 = __importDefault(require("path")); const OLD_ENV = process.env; describe('initCommand', () => { let existsSyncSpy; let consoleLogSpy; let consoleErrorSpy; beforeAll(() => { consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { }); consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { }); }); afterAll(() => { consoleErrorSpy.mockRestore(); consoleLogSpy.mockRestore(); jest.restoreAllMocks(); }); beforeEach(() => { jest.resetModules(); jest.clearAllMocks(); existsSyncSpy = jest.spyOn(fs_1.default, 'existsSync'); jest.spyOn(fsPromises, 'writeFile').mockResolvedValue(undefined); jest .spyOn(path_1.default, 'join') .mockImplementation((...args) => args.join('/')); process.env = { ...OLD_ENV }; }); afterEach(() => { jest.clearAllTimers(); jest.clearAllMocks(); process.env = OLD_ENV; }); it('should use existing config file if present', async () => { existsSyncSpy.mockReturnValue(true); await (0, init_1.initCommand)(); expect(fsPromises.writeFile).not.toHaveBeenCalled(); }); it('should create new config file if not present', async () => { existsSyncSpy.mockReturnValue(false); await (0, init_1.initCommand)(); expect(fsPromises.writeFile).toHaveBeenCalled(); expect(consoleLogSpy).toHaveBeenCalled(); }); it('should handle errors and exit process', async () => { existsSyncSpy.mockReturnValue(false); fsPromises.writeFile.mockRejectedValue(new Error('fail')); process.env.NODE_ENV = 'test'; await expect(async () => { try { await (0, init_1.initCommand)(); } catch (error) { expect(error.message).toBe('process.exit'); throw error; } }).rejects.toThrow('process.exit'); }); });