Skip to content

Commit 3e24628

Browse files
committed
test(diagnostics): cover tracing subscriber fallback
1 parent ad901f1 commit 3e24628

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/__tests__/diagnostics-test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { expect } from 'chai';
66

77
import { invariant } from '../jsutils/invariant.ts';
88

9-
import type { MinimalTracingChannel } from '../diagnostics.ts';
9+
import type { MinimalChannel, MinimalTracingChannel } from '../diagnostics.ts';
1010
import {
1111
executeChannel,
1212
executeRootSelectionSetChannel,
@@ -54,6 +54,38 @@ describe('diagnostics', () => {
5454
});
5555

5656
describe('shouldTrace', () => {
57+
function makeSubChannel(hasSubscribers: boolean): MinimalChannel {
58+
return {
59+
hasSubscribers,
60+
publish: () => undefined,
61+
runStores<T, ContextType extends object>(
62+
context: ContextType,
63+
fn: (this: ContextType, ...args: Array<unknown>) => T,
64+
): T {
65+
return fn.call(context);
66+
},
67+
};
68+
}
69+
70+
function makeFallbackTracingChannel(
71+
subscribedSubChannel?: keyof Pick<
72+
MinimalTracingChannel,
73+
'start' | 'end' | 'asyncStart' | 'asyncEnd' | 'error'
74+
>,
75+
): MinimalTracingChannel {
76+
return {
77+
hasSubscribers: undefined,
78+
start: makeSubChannel(subscribedSubChannel === 'start'),
79+
end: makeSubChannel(subscribedSubChannel === 'end'),
80+
asyncStart: makeSubChannel(subscribedSubChannel === 'asyncStart'),
81+
asyncEnd: makeSubChannel(subscribedSubChannel === 'asyncEnd'),
82+
error: makeSubChannel(subscribedSubChannel === 'error'),
83+
traceSync<T>(fn: (...args: Array<unknown>) => T): T {
84+
return fn();
85+
},
86+
};
87+
}
88+
5789
it('returns false when channel is undefined', () => {
5890
expect(shouldTrace(undefined)).to.equal(false);
5991
});
@@ -79,5 +111,10 @@ describe('diagnostics', () => {
79111
realTC.unsubscribe(handler);
80112
}
81113
});
114+
115+
it('falls back to sub-channel subscribers when aggregate is missing', () => {
116+
expect(shouldTrace(makeFallbackTracingChannel('error'))).to.equal(true);
117+
expect(shouldTrace(makeFallbackTracingChannel())).to.equal(false);
118+
});
82119
});
83120
});

src/diagnostics.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,14 @@ function resolveDiagnosticsChannel(): DiagnosticsChannelModule | undefined {
280280
) as DiagnosticsChannelModule;
281281
}
282282
// TODO: remove this code when we drop support for Node < 20.16>.
283-
/* c8 ignore next 6 */
283+
/* node:coverage ignore next 6 */
284284
if (!dc && typeof require === 'function') {
285285
// CJS fallback for runtimes that lack `process.getBuiltinModule`
286286
// (e.g. Node 20.0 - 20.15). ESM builds skip this branch because
287287
// `require` is undeclared there.
288288
dc = require('node:diagnostics_channel') as DiagnosticsChannelModule;
289289
}
290-
/* c8 ignore next 3 */
290+
/* node:coverage ignore next 3 */
291291
} catch {
292292
// diagnostics_channel not available on this runtime; tracing is a no-op.
293293
}
@@ -355,7 +355,6 @@ export function shouldTrace<TContext = unknown>(
355355
const aggregate = channel.hasSubscribers;
356356
if (aggregate !== undefined) {
357357
return aggregate;
358-
/* c8 ignore start */
359358
}
360359
// Bun-only fallback, exercised by integrationTests/diagnostics-bun.
361360
for (const key of SUB_CHANNEL_KEYS) {
@@ -364,7 +363,6 @@ export function shouldTrace<TContext = unknown>(
364363
}
365364
}
366365
return false;
367-
/* c8 ignore stop */
368366
}
369367

370368
interface TraceLifecycleContext {

0 commit comments

Comments
 (0)