Skip to content

Commit dc3b80f

Browse files
logaretmyaacovCR
authored andcommitted
cover shouldTrace: real-channel test plus c8 ignore for Bun fallback
1 parent 935a1b7 commit dc3b80f

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/__tests__/diagnostics-test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
/* eslint-disable import/no-nodejs-modules */
1+
/* eslint-disable import/no-nodejs-modules, n/no-unsupported-features/node-builtins */
22
import dc from 'node:diagnostics_channel';
33

44
import { expect } from 'chai';
55
import { describe, it } from 'mocha';
66

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

9+
import type { MinimalTracingChannel } from '../diagnostics.js';
910
import {
1011
executeChannel,
1112
parseChannel,
1213
resolveChannel,
14+
shouldTrace,
1315
subscribeChannel,
1416
validateChannel,
1517
} from '../diagnostics.js';
@@ -40,4 +42,32 @@ describe('diagnostics', () => {
4042
dc.channel('tracing:graphql:resolve:start'),
4143
);
4244
});
45+
46+
describe('shouldTrace', () => {
47+
it('returns false when channel is undefined', () => {
48+
expect(shouldTrace(undefined)).to.equal(false);
49+
});
50+
51+
it('reflects the aggregate hasSubscribers on a real tracing channel', () => {
52+
const tc = dc.tracingChannel(
53+
'shouldTrace:aggregate',
54+
) as unknown as MinimalTracingChannel;
55+
expect(shouldTrace(tc)).to.equal(false);
56+
57+
const handler = {
58+
start: () => undefined,
59+
end: () => undefined,
60+
asyncStart: () => undefined,
61+
asyncEnd: () => undefined,
62+
error: () => undefined,
63+
};
64+
const realTC = dc.tracingChannel('shouldTrace:aggregate');
65+
realTC.subscribe(handler);
66+
try {
67+
expect(shouldTrace(tc)).to.equal(true);
68+
} finally {
69+
realTC.unsubscribe(handler);
70+
}
71+
});
72+
});
4373
});

src/diagnostics.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ export function shouldTrace(
150150
return false;
151151
}
152152
const aggregate = channel.hasSubscribers;
153-
if (aggregate !== undefined) {
154-
return aggregate;
153+
/* c8 ignore next 3: Bun-only fallback, exercised by integrationTests/diagnostics-bun. */
154+
if (aggregate === undefined) {
155+
return SUB_CHANNEL_KEYS.some((key) => channel[key].hasSubscribers === true);
155156
}
156-
return SUB_CHANNEL_KEYS.some((key) => channel[key].hasSubscribers === true);
157+
return aggregate;
157158
}
158159

159160
/**

0 commit comments

Comments
 (0)