UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

216 lines 10.7 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const fs = __importStar(require("node:fs")); const path = __importStar(require("node:path")); const os = __importStar(require("node:os")); const update_wrangler_config_1 = require("../../src/cloudflare/wrangler/update-wrangler-config"); const { clackMocks } = vitest_1.vi.hoisted(() => { return { clackMocks: { log: { success: vitest_1.vi.fn(), info: vitest_1.vi.fn(), step: vitest_1.vi.fn(), warn: vitest_1.vi.fn(), error: vitest_1.vi.fn(), }, }, }; }); vitest_1.vi.mock('@clack/prompts', () => ({ default: clackMocks, })); (0, vitest_1.describe)('updateWranglerConfig', () => { const fixturesDir = path.join(__dirname, 'fixtures', 'wrangler'); let tmpDir; function copyFixture(fixtureName, targetName) { const content = fs.readFileSync(path.join(fixturesDir, fixtureName), 'utf-8'); fs.writeFileSync(path.join(tmpDir, targetName), content); } function readResult(fileName) { return fs.readFileSync(path.join(tmpDir, fileName), 'utf-8'); } (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'update-wrangler-config-')); vitest_1.vi.spyOn(process, 'cwd').mockReturnValue(tmpDir); }); (0, vitest_1.afterEach)(() => { if (fs.existsSync(tmpDir)) { fs.rmSync(tmpDir, { recursive: true }); } vitest_1.vi.restoreAllMocks(); }); (0, vitest_1.describe)('JSON/JSONC format', () => { (0, vitest_1.it)('adds new fields to JSON config', async () => { copyFixture('wrangler-basic.json', 'wrangler.json'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], version_metadata: { binding: 'CF_VERSION_METADATA' }, }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.compatibility_flags).toEqual(['nodejs_als']); (0, vitest_1.expect)(parsed.version_metadata).toEqual({ binding: 'CF_VERSION_METADATA', }); (0, vitest_1.expect)(parsed.name).toBe('my-worker'); }); (0, vitest_1.it)('overrides existing fields in JSON config', async () => { copyFixture('wrangler-basic.json', 'wrangler.json'); const initialContent = readResult('wrangler.json'); const initialParsed = JSON.parse(initialContent); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_date: '1337-01-01', }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.compatibility_date).not.toEqual(initialParsed.compatibility_date); (0, vitest_1.expect)(parsed.compatibility_date).toEqual('1337-01-01'); }); (0, vitest_1.it)('merges array fields in JSON config', async () => { copyFixture('wrangler-with-flags.json', 'wrangler.json'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als', 'nodejs_compat'], }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.compatibility_flags).toEqual([ 'old_flag', 'nodejs_als', 'nodejs_compat', ]); }); (0, vitest_1.it)('deduplicates array values in JSON', async () => { copyFixture('wrangler-with-duplicate-flags.json', 'wrangler.json'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als', 'new_flag'], }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.compatibility_flags).toEqual([ 'nodejs_als', 'old_flag', 'new_flag', ]); }); (0, vitest_1.it)('preserves comments in JSONC config', async () => { copyFixture('wrangler-with-comment.jsonc', 'wrangler.jsonc'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], }); const writtenContent = readResult('wrangler.jsonc'); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(writtenContent).toMatchSnapshot(); }); (0, vitest_1.it)('merges multi-line array fields in JSON config', async () => { copyFixture('wrangler-multi-line-array.json', 'wrangler.json'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.compatibility_flags).toEqual([ 'global_fetch_strictly_public', 'nodejs_als', ]); }); vitest_1.it.skip('merges multiple fields including arrays with comments', async () => { copyFixture('wrangler-complex-with-comments.jsonc', 'wrangler.jsonc'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], version_metadata: { binding: 'CF_VERSION_METADATA' }, }); const writtenContent = readResult('wrangler.jsonc'); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(writtenContent).toMatchSnapshot(); }); (0, vitest_1.it)('adds new fields to JSONC in object', async () => { copyFixture('wrangler-with-metadata.jsonc', 'wrangler.jsonc'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ version_metadata: { binding: 'CF_VERSION_METADATA' }, }); const writtenContent = readResult('wrangler.jsonc'); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(writtenContent).toMatchSnapshot(); }); (0, vitest_1.it)('preserves all comment types in JSONC', async () => { copyFixture('wrangler-all-comment-types.jsonc', 'wrangler.jsonc'); await (0, update_wrangler_config_1.updateWranglerConfig)({ version_metadata: { binding: 'CF_VERSION_METADATA' }, }); const writtenContent = readResult('wrangler.jsonc'); (0, vitest_1.expect)(writtenContent).toMatchSnapshot(); }); (0, vitest_1.it)('handles empty JSON object', async () => { copyFixture('wrangler-empty.json', 'wrangler.json'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.compatibility_flags).toEqual(['nodejs_als']); }); (0, vitest_1.it)('adds nested objects to JSON', async () => { copyFixture('wrangler-minimal.json', 'wrangler.json'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ version_metadata: { binding: 'CF_VERSION_METADATA' }, }); const writtenContent = readResult('wrangler.json'); const parsed = JSON.parse(writtenContent); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(parsed.version_metadata).toEqual({ binding: 'CF_VERSION_METADATA', }); }); (0, vitest_1.it)('handles JSONC with trailing comma', async () => { copyFixture('wrangler-trailing-comma.jsonc', 'wrangler.jsonc'); const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], }); const writtenContent = readResult('wrangler.jsonc'); (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(writtenContent).toContain('compatibility_flags'); }); }); (0, vitest_1.describe)('error handling', () => { (0, vitest_1.it)('returns false when no config file exists', async () => { const result = await (0, update_wrangler_config_1.updateWranglerConfig)({ compatibility_flags: ['nodejs_als'], }); (0, vitest_1.expect)(result).toBe(false); (0, vitest_1.expect)(clackMocks.log.warn).toHaveBeenCalledWith('No wrangler config file found.'); }); }); }); //# sourceMappingURL=update-wrangler-config.test.js.map