@sentry/wizard
Version:
Sentry wizard helping you to configure your project
206 lines • 9.36 kB
JavaScript
"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 wrap_worker_1 = require("../../src/cloudflare/wrap-worker");
(0, vitest_1.describe)('wrapWorkerWithSentry', () => {
const fixturesDir = path.join(__dirname, 'fixtures', 'worker');
let tmpDir;
function copyFixture(fixtureName) {
const content = fs.readFileSync(path.join(fixturesDir, fixtureName), 'utf-8');
const targetPath = path.join(tmpDir, 'worker.ts');
fs.writeFileSync(targetPath, content);
return targetPath;
}
function readResult() {
return fs.readFileSync(path.join(tmpDir, 'worker.ts'), 'utf-8');
}
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wrap-worker-'));
});
(0, vitest_1.afterEach)(() => {
if (fs.existsSync(tmpDir)) {
fs.rmSync(tmpDir, { recursive: true });
}
vitest_1.vi.restoreAllMocks();
});
(0, vitest_1.describe)('basic wrapping', () => {
(0, vitest_1.it)('wraps a simple worker export with Sentry', async () => {
const filePath = copyFixture('simple-with-satisfies.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-test-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
(0, vitest_1.it)('preserves complex handler logic', async () => {
const filePath = copyFixture('complex-handler.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
});
(0, vitest_1.describe)('performance monitoring', () => {
(0, vitest_1.it)('includes tracesSampleRate when performance is enabled', async () => {
const filePath = copyFixture('simple.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: true,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
(0, vitest_1.it)('omits tracesSampleRate when performance is disabled', async () => {
const filePath = copyFixture('simple.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
});
(0, vitest_1.describe)('logs', () => {
(0, vitest_1.it)('includes enableLogs when logs is enabled', async () => {
const filePath = copyFixture('simple.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: true,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
(0, vitest_1.it)('omits enableLogs when logs is disabled', async () => {
const filePath = copyFixture('simple.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).not.toContain('enableLogs');
});
(0, vitest_1.it)('includes both tracesSampleRate and enableLogs when both are enabled', async () => {
const filePath = copyFixture('simple.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: true,
logs: true,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
});
(0, vitest_1.describe)('import handling', () => {
(0, vitest_1.it)('adds Sentry import at the beginning of the file', async () => {
const filePath = copyFixture('with-comment.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
(0, vitest_1.it)('preserves existing imports', async () => {
const filePath = copyFixture('with-import.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
(0, vitest_1.it)('preserves an external default export', async () => {
const filePath = copyFixture('external-default-export.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
});
(0, vitest_1.describe)('idempotency', () => {
(0, vitest_1.it)('does not wrap again if Sentry is already present', async () => {
const filePath = copyFixture('already-wrapped.ts');
const originalContent = fs.readFileSync(path.join(fixturesDir, 'already-wrapped.ts'), 'utf-8');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'new-dsn', {
performance: true,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toBe(originalContent);
});
(0, vitest_1.it)('does not modify if @sentry/cloudflare is imported', async () => {
const filePath = copyFixture('with-sentry-import.ts');
const originalContent = fs.readFileSync(path.join(fixturesDir, 'with-sentry-import.ts'), 'utf-8');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toBe(originalContent);
});
});
(0, vitest_1.describe)('DSN handling', () => {
(0, vitest_1.it)('uses the provided DSN', async () => {
const filePath = copyFixture('simple.ts');
const testDsn = 'https://d7a9abbecd95ed7d0f5b6c965f5fb6ba@o447951.ingest.us.sentry.io/4510147615391744';
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, testDsn, {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
});
(0, vitest_1.describe)('edge cases', () => {
(0, vitest_1.it)('handles worker without satisfies clause', async () => {
const filePath = copyFixture('simple.ts');
await (0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
});
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
(0, vitest_1.it)('handles files with no default export gracefully', async () => {
const filePath = copyFixture('no-default-export.ts');
await (0, vitest_1.expect)((0, wrap_worker_1.wrapWorkerWithSentry)(filePath, 'my-dsn', {
performance: false,
logs: false,
})).resolves.not.toThrow();
const result = readResult();
(0, vitest_1.expect)(result).toMatchSnapshot();
});
});
});
//# sourceMappingURL=wrap-worker.test.js.map