|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { describe, it } from 'mocha'; |
3 | 3 |
|
| 4 | +import { sharedFakeDc } from '../__testUtils__/fakeDiagnosticsChannel.js'; |
| 5 | + |
4 | 6 | import { invariant } from '../jsutils/invariant.js'; |
5 | 7 |
|
6 | | -import type { |
7 | | - MinimalDiagnosticsChannel, |
8 | | - MinimalTracingChannel, |
9 | | -} from '../diagnostics.js'; |
10 | 8 | import { enableDiagnosticsChannel, getChannels } from '../diagnostics.js'; |
11 | 9 |
|
12 | | -function fakeTracingChannel(name: string): MinimalTracingChannel { |
13 | | - const noop: MinimalTracingChannel['start'] = { |
14 | | - hasSubscribers: false, |
15 | | - publish: () => { |
16 | | - /* noop */ |
17 | | - }, |
18 | | - runStores: <T>( |
19 | | - _ctx: unknown, |
20 | | - fn: (this: unknown, ...args: Array<unknown>) => T, |
21 | | - ): T => fn(), |
22 | | - }; |
23 | | - const channel: MinimalTracingChannel & { _name: string } = { |
24 | | - _name: name, |
25 | | - hasSubscribers: false, |
26 | | - start: noop, |
27 | | - end: noop, |
28 | | - asyncStart: noop, |
29 | | - asyncEnd: noop, |
30 | | - error: noop, |
31 | | - traceSync: <T>(fn: (...args: Array<unknown>) => T): T => fn(), |
32 | | - }; |
33 | | - return channel; |
34 | | -} |
35 | | - |
36 | | -function fakeDc(): MinimalDiagnosticsChannel & { |
37 | | - created: Array<string>; |
38 | | -} { |
39 | | - const created: Array<string> = []; |
40 | | - const cache = new Map<string, MinimalTracingChannel>(); |
41 | | - return { |
42 | | - created, |
43 | | - tracingChannel(name: string) { |
44 | | - let existing = cache.get(name); |
45 | | - if (existing === undefined) { |
46 | | - created.push(name); |
47 | | - existing = fakeTracingChannel(name); |
48 | | - cache.set(name, existing); |
49 | | - } |
50 | | - return existing; |
51 | | - }, |
52 | | - }; |
53 | | -} |
54 | | - |
55 | 10 | describe('diagnostics', () => { |
56 | | - it('registers the five graphql tracing channels', () => { |
57 | | - const dc = fakeDc(); |
58 | | - enableDiagnosticsChannel(dc); |
59 | | - |
60 | | - expect(dc.created).to.deep.equal([ |
61 | | - 'graphql:execute', |
62 | | - 'graphql:parse', |
63 | | - 'graphql:validate', |
64 | | - 'graphql:resolve', |
65 | | - 'graphql:subscribe', |
66 | | - ]); |
| 11 | + it('exposes the five graphql tracing channels after registration', () => { |
| 12 | + enableDiagnosticsChannel(sharedFakeDc); |
67 | 13 |
|
68 | 14 | const channels = getChannels(); |
69 | 15 | invariant(channels !== undefined); |
70 | | - expect(channels.execute).to.not.equal(undefined); |
71 | | - expect(channels.parse).to.not.equal(undefined); |
72 | | - expect(channels.validate).to.not.equal(undefined); |
73 | | - expect(channels.resolve).to.not.equal(undefined); |
74 | | - expect(channels.subscribe).to.not.equal(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 | + ); |
75 | 31 | }); |
76 | 32 |
|
77 | | - it('re-registration with the same module preserves channel identity', () => { |
78 | | - const dc = fakeDc(); |
79 | | - enableDiagnosticsChannel(dc); |
| 33 | + it('re-registration with the same module is a no-op', () => { |
| 34 | + enableDiagnosticsChannel(sharedFakeDc); |
80 | 35 | const first = getChannels(); |
81 | 36 | invariant(first !== undefined); |
82 | 37 |
|
83 | | - enableDiagnosticsChannel(dc); |
| 38 | + enableDiagnosticsChannel(sharedFakeDc); |
84 | 39 | const second = getChannels(); |
85 | | - invariant(second !== undefined); |
86 | 40 |
|
87 | | - expect(second.execute).to.equal(first.execute); |
88 | | - expect(second.parse).to.equal(first.parse); |
89 | | - expect(second.validate).to.equal(first.validate); |
90 | | - expect(second.resolve).to.equal(first.resolve); |
91 | | - expect(second.subscribe).to.equal(first.subscribe); |
| 41 | + expect(second).to.equal(first); |
92 | 42 | }); |
93 | 43 |
|
94 | | - it('re-registration with a different module replaces stored references', () => { |
95 | | - const dc1 = fakeDc(); |
96 | | - const dc2 = fakeDc(); |
97 | | - |
98 | | - enableDiagnosticsChannel(dc1); |
99 | | - const first = getChannels(); |
100 | | - invariant(first !== undefined); |
101 | | - |
102 | | - enableDiagnosticsChannel(dc2); |
103 | | - const second = getChannels(); |
104 | | - invariant(second !== undefined); |
| 44 | + it('re-registration with a different module throws', () => { |
| 45 | + enableDiagnosticsChannel(sharedFakeDc); |
105 | 46 |
|
106 | | - expect(second.execute).to.not.equal(first.execute); |
| 47 | + expect(() => |
| 48 | + enableDiagnosticsChannel({ |
| 49 | + tracingChannel: () => { |
| 50 | + throw new Error('should not be called'); |
| 51 | + }, |
| 52 | + }), |
| 53 | + ).to.throw(/different `diagnostics_channel` module/); |
107 | 54 | }); |
108 | 55 | }); |
0 commit comments