11import { performance } from 'node:perf_hooks' ;
22import { beforeEach , describe , expect , it , vi } from 'vitest' ;
33import type { ActionTrackEntryPayload } from '../user-timing-extensibility-api.type.js' ;
4- import { Profiler } from './profiler.js' ;
4+ import { Profiler , type ProfilerOptions } from './profiler.js' ;
55
66describe ( 'Profiler' , ( ) => {
7+ const getProfiler = ( overrides ?: Partial < ProfilerOptions > ) =>
8+ new Profiler ( {
9+ prefix : 'cp' ,
10+ track : 'test-track' ,
11+ ...overrides ,
12+ } ) ;
13+
714 let profiler : Profiler < Record < string , ActionTrackEntryPayload > > ;
815
916 beforeEach ( ( ) => {
1017 performance . clearMarks ( ) ;
1118 performance . clearMeasures ( ) ;
19+ // eslint-disable-next-line functional/immutable-data
1220 delete process . env . CP_PROFILING ;
1321
14- profiler = new Profiler ( {
15- prefix : 'cp' ,
16- track : 'test-track' ,
17- tracks : { } ,
18- } ) ;
22+ profiler = getProfiler ( ) ;
1923 } ) ;
2024
2125 it ( 'constructor should initialize with default enabled state from env' , ( ) => {
2226 vi . stubEnv ( 'CP_PROFILING' , 'true' ) ;
23- const profilerWithEnv = new Profiler ( {
24- prefix : 'cp' ,
25- track : 'test-track' ,
26- tracks : { } ,
27- } ) ;
27+ const profilerWithEnv = getProfiler ( ) ;
2828
2929 expect ( profilerWithEnv . isEnabled ( ) ) . toBe ( true ) ;
3030 } ) ;
@@ -34,20 +34,14 @@ describe('Profiler', () => {
3434 const profilerWithOverride = new Profiler ( {
3535 prefix : 'cp' ,
3636 track : 'test-track' ,
37- tracks : { } ,
3837 enabled : true ,
3938 } ) ;
4039
4140 expect ( profilerWithOverride . isEnabled ( ) ) . toBe ( true ) ;
4241 } ) ;
4342
4443 it ( 'constructor should use defaults for measure' , ( ) => {
45- const customProfiler = new Profiler ( {
46- prefix : 'custom' ,
47- track : 'custom-track' ,
48- trackGroup : 'custom-group' ,
49- color : 'secondary' ,
50- } ) ;
44+ const customProfiler = getProfiler ( { color : 'secondary' } ) ;
5145
5246 customProfiler . setEnabled ( true ) ;
5347
@@ -61,23 +55,21 @@ describe('Profiler', () => {
6155 expect ( marks ) . toStrictEqual (
6256 expect . arrayContaining ( [
6357 expect . objectContaining ( {
64- name : 'custom :test-operation:start' ,
58+ name : 'cp :test-operation:start' ,
6559 detail : {
6660 devtools : expect . objectContaining ( {
6761 dataType : 'track-entry' ,
68- track : 'custom-track' ,
69- trackGroup : 'custom-group' ,
62+ track : 'test-track' ,
7063 color : 'secondary' ,
7164 } ) ,
7265 } ,
7366 } ) ,
7467 expect . objectContaining ( {
75- name : 'custom :test-operation:end' ,
68+ name : 'cp :test-operation:end' ,
7669 detail : {
7770 devtools : expect . objectContaining ( {
7871 dataType : 'track-entry' ,
79- track : 'custom-track' ,
80- trackGroup : 'custom-group' ,
72+ track : 'test-track' ,
8173 color : 'secondary' ,
8274 } ) ,
8375 } ,
@@ -86,12 +78,11 @@ describe('Profiler', () => {
8678 ) ;
8779 expect ( measures ) . toStrictEqual ( [
8880 expect . objectContaining ( {
89- name : 'custom :test-operation' ,
81+ name : 'cp :test-operation' ,
9082 detail : {
9183 devtools : expect . objectContaining ( {
9284 dataType : 'track-entry' ,
93- track : 'custom-track' ,
94- trackGroup : 'custom-group' ,
85+ track : 'test-track' ,
9586 color : 'secondary' ,
9687 } ) ,
9788 } ,
@@ -186,12 +177,7 @@ describe('Profiler', () => {
186177 it ( 'marker should execute without error when enabled with default color' , ( ) => {
187178 performance . clearMarks ( ) ;
188179
189- const profilerWithColor = new Profiler ( {
190- prefix : 'cp' ,
191- track : 'test-track' ,
192- color : 'primary' ,
193- tracks : { } ,
194- } ) ;
180+ const profilerWithColor = getProfiler ( { color : 'primary' } ) ;
195181 profilerWithColor . setEnabled ( true ) ;
196182
197183 expect ( ( ) => {
@@ -216,11 +202,7 @@ describe('Profiler', () => {
216202 } ) ;
217203
218204 it ( 'marker should execute without error when enabled with no default color' , ( ) => {
219- const profilerNoColor = new Profiler ( {
220- prefix : 'cp' ,
221- track : 'test-track' ,
222- tracks : { } ,
223- } ) ;
205+ const profilerNoColor = getProfiler ( ) ;
224206 profilerNoColor . setEnabled ( true ) ;
225207
226208 expect ( ( ) => {
0 commit comments