|
1 | 1 | import { type PerformanceEntry, performance } from 'node:perf_hooks'; |
2 | | -import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + type MockedFunction, |
| 4 | + beforeEach, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + it, |
| 8 | + vi, |
| 9 | +} from 'vitest'; |
| 10 | +import { MockPerformanceObserver } from '@code-pushup/test-utils'; |
3 | 11 | import { MockSink } from '../../mocks/sink.mock'; |
4 | 12 | import { |
5 | 13 | type PerformanceObserverOptions, |
6 | 14 | PerformanceObserverSink, |
7 | 15 | } from './performance-observer.js'; |
8 | | -import type { Sink } from './sink-source.types'; |
9 | 16 |
|
10 | 17 | describe('PerformanceObserverSink', () => { |
| 18 | + let encode: MockedFunction<(entry: PerformanceEntry) => string[]>; |
11 | 19 | let sink: MockSink; |
12 | 20 | let options: PerformanceObserverOptions<string>; |
13 | 21 |
|
| 22 | + const awaitObserverCallback = () => |
| 23 | + new Promise(resolve => setTimeout(resolve, 10)); |
| 24 | + |
14 | 25 | beforeEach(() => { |
15 | | - vi.clearAllMocks(); |
16 | | - performance.clearMeasures(); |
17 | | - performance.clearMarks(); |
18 | 26 | sink = new MockSink(); |
| 27 | + encode = vi.fn((entry: PerformanceEntry) => [ |
| 28 | + `${entry.name}:${entry.entryType}`, |
| 29 | + ]); |
19 | 30 |
|
20 | 31 | options = { |
21 | 32 | sink, |
22 | | - encode: vi.fn((entry: PerformanceEntry) => [ |
23 | | - `${entry.name}:${entry.entryType}`, |
24 | | - ]), |
| 33 | + encode, |
25 | 34 | }; |
26 | | - }); |
27 | 35 |
|
28 | | - afterEach(() => { |
29 | | - vi.restoreAllMocks(); |
| 36 | + performance.clearMarks(); |
| 37 | + performance.clearMeasures(); |
30 | 38 | }); |
31 | 39 |
|
32 | | - it('creates instance with default options', () => { |
| 40 | + it('creates instance with required options', () => { |
33 | 41 | expect(() => new PerformanceObserverSink(options)).not.toThrow(); |
34 | 42 | }); |
35 | 43 |
|
36 | | - it('creates instance with custom options', () => { |
37 | | - expect( |
38 | | - () => |
39 | | - new PerformanceObserverSink({ |
40 | | - ...options, |
41 | | - buffered: true, |
42 | | - flushThreshold: 10, |
43 | | - }), |
44 | | - ).not.toThrow(); |
| 44 | + it('internal PerformanceObserver should process observed entries', () => { |
| 45 | + const observer = new PerformanceObserverSink(options); |
| 46 | + observer.subscribe(); |
| 47 | + |
| 48 | + performance.mark('test-mark'); |
| 49 | + performance.measure('test-measure'); |
| 50 | + observer.flush(); |
| 51 | + expect(encode).toHaveBeenCalledTimes(2); |
| 52 | + expect(encode).toHaveBeenNthCalledWith( |
| 53 | + 1, |
| 54 | + expect.objectContaining({ |
| 55 | + name: 'test-mark', |
| 56 | + entryType: 'mark', |
| 57 | + }), |
| 58 | + ); |
| 59 | + expect(encode).toHaveBeenNthCalledWith( |
| 60 | + 2, |
| 61 | + expect.objectContaining({ |
| 62 | + name: 'test-measure', |
| 63 | + entryType: 'measure', |
| 64 | + }), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('internal PerformanceObserver calls flush if flushThreshold exceeded', async () => { |
| 69 | + const observer = new PerformanceObserverSink({ |
| 70 | + ...options, |
| 71 | + flushThreshold: 3, |
| 72 | + }); |
| 73 | + observer.subscribe(); |
| 74 | + |
| 75 | + performance.mark('test-mark1'); |
| 76 | + performance.mark('test-mark2'); |
| 77 | + performance.mark('test-mark3'); |
| 78 | + |
| 79 | + await awaitObserverCallback(); |
| 80 | + |
| 81 | + expect(encode).toHaveBeenCalledTimes(3); |
| 82 | + }); |
| 83 | + |
| 84 | + it('flush flushes observed entries when subscribed', () => { |
| 85 | + const observer = new PerformanceObserverSink(options); |
| 86 | + observer.subscribe(); |
| 87 | + |
| 88 | + performance.mark('test-mark1'); |
| 89 | + performance.mark('test-mark2'); |
| 90 | + expect(sink.getWrittenItems()).toStrictEqual([]); |
| 91 | + |
| 92 | + observer.flush(); |
| 93 | + expect(sink.getWrittenItems()).toStrictEqual([ |
| 94 | + 'test-mark1:mark', |
| 95 | + 'test-mark2:mark', |
| 96 | + ]); |
| 97 | + }); |
| 98 | + |
| 99 | + it('flush calls encode for each entry', () => { |
| 100 | + const observer = new PerformanceObserverSink(options); |
| 101 | + observer.subscribe(); |
| 102 | + |
| 103 | + performance.mark('test-mark1'); |
| 104 | + performance.mark('test-mark2'); |
| 105 | + |
| 106 | + observer.flush(); |
| 107 | + |
| 108 | + expect(encode).toHaveBeenCalledWith( |
| 109 | + expect.objectContaining({ |
| 110 | + name: 'test-mark1', |
| 111 | + entryType: 'mark', |
| 112 | + }), |
| 113 | + ); |
| 114 | + expect(encode).toHaveBeenCalledWith( |
| 115 | + expect.objectContaining({ |
| 116 | + name: 'test-mark2', |
| 117 | + entryType: 'mark', |
| 118 | + }), |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('unsubscribe stops observing performance entries', async () => { |
| 123 | + const observer = new PerformanceObserverSink({ |
| 124 | + ...options, |
| 125 | + flushThreshold: 1, |
| 126 | + }); |
| 127 | + |
| 128 | + observer.subscribe(); |
| 129 | + performance.mark('subscribed-mark1'); |
| 130 | + performance.mark('subscribed-mark2'); |
| 131 | + await awaitObserverCallback(); |
| 132 | + expect(encode).toHaveBeenCalledTimes(2); |
| 133 | + |
| 134 | + observer.unsubscribe(); |
| 135 | + performance.mark('unsubscribed-mark1'); |
| 136 | + performance.mark('unsubscribed-mark2'); |
| 137 | + await awaitObserverCallback(); |
| 138 | + expect(encode).toHaveBeenCalledTimes(2); |
45 | 139 | }); |
46 | 140 |
|
47 | 141 | it('should observe performance entries and write them to the sink on flush', () => { |
|
0 commit comments