1- import { describe , expect , test } from "bun:test" ;
1+ import { afterEach , beforeEach , describe , expect , test } from "bun:test" ;
22import createSentrySDK , { SentryError } from "../../src/index.js" ;
3+ import { mockFetch } from "../helpers.js" ;
34
45describe ( "createSentrySDK() library API" , ( ) => {
6+ // Silence unmocked fetch calls from resolution cascade.
7+ // SDK tests that call commands like "issue list" or "org list" trigger
8+ // the org/project resolution cascade which hits real API endpoints.
9+ // A silent 404 prevents preload warnings while preserving error behavior.
10+ let originalFetch : typeof globalThis . fetch ;
11+
12+ beforeEach ( ( ) => {
13+ originalFetch = globalThis . fetch ;
14+ // Return empty successes rather than 404s so the resolution cascade
15+ // terminates cleanly without triggering follow-up requests that could
16+ // outlive the test and spill into later test files.
17+ globalThis . fetch = mockFetch ( async ( input ) => {
18+ let url : string ;
19+ if ( typeof input === "string" ) {
20+ url = input ;
21+ } else if ( input instanceof URL ) {
22+ url = input . href ;
23+ } else {
24+ url = new Request ( input ) . url ;
25+ }
26+ if ( url . includes ( "/regions/" ) ) {
27+ return new Response ( JSON . stringify ( { regions : [ ] } ) , { status : 200 } ) ;
28+ }
29+ if ( url . includes ( "/organizations/" ) ) {
30+ return new Response ( JSON . stringify ( [ ] ) , { status : 200 } ) ;
31+ }
32+ // Return empty 200 for all other endpoints (projects, issues, etc.)
33+ // to prevent follow-up requests from outliving the test.
34+ return new Response ( JSON . stringify ( { } ) , { status : 200 } ) ;
35+ } ) ;
36+ } ) ;
37+
38+ afterEach ( ( ) => {
39+ globalThis . fetch = originalFetch ;
40+ } ) ;
41+
542 test ( "sdk.run returns version string for --version" , async ( ) => {
643 const sdk = createSentrySDK ( ) ;
744 const result = await sdk . run ( "--version" ) ;
@@ -19,7 +56,9 @@ describe("createSentrySDK() library API", () => {
1956 } ) ;
2057
2158 test ( "sdk.run throws when auth is required but missing" , async ( ) => {
22- const sdk = createSentrySDK ( ) ;
59+ // Use cwd:/tmp to prevent DSN scanning of the repo root which finds
60+ // real DSNs and triggers async project resolution that can outlive the test.
61+ const sdk = createSentrySDK ( { cwd : "/tmp" } ) ;
2362 try {
2463 // issue list requires auth — with no token and isolated config, it should fail
2564 await sdk . run ( "issue" , "list" ) ;
@@ -43,7 +82,7 @@ describe("createSentrySDK() library API", () => {
4382 } ) ;
4483
4584 test ( "process.env is unchanged after failed call" , async ( ) => {
46- const sdk = createSentrySDK ( ) ;
85+ const sdk = createSentrySDK ( { cwd : "/tmp" } ) ;
4786 const envBefore = { ...process . env } ;
4887 try {
4988 await sdk . run ( "issue" , "list" ) ;
0 commit comments