|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { expectEvents } from '../expectEvents.js'; |
| 5 | +import { expectPromise } from '../expectPromise.js'; |
| 6 | + |
| 7 | +type TestTracingChannel = Parameters<typeof expectEvents>[0]; |
| 8 | + |
| 9 | +function createFakeTracingChannel(): TestTracingChannel { |
| 10 | + let handler: |
| 11 | + | { |
| 12 | + start: (context: unknown) => void; |
| 13 | + end: (context: unknown) => void; |
| 14 | + asyncStart: (context: unknown) => void; |
| 15 | + asyncEnd: (context: unknown) => void; |
| 16 | + error: (context: unknown) => void; |
| 17 | + } |
| 18 | + | undefined; |
| 19 | + |
| 20 | + function runStores<T>( |
| 21 | + context: object, |
| 22 | + fn: (this: object, ...args: Array<unknown>) => T, |
| 23 | + thisArg?: unknown, |
| 24 | + ...args: Array<unknown> |
| 25 | + ): T { |
| 26 | + return fn.apply((thisArg as object | undefined) ?? context, args); |
| 27 | + } |
| 28 | + |
| 29 | + return { |
| 30 | + hasSubscribers: false, |
| 31 | + subscribe(nextHandler) { |
| 32 | + handler = nextHandler; |
| 33 | + }, |
| 34 | + unsubscribe(nextHandler) { |
| 35 | + expect(handler).to.equal(nextHandler); |
| 36 | + handler = undefined; |
| 37 | + }, |
| 38 | + traceSync(fn, _context, thisArg, ...args) { |
| 39 | + return fn.apply(thisArg, args); |
| 40 | + }, |
| 41 | + start: { |
| 42 | + publish(context) { |
| 43 | + handler?.start(context); |
| 44 | + }, |
| 45 | + runStores, |
| 46 | + }, |
| 47 | + end: { |
| 48 | + publish(context) { |
| 49 | + handler?.end(context); |
| 50 | + }, |
| 51 | + runStores, |
| 52 | + }, |
| 53 | + asyncStart: { |
| 54 | + publish(context) { |
| 55 | + handler?.asyncStart(context); |
| 56 | + }, |
| 57 | + runStores, |
| 58 | + }, |
| 59 | + asyncEnd: { |
| 60 | + publish(context) { |
| 61 | + handler?.asyncEnd(context); |
| 62 | + }, |
| 63 | + runStores, |
| 64 | + }, |
| 65 | + error: { |
| 66 | + publish(context) { |
| 67 | + handler?.error(context); |
| 68 | + }, |
| 69 | + runStores, |
| 70 | + }, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +describe('expectEvents', () => { |
| 75 | + it('collects events and snapshots each published context', async () => { |
| 76 | + const channel = createFakeTracingChannel(); |
| 77 | + const context = { value: 1 }; |
| 78 | + |
| 79 | + await expectEvents( |
| 80 | + channel, |
| 81 | + () => { |
| 82 | + channel.start.publish(context); |
| 83 | + context.value = 2; |
| 84 | + channel.end.publish(context); |
| 85 | + return 'done'; |
| 86 | + }, |
| 87 | + (_result) => [ |
| 88 | + { |
| 89 | + channel: 'start', |
| 90 | + context: { value: 1 }, |
| 91 | + }, |
| 92 | + { |
| 93 | + channel: 'end', |
| 94 | + context: { value: 2 }, |
| 95 | + }, |
| 96 | + ], |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('unsubscribes when the callback rejects', async () => { |
| 101 | + let activeHandler: object | undefined; |
| 102 | + const error = new Error('boom'); |
| 103 | + const channel = createFakeTracingChannel(); |
| 104 | + const originalSubscribe = channel.subscribe; |
| 105 | + const originalUnsubscribe = channel.unsubscribe; |
| 106 | + |
| 107 | + channel.subscribe = (handler) => { |
| 108 | + activeHandler = handler; |
| 109 | + originalSubscribe.call(channel, handler); |
| 110 | + }; |
| 111 | + channel.unsubscribe = (handler) => { |
| 112 | + expect(handler).to.equal(activeHandler); |
| 113 | + activeHandler = undefined; |
| 114 | + originalUnsubscribe.call(channel, handler); |
| 115 | + }; |
| 116 | + |
| 117 | + expect( |
| 118 | + await expectPromise( |
| 119 | + expectEvents( |
| 120 | + channel, |
| 121 | + () => Promise.reject(error), |
| 122 | + () => [], |
| 123 | + ), |
| 124 | + ).toReject(), |
| 125 | + ).to.equal(error); |
| 126 | + expect(activeHandler).to.equal(undefined); |
| 127 | + }); |
| 128 | +}); |
0 commit comments