Skip to content

Commit 8fcc36f

Browse files
committed
test(integration): exercise graphql tracing channels on real node:diagnostics_channel
Adds an integrationTests/diagnostics/ project that installs the built graphql package, registers node:diagnostics_channel, and asserts that subscribers to graphql:parse receive the expected start/end/error lifecycle events. Complements the unit tests (which use a FakeDc) with coverage against a real TracingChannel. The in-tree eslint rules forbid node: imports and flag TracingChannel as experimental in src/; integrationTests/* overrides already allow the node: import, and a file-scope disable handles the experimental warning. The integration project pins engines to >=22 to match where graphql-js v17 is actively tested. This file grows as later channels (validate, execute, subscribe, resolve) are wired up.
1 parent be6381a commit 8fcc36f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"description": "graphql-js tracing channels should publish on node:diagnostics_channel",
3+
"private": true,
4+
"type": "module",
5+
"engines": {
6+
"node": ">=22.0.0"
7+
},
8+
"scripts": {
9+
"test": "node test.js"
10+
},
11+
"dependencies": {
12+
"graphql": "file:../graphql.tgz"
13+
}
14+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// TracingChannel is marked experimental in Node's docs but is shipped on
2+
// every runtime graphql-js supports. This test exercises it directly.
3+
/* eslint-disable n/no-unsupported-features/node-builtins */
4+
5+
import assert from 'node:assert/strict';
6+
import dc from 'node:diagnostics_channel';
7+
8+
import { enableDiagnosticsChannel, parse } from 'graphql';
9+
10+
enableDiagnosticsChannel(dc);
11+
12+
// graphql:parse - synchronous
13+
{
14+
const events = [];
15+
const handler = {
16+
start: (msg) => events.push({ kind: 'start', source: msg.source }),
17+
end: (msg) => events.push({ kind: 'end', source: msg.source }),
18+
asyncStart: (msg) =>
19+
events.push({ kind: 'asyncStart', source: msg.source }),
20+
asyncEnd: (msg) => events.push({ kind: 'asyncEnd', source: msg.source }),
21+
error: (msg) =>
22+
events.push({ kind: 'error', source: msg.source, error: msg.error }),
23+
};
24+
25+
const channel = dc.tracingChannel('graphql:parse');
26+
channel.subscribe(handler);
27+
28+
try {
29+
const doc = parse('{ field }');
30+
assert.equal(doc.kind, 'Document');
31+
assert.deepEqual(
32+
events.map((e) => e.kind),
33+
['start', 'end'],
34+
);
35+
assert.equal(events[0].source, '{ field }');
36+
assert.equal(events[1].source, '{ field }');
37+
} finally {
38+
channel.unsubscribe(handler);
39+
}
40+
}
41+
42+
// graphql:parse - error path fires start, error, end (traceSync finally-emits end)
43+
{
44+
const events = [];
45+
const handler = {
46+
start: (msg) => events.push({ kind: 'start', source: msg.source }),
47+
end: (msg) => events.push({ kind: 'end', source: msg.source }),
48+
error: (msg) =>
49+
events.push({ kind: 'error', source: msg.source, error: msg.error }),
50+
};
51+
52+
const channel = dc.tracingChannel('graphql:parse');
53+
channel.subscribe(handler);
54+
55+
try {
56+
assert.throws(() => parse('{ '));
57+
assert.deepEqual(
58+
events.map((e) => e.kind),
59+
['start', 'error', 'end'],
60+
);
61+
assert.ok(events[1].error instanceof Error);
62+
} finally {
63+
channel.unsubscribe(handler);
64+
}
65+
}
66+
67+
// No-op when nothing is subscribed - parse still succeeds.
68+
{
69+
const doc = parse('{ field }');
70+
assert.equal(doc.kind, 'Document');
71+
}
72+
73+
console.log('diagnostics integration test passed');

resources/integration-test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ describe('Integration Tests', () => {
5555
testOnNodeProject('node');
5656
testOnNodeProject('webpack');
5757

58+
// Tracing channel tests
59+
testOnNodeProject('diagnostics');
60+
5861
// Conditional export tests
5962
testOnNodeProject('conditions');
6063

0 commit comments

Comments
 (0)