refactor(core-testing): no-sentry → no-instrumentation (mocks OTel too)

This commit is contained in:
2026-05-11 12:14:18 +02:00
parent bd95315a44
commit 8bedb649ca
5 changed files with 50 additions and 9 deletions

View File

@@ -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",

View File

@@ -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";

View File

@@ -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);
});

View File

@@ -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(),
}));

View File

@@ -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.