UNPKG

@posthog/wizard

Version:

The PostHog wizard helps you to configure your project

117 lines 4.72 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const vercel_1 = require("../vercel"); const fs = __importStar(require("fs")); const child_process = __importStar(require("child_process")); jest.mock('fs'); jest.mock('child_process'); const mockOptions = { installDir: '/tmp/project' }; describe('VercelEnvironmentProvider', () => { let provider; beforeEach(() => { provider = new vercel_1.VercelEnvironmentProvider(mockOptions); jest.clearAllMocks(); }); it('should detect Vercel CLI, project link, and authentication', async () => { child_process.execSync.mockReturnValue(undefined); fs.existsSync.mockImplementation((p) => { if (p.endsWith('.vercel')) return true; if (p.endsWith('project.json')) return true; return false; }); child_process.spawnSync.mockReturnValue({ stdout: 'testuser', stderr: '', status: 0, }); await expect(provider.detect()).resolves.toBe(true); }); it('should return false if Vercel CLI is missing', async () => { child_process.execSync.mockImplementation(() => { throw new Error(); }); await expect(provider.detect()).resolves.toBe(false); }); it('should return false if project is not linked', async () => { child_process.execSync.mockReturnValue(undefined); fs.existsSync.mockReturnValue(false); await expect(provider.detect()).resolves.toBe(false); }); it('should return false if not authenticated', async () => { child_process.execSync.mockReturnValue(undefined); fs.existsSync.mockReturnValue(true); child_process.spawnSync.mockReturnValue({ stdout: 'Log in to Vercel', stderr: '', status: 0, }); await expect(provider.detect()).resolves.toBe(false); }); it('should return false if env var already exists', async () => { const stdinMock = { write: jest.fn(), end: jest.fn() }; let closeCallback; const onMock = jest.fn((event, cb) => { if (event === 'close') closeCallback = cb; }); // Simulate a process with a writable stderr stream let stderrListener; const stderr = { on: jest.fn((event, cb) => { if (event === 'data') stderrListener = cb; }), }; child_process.spawn.mockReturnValue({ stdin: stdinMock, on: onMock, stderr, }); const uploadPromise = provider.uploadEnvVars({ FOO: 'bar' }); // Simulate "already exists" error on stderr, then process close stderrListener && stderrListener('already exists'); closeCallback && closeCallback(1); await expect(uploadPromise).resolves.toEqual({ FOO: false }); }); it('should attempt to upload environment variables', async () => { child_process.spawn.mockReturnValue({}); await provider.uploadEnvVars({ FOO: 'bar' }); expect(child_process.spawn).toHaveBeenCalledWith('vercel', ['env', 'add', 'FOO', 'production'], expect.objectContaining({ stdio: ['pipe', 'pipe', 'pipe'] })); }); }); //# sourceMappingURL=vercel.test.js.map