Files
agentic-dev/packages/core-testing/src/setup/no-instrumentation.ts
Danijel Martinek 2edc76002a refactor(docs): strip residual Phase/Plan setup-history references
Final sweep for setup-process bookkeeping not caught by template-reset-v1.
ADRs drop Plan-N qualifiers; spec collapses the historical 11-phase
migration table; scaffolding guide drops "Phase added" column; comment
prefixes referencing R-numbers in test describes / eslint inline comments
are normalized. Architecture-level rule IDs (R40, R52, E0, J0, etc.) are
preserved where they serve as stable cross-references in ADRs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 10:28:31 +02:00

106 lines
3.0 KiB
TypeScript

import { vi } from "vitest";
/**
* Guard against real Sentry SDK + OTel SDK initialization in test processes.
*
* Mocks @sentry/* and key @opentelemetry/sdk-* modules at the module level so
* any code that imports them receives a no-op surface. Tests that need to assert
* specific SDK behavior still use vi.mock locally with their own implementation;
* this guard just ensures *unintentional* imports don't cause real network/init.
*
* Also exported as ./setup/no-sentry for one release cycle (backward-compat alias).
*/
vi.mock("@sentry/nextjs", () => ({
init: vi.fn(),
startSpan: vi.fn((_opts: unknown, fn: (span: unknown) => unknown) =>
fn({ setAttribute: vi.fn(), setStatus: vi.fn() }),
),
captureException: vi.fn(),
captureMessage: vi.fn(),
addBreadcrumb: vi.fn(),
setUser: vi.fn(),
setContext: vi.fn(),
setTag: vi.fn(),
setExtra: vi.fn(),
withScope: vi.fn((fn: (scope: unknown) => unknown) =>
fn({ setTag: vi.fn(), setExtra: vi.fn() }),
),
replayIntegration: vi.fn(() => ({ name: "Replay" })),
getActiveSpan: vi.fn(() => undefined),
getCurrentHub: vi.fn(() => ({ getClient: () => undefined })),
}));
vi.mock("@sentry/node", () => ({
init: vi.fn(),
startSpan: vi.fn((_opts: unknown, fn: (span: unknown) => unknown) =>
fn({ setAttribute: vi.fn(), setStatus: vi.fn() }),
),
captureException: vi.fn(),
captureMessage: vi.fn(),
addBreadcrumb: vi.fn(),
setUser: vi.fn(),
setContext: vi.fn(),
setTag: vi.fn(),
setExtra: vi.fn(),
withScope: vi.fn((fn: (scope: unknown) => unknown) =>
fn({ setTag: vi.fn(), setExtra: vi.fn() }),
),
getActiveSpan: vi.fn(() => undefined),
}));
vi.mock("@sentry/react", () => ({
init: vi.fn(),
captureException: vi.fn(),
captureMessage: vi.fn(),
addBreadcrumb: vi.fn(),
setUser: vi.fn(),
setContext: vi.fn(),
setTag: vi.fn(),
setExtra: vi.fn(),
withScope: vi.fn((fn: (scope: unknown) => unknown) =>
fn({ setTag: vi.fn(), setExtra: vi.fn() }),
),
replayIntegration: vi.fn(() => ({ name: "Replay" })),
}));
// OTel SDK mocks — prevent real SDK initialization in vitest runs.
// Feature packages and core-shared instrumentation code import these; without
// mocks the NodeSDK would attempt to bootstrap a real tracer/logger provider.
vi.mock("@opentelemetry/sdk-node", () => ({
NodeSDK: class {
start() {}
shutdown() {
return Promise.resolve();
}
},
// Re-export tracing namespace so destructured imports work
tracing: {
BatchSpanProcessor: class {
onStart() {}
onEnd() {}
forceFlush() {
return Promise.resolve();
}
shutdown() {
return Promise.resolve();
}
},
},
}));
vi.mock("@sentry/opentelemetry", () => ({
SentrySpanProcessor: class {
onStart() {}
onEnd() {}
forceFlush() {
return Promise.resolve();
}
shutdown() {
return Promise.resolve();
}
},
// SentryLogRecordProcessor does NOT exist in @sentry/opentelemetry v10 — omitted.
// No-op Sentry.init wrapper used by sentry-bridge.ts
init: vi.fn(),
}));