source: trip-planner-front/node_modules/eventemitter-asyncresource/test/test.ts@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[6a3a178]1import EventEmitterAsyncResource from '..';
2import { createHook, executionAsyncId, executionAsyncResource } from 'async_hooks';
3import { test } from 'tap';
4import { promisify } from 'util';
5
6const tick = promisify(setImmediate);
7
8interface InitAsyncEvent {
9 name : 'init',
10 type : string,
11 triggerAsyncId : number,
12 resource: object
13}
14interface OtherAsyncEvent {
15 name : 'before' | 'after' | 'destroy'
16}
17type AsyncEvent = InitAsyncEvent | OtherAsyncEvent;
18
19function makeHook (trackedTypes : string[]) {
20 const eventMap = new Map<number, AsyncEvent[]>();
21
22 function log (asyncId : number, name : 'before' | 'after' | 'destroy') {
23 const entry = eventMap.get(asyncId);
24 if (entry !== undefined) entry.push({ name });
25 }
26
27 const hook = createHook({
28 init (asyncId, type, triggerAsyncId, resource) {
29 if (trackedTypes.includes(type)) {
30 eventMap.set(asyncId, [
31 { name: 'init', type, triggerAsyncId, resource }
32 ]);
33 }
34 },
35
36 before (asyncId) { log(asyncId, 'before'); },
37 after (asyncId) { log(asyncId, 'after'); },
38 destroy (asyncId) { log(asyncId, 'destroy'); }
39 }).enable();
40 return {
41 done () {
42 hook.disable();
43 return new Set(eventMap.values());
44 },
45 ids () {
46 return new Set(eventMap.keys());
47 }
48 };
49}
50
51test('tracks emit() calls correctly using async_hooks', async ({ is, isDeep }) => {
52 const tracer = makeHook(['Foo']);
53
54 class Foo extends EventEmitterAsyncResource {}
55
56 const origExecutionAsyncId = executionAsyncId();
57 const foo = new Foo();
58
59 let called = false;
60 foo.on('someEvent', () => {
61 if (typeof executionAsyncResource === 'function') { // Node.js 12+ only
62 is(executionAsyncResource(), foo.asyncResource);
63 }
64 called = true;
65 });
66 foo.emit('someEvent');
67 is(called, true);
68
69 isDeep([foo.asyncId()], [...tracer.ids()]);
70 is(foo.triggerAsyncId(), origExecutionAsyncId);
71 is(foo.asyncResource.eventEmitter, foo);
72
73 foo.emitDestroy();
74
75 await tick();
76
77 isDeep(tracer.done(), new Set<AsyncEvent[]>([
78 [
79 { name: 'init', type: 'Foo', triggerAsyncId: origExecutionAsyncId, resource: foo.asyncResource },
80 { name: 'before' },
81 { name: 'after' },
82 { name: 'destroy' }
83 ]
84 ]));
85});
86
87test('can explicitly specify name as positional arg', async ({ isDeep }) => {
88 const tracer = makeHook(['ResourceName']);
89
90 const origExecutionAsyncId = executionAsyncId();
91 class Foo extends EventEmitterAsyncResource {}
92
93 const foo = new Foo('ResourceName');
94
95 isDeep(tracer.done(), new Set<AsyncEvent[]>([
96 [
97 { name: 'init', type: 'ResourceName', triggerAsyncId: origExecutionAsyncId, resource: foo.asyncResource }
98 ]
99 ]));
100});
101
102test('can explicitly specify name as option', async ({ isDeep }) => {
103 const tracer = makeHook(['ResourceName']);
104
105 const origExecutionAsyncId = executionAsyncId();
106 class Foo extends EventEmitterAsyncResource {}
107
108 const foo = new Foo({ name: 'ResourceName' });
109
110 isDeep(tracer.done(), new Set<AsyncEvent[]>([
111 [
112 { name: 'init', type: 'ResourceName', triggerAsyncId: origExecutionAsyncId, resource: foo.asyncResource }
113 ]
114 ]));
115});
116
117test('is provided as export on itself', async ({ is }) => {
118 is(EventEmitterAsyncResource.EventEmitterAsyncResource, EventEmitterAsyncResource);
119});
Note: See TracBrowser for help on using the repository browser.