|
| 1 | +/* eslint-disable import/no-nodejs-modules */ |
| 2 | +import dc from 'node:diagnostics_channel'; |
| 3 | + |
1 | 4 | import { expect } from 'chai'; |
2 | 5 | import { describe, it } from 'mocha'; |
3 | 6 |
|
4 | | -import { sharedFakeDc } from '../__testUtils__/fakeDiagnosticsChannel.js'; |
5 | | - |
6 | 7 | import { invariant } from '../jsutils/invariant.js'; |
7 | 8 |
|
8 | | -import { enableDiagnosticsChannel, getChannels } from '../diagnostics.js'; |
| 9 | +import { getChannels } from '../diagnostics.js'; |
9 | 10 |
|
10 | 11 | describe('diagnostics', () => { |
11 | | - it('exposes the five graphql tracing channels after registration', () => { |
12 | | - enableDiagnosticsChannel(sharedFakeDc); |
13 | | - |
| 12 | + it('auto-registers the five graphql tracing channels', () => { |
14 | 13 | const channels = getChannels(); |
15 | 14 | invariant(channels !== undefined); |
16 | | - expect(channels.execute).to.equal( |
17 | | - sharedFakeDc.tracingChannel('graphql:execute'), |
18 | | - ); |
19 | | - expect(channels.parse).to.equal( |
20 | | - sharedFakeDc.tracingChannel('graphql:parse'), |
21 | | - ); |
22 | | - expect(channels.validate).to.equal( |
23 | | - sharedFakeDc.tracingChannel('graphql:validate'), |
24 | | - ); |
25 | | - expect(channels.resolve).to.equal( |
26 | | - sharedFakeDc.tracingChannel('graphql:resolve'), |
27 | | - ); |
28 | | - expect(channels.subscribe).to.equal( |
29 | | - sharedFakeDc.tracingChannel('graphql:subscribe'), |
30 | | - ); |
31 | | - }); |
32 | | - |
33 | | - it('re-registration with the same module is a no-op', () => { |
34 | | - enableDiagnosticsChannel(sharedFakeDc); |
35 | | - const first = getChannels(); |
36 | | - invariant(first !== undefined); |
37 | | - |
38 | | - enableDiagnosticsChannel(sharedFakeDc); |
39 | | - const second = getChannels(); |
40 | | - |
41 | | - expect(second).to.equal(first); |
42 | | - }); |
43 | | - |
44 | | - it('re-registration with a different module throws', () => { |
45 | | - enableDiagnosticsChannel(sharedFakeDc); |
46 | | - |
47 | | - expect(() => |
48 | | - enableDiagnosticsChannel({ |
49 | | - tracingChannel: () => { |
50 | | - throw new Error('should not be called'); |
51 | | - }, |
52 | | - }), |
53 | | - ).to.throw(/different `diagnostics_channel` module/); |
54 | | - }); |
55 | | - |
56 | | - it('re-registration accepts a wrapper sharing the same tracingChannel fn', () => { |
57 | | - enableDiagnosticsChannel(sharedFakeDc); |
58 | | - const first = getChannels(); |
59 | | - invariant(first !== undefined); |
60 | | - |
61 | | - // Models an ESM Module Namespace object: distinct outer reference, but |
62 | | - // the `tracingChannel` property is strictly the same function as the |
63 | | - // default export's, so it routes to the same underlying channel cache. |
64 | | - const namespaceLike = { tracingChannel: sharedFakeDc.tracingChannel }; |
65 | | - expect(namespaceLike).to.not.equal(sharedFakeDc); |
66 | | - expect(namespaceLike.tracingChannel).to.equal(sharedFakeDc.tracingChannel); |
67 | 15 |
|
68 | | - expect(() => enableDiagnosticsChannel(namespaceLike)).to.not.throw(); |
69 | | - expect(getChannels()).to.equal(first); |
| 16 | + // Node's `tracingChannel(name)` returns a fresh wrapper per call but |
| 17 | + // the underlying sub-channels are cached by name, so compare those. |
| 18 | + const byName = { |
| 19 | + execute: 'graphql:execute', |
| 20 | + parse: 'graphql:parse', |
| 21 | + validate: 'graphql:validate', |
| 22 | + resolve: 'graphql:resolve', |
| 23 | + subscribe: 'graphql:subscribe', |
| 24 | + } as const; |
| 25 | + for (const [key, name] of Object.entries(byName)) { |
| 26 | + expect(channels[key as keyof typeof byName].start).to.equal( |
| 27 | + dc.channel(`tracing:${name}:start`), |
| 28 | + ); |
| 29 | + } |
70 | 30 | }); |
71 | 31 | }); |
0 commit comments