@sentry/wizard
Version:
Sentry wizard helping you to configure your project
170 lines • 9.41 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 (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 sdk_setup_1 = require("../../src/cloudflare/sdk-setup");
const get_entry_point_from_wrangler_config_1 = require("../../src/cloudflare/wrangler/get-entry-point-from-wrangler-config");
const templates = __importStar(require("../../src/cloudflare/templates"));
const wrapWorker = __importStar(require("../../src/cloudflare/wrap-worker"));
const { clackMocks } = vitest_1.vi.hoisted(() => {
const info = vitest_1.vi.fn();
const warn = vitest_1.vi.fn();
const success = vitest_1.vi.fn();
const step = vitest_1.vi.fn();
const note = vitest_1.vi.fn();
return {
clackMocks: {
info,
warn,
success,
step,
note,
},
};
});
vitest_1.vi.mock('@clack/prompts', () => ({
__esModule: true,
default: {
log: {
info: clackMocks.info,
warn: clackMocks.warn,
success: clackMocks.success,
step: clackMocks.step,
},
note: clackMocks.note,
},
}));
const { getEntryPointFromWranglerConfigMock } = vitest_1.vi.hoisted(() => ({
getEntryPointFromWranglerConfigMock: vitest_1.vi.fn(),
}));
vitest_1.vi.mock('../../src/cloudflare/wrangler/get-entry-point-from-wrangler-config', () => ({
getEntryPointFromWranglerConfig: getEntryPointFromWranglerConfigMock,
defaultEntryPoint: 'src/index.ts',
}));
(0, vitest_1.describe)('createSentryInitFile', () => {
let tmpDir;
const testDsn = 'https://example@sentry.io/123';
const testFeatures = {
performance: true,
logs: false,
};
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sdk-setup-test-'));
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.it)('creates a new default entry point file', async () => {
const template = 'export default { async fetch() { return new Response("ᕕ(⌐■_■)ᕗ ♪♬"); } }';
const getCloudflareWorkerTemplateWithHandlerSpy = vitest_1.vi
.spyOn(templates, 'getCloudflareWorkerTemplateWithHandler')
.mockReturnValue(template);
await (0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures);
const expectedPath = path.join(tmpDir, 'src/index.ts');
const content = fs.readFileSync(expectedPath, 'utf-8');
(0, vitest_1.expect)(clackMocks.info).toHaveBeenCalledWith('No entry point found in wrangler config, creating a new one.');
(0, vitest_1.expect)(fs.existsSync(expectedPath)).toBe(true);
(0, vitest_1.expect)(content).toBe(template);
(0, vitest_1.expect)(clackMocks.success).toHaveBeenCalledWith('Created src/index.ts.');
(0, vitest_1.expect)(getCloudflareWorkerTemplateWithHandlerSpy).toHaveBeenCalled();
});
(0, vitest_1.describe)('when entry point is found in wrangler config', () => {
(0, vitest_1.describe)('and the entry point file exists', () => {
(0, vitest_1.beforeEach)(async () => {
await (0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures);
getEntryPointFromWranglerConfigMock.mockReturnValue(get_entry_point_from_wrangler_config_1.defaultEntryPoint);
});
(0, vitest_1.it)('wraps the worker with Sentry when wrapping succeeds', async () => {
const wrapWorkerWithSentrySpy = vitest_1.vi
.spyOn(wrapWorker, 'wrapWorkerWithSentry')
.mockResolvedValue(undefined);
await (0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures);
(0, vitest_1.expect)(wrapWorkerWithSentrySpy).toHaveBeenCalledWith(path.join(tmpDir, get_entry_point_from_wrangler_config_1.defaultEntryPoint), testDsn, testFeatures);
});
(0, vitest_1.it)('handles wrapping failure gracefully', async () => {
vitest_1.vi.spyOn(wrapWorker, 'wrapWorkerWithSentry').mockRejectedValue(new Error('Wrapping failed'));
await (0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures);
(0, vitest_1.expect)(clackMocks.warn).toHaveBeenCalledWith('Failed to wrap worker automatically.');
(0, vitest_1.expect)(clackMocks.step).toHaveBeenCalledWith('Please wrap your handler with Sentry initialization:');
(0, vitest_1.expect)(clackMocks.note).toHaveBeenCalledWith(vitest_1.expect.stringContaining('import * as Sentry'));
});
(0, vitest_1.it)('shows template with correct DSN when wrapping fails', async () => {
vitest_1.vi.spyOn(wrapWorker, 'wrapWorkerWithSentry').mockRejectedValue(new Error('Failed'));
const getCloudflareWorkerTemplateSpy = vitest_1.vi
.spyOn(templates, 'getCloudflareWorkerTemplate')
.mockReturnValue('template with dsn');
await (0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures);
(0, vitest_1.expect)(getCloudflareWorkerTemplateSpy).toHaveBeenCalledWith(testDsn, testFeatures);
(0, vitest_1.expect)(clackMocks.note).toHaveBeenCalledWith('template with dsn');
});
(0, vitest_1.it)('passes performance feature flag correctly', async () => {
const wrapWorkerWithSentrySpy = vitest_1.vi
.spyOn(wrapWorker, 'wrapWorkerWithSentry')
.mockResolvedValue(undefined);
await (0, sdk_setup_1.createSentryInitFile)(testDsn, {
performance: false,
logs: false,
});
(0, vitest_1.expect)(wrapWorkerWithSentrySpy).toHaveBeenCalledWith(path.join(tmpDir, get_entry_point_from_wrangler_config_1.defaultEntryPoint), testDsn, { performance: false, logs: false });
});
(0, vitest_1.it)('passes logs feature flag correctly', async () => {
const wrapWorkerWithSentrySpy = vitest_1.vi
.spyOn(wrapWorker, 'wrapWorkerWithSentry')
.mockResolvedValue(undefined);
await (0, sdk_setup_1.createSentryInitFile)(testDsn, { performance: false, logs: true });
(0, vitest_1.expect)(wrapWorkerWithSentrySpy).toHaveBeenCalledWith(path.join(tmpDir, get_entry_point_from_wrangler_config_1.defaultEntryPoint), testDsn, { performance: false, logs: true });
});
(0, vitest_1.it)('passes both feature flags correctly', async () => {
const wrapWorkerWithSentrySpy = vitest_1.vi
.spyOn(wrapWorker, 'wrapWorkerWithSentry')
.mockResolvedValue(undefined);
await (0, sdk_setup_1.createSentryInitFile)(testDsn, { performance: true, logs: true });
(0, vitest_1.expect)(wrapWorkerWithSentrySpy).toHaveBeenCalledWith(path.join(tmpDir, get_entry_point_from_wrangler_config_1.defaultEntryPoint), testDsn, { performance: true, logs: true });
});
});
(0, vitest_1.describe)('and the entry point file does not exist', () => {
(0, vitest_1.it)('does not throw an error', async () => {
vitest_1.vi.spyOn(wrapWorker, 'wrapWorkerWithSentry').mockResolvedValue(undefined);
await (0, vitest_1.expect)((0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures)).resolves.not.toThrow();
});
(0, vitest_1.it)('does not call wrapWorkerWithSentry', async () => {
const wrapWorkerWithSentrySpy = vitest_1.vi
.spyOn(wrapWorker, 'wrapWorkerWithSentry')
.mockResolvedValue(undefined);
await (0, sdk_setup_1.createSentryInitFile)(testDsn, testFeatures);
(0, vitest_1.expect)(wrapWorkerWithSentrySpy).not.toHaveBeenCalled();
});
});
});
});
//# sourceMappingURL=sdk-setup.test.js.map