UNPKG

@mbc-cqrs-serverless/cli

Version:

a CLI to get started with MBC CQRS serverless framework

85 lines (84 loc) 4.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const commander_1 = require("commander"); const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const new_action_1 = __importDefault(require("./new.action")); jest.mock('child_process', () => ({ execSync: jest.fn(), })); jest.mock('fs', () => ({ copyFileSync: jest.fn(), cpSync: jest.fn(), mkdirSync: jest.fn(), unlinkSync: jest.fn(), writeFileSync: jest.fn(), readFileSync: jest.fn(() => JSON.stringify({ version: '1.2.3', dependencies: {}, devDependencies: {} })), })); describe('newAction', () => { const mockExecSync = child_process_1.execSync; const mockCommand = new commander_1.Command().name('new'); // Mock command with name 'new' beforeEach(() => { jest.clearAllMocks(); }); it('should generate a project with the latest version when version is not specified', async () => { const projectName = 'test-project'; const latestVersion = '1.2.3'; mockExecSync .mockReturnValueOnce(Buffer.from(latestVersion)) // latest core .mockReturnValueOnce(Buffer.from(latestVersion)) // latest cli .mockReturnValue(Buffer.from('')); await (0, new_action_1.default)(`${projectName}`, {}, mockCommand); expect(child_process_1.execSync).toHaveBeenCalledWith('npm view @mbc-cqrs-serverless/core dist-tags.latest'); expect(fs_1.mkdirSync).toHaveBeenCalledWith(path_1.default.join(process.cwd(), projectName), { recursive: true }); expect(fs_1.cpSync).toHaveBeenCalledWith(path_1.default.join(__dirname, '../../templates'), path_1.default.join(process.cwd(), projectName), { recursive: true }); expect(fs_1.copyFileSync).toHaveBeenCalledTimes(3); // For .gitignore, infra/.gitignore and .env.local expect(mockExecSync).toHaveBeenCalledWith('git init', { cwd: path_1.default.join(process.cwd(), projectName), }); expect(mockExecSync).toHaveBeenCalledWith('npm i --ignore-scripts', { cwd: path_1.default.join(process.cwd(), projectName), }); expect(mockExecSync).toHaveBeenCalledWith('npx prisma generate', { cwd: path_1.default.join(process.cwd(), projectName), }); }); it('should use a specific version if specified', async () => { const projectName = 'test-project'; const version = '1.0.0'; const mockVersions = ['1.0.0', '1.1.0', '1.2.0']; mockExecSync .mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))) // list version core .mockReturnValueOnce(Buffer.from('1.2.3')) // latest cli .mockReturnValue(Buffer.from('')); await (0, new_action_1.default)(`${projectName}@${version}`, {}, mockCommand); expect(child_process_1.execSync).toHaveBeenCalledWith('npm view @mbc-cqrs-serverless/core versions --json'); expect(fs_1.mkdirSync).toHaveBeenCalledWith(path_1.default.join(process.cwd(), projectName), { recursive: true }); expect(fs_1.cpSync).toHaveBeenCalledWith(path_1.default.join(__dirname, '../../templates'), path_1.default.join(process.cwd(), projectName), { recursive: true }); expect(fs_1.copyFileSync).toHaveBeenCalledTimes(3); // For .gitignore, infra/.gitignore and .env.local expect(mockExecSync).toHaveBeenCalledWith('git init', { cwd: path_1.default.join(process.cwd(), projectName), }); expect(mockExecSync).toHaveBeenCalledWith('npm i --ignore-scripts', { cwd: path_1.default.join(process.cwd(), projectName), }); expect(mockExecSync).toHaveBeenCalledWith('npx prisma generate', { cwd: path_1.default.join(process.cwd(), projectName), }); }); it('should throw an error for an invalid version', async () => { const projectName = 'test-project'; const invalidVersion = '2.0.0'; const mockVersions = ['1.0.0', '1.1.0', '1.2.0']; mockExecSync.mockReturnValueOnce(Buffer.from(JSON.stringify(mockVersions))); const consoleSpy = jest.spyOn(console, 'error').mockImplementation(); await (0, new_action_1.default)(`${projectName}@${invalidVersion}`, {}, mockCommand); expect(child_process_1.execSync).toHaveBeenCalledWith('npm view @mbc-cqrs-serverless/core versions --json'); expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('The specified package version does not exist')); consoleSpy.mockRestore(); }); });