From 8bedb649ca78d5a951b2618f0aea92a56f98007b Mon Sep 17 00:00:00 2001 From: Danijel Martinek Date: Mon, 11 May 2026 12:14:18 +0200 Subject: [PATCH] =?UTF-8?q?refactor(core-testing):=20no-sentry=20=E2=86=92?= =?UTF-8?q?=20no-instrumentation=20(mocks=20OTel=20too)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core-testing/package.json | 4 +- packages/core-testing/src/setup/jsdom.ts | 2 +- ...try.test.ts => no-instrumentation.test.ts} | 2 +- .../{no-sentry.ts => no-instrumentation.ts} | 49 +++++++++++++++++-- packages/core-testing/src/setup/node.ts | 2 +- 5 files changed, 50 insertions(+), 9 deletions(-) rename packages/core-testing/src/setup/{no-sentry.test.ts => no-instrumentation.test.ts} (90%) rename packages/core-testing/src/setup/{no-sentry.ts => no-instrumentation.ts} (50%) diff --git a/packages/core-testing/package.json b/packages/core-testing/package.json index c41b55d..9715dec 100644 --- a/packages/core-testing/package.json +++ b/packages/core-testing/package.json @@ -12,7 +12,9 @@ "./payload": "./src/payload/index.ts", "./payload/stub-config": "./src/payload/stub-config.ts", "./setup/jsdom": "./src/setup/jsdom.ts", - "./setup/node": "./src/setup/node.ts" + "./setup/node": "./src/setup/node.ts", + "./setup/no-instrumentation": "./src/setup/no-instrumentation.ts", + "./setup/no-sentry": "./src/setup/no-instrumentation.ts" }, "scripts": { "build": "tsc --noEmit", diff --git a/packages/core-testing/src/setup/jsdom.ts b/packages/core-testing/src/setup/jsdom.ts index c079b4f..54f4020 100644 --- a/packages/core-testing/src/setup/jsdom.ts +++ b/packages/core-testing/src/setup/jsdom.ts @@ -1,4 +1,4 @@ -import "./no-sentry"; +import "./no-instrumentation"; import "@testing-library/jest-dom/vitest"; import { afterEach } from "vitest"; import { cleanup } from "@testing-library/react"; diff --git a/packages/core-testing/src/setup/no-sentry.test.ts b/packages/core-testing/src/setup/no-instrumentation.test.ts similarity index 90% rename from packages/core-testing/src/setup/no-sentry.test.ts rename to packages/core-testing/src/setup/no-instrumentation.test.ts index 5f19577..dad0cb1 100644 --- a/packages/core-testing/src/setup/no-sentry.test.ts +++ b/packages/core-testing/src/setup/no-instrumentation.test.ts @@ -1,7 +1,7 @@ import { describe, it, expect, vi } from "vitest"; import * as Sentry from "@sentry/nextjs"; -describe("setup/no-sentry guard (R49)", () => { +describe("setup/no-instrumentation guard (R49)", () => { it("Sentry.init is a vi.fn (mocked, not real)", () => { expect(vi.isMockFunction(Sentry.init)).toBe(true); }); diff --git a/packages/core-testing/src/setup/no-sentry.ts b/packages/core-testing/src/setup/no-instrumentation.ts similarity index 50% rename from packages/core-testing/src/setup/no-sentry.ts rename to packages/core-testing/src/setup/no-instrumentation.ts index e9df5b1..aee602a 100644 --- a/packages/core-testing/src/setup/no-sentry.ts +++ b/packages/core-testing/src/setup/no-instrumentation.ts @@ -1,12 +1,14 @@ import { vi } from "vitest"; /** - * R49 — guard against real Sentry SDK initialization in test processes. + * R49 — guard against real Sentry SDK + OTel SDK initialization in test processes. * - * Mocks @sentry/nextjs at the module level so any code that imports it - * receives a no-op surface. Tests that need to assert Sentry behavior - * still use vi.mock locally with their own implementation; this guard - * just ensures *unintentional* imports don't cause real network/init. + * 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(), @@ -60,3 +62,40 @@ vi.mock("@sentry/react", () => ({ ), 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: class { + onEmit() {} + forceFlush() { return Promise.resolve(); } + shutdown() { return Promise.resolve(); } + }, + // No-op Sentry.init wrapper used by sentry-bridge.ts + init: vi.fn(), +})); diff --git a/packages/core-testing/src/setup/node.ts b/packages/core-testing/src/setup/node.ts index 923a984..d186513 100644 --- a/packages/core-testing/src/setup/node.ts +++ b/packages/core-testing/src/setup/node.ts @@ -1,4 +1,4 @@ -import "./no-sentry"; +import "./no-instrumentation"; // Reserved for future global node-env setup. Currently a no-op so that // vitest configs may reference @repo/core-testing/setup/node uniformly.