Skip to content

Commit 2cec424

Browse files
author
John Doe
committed
refactor: fix unit tests
1 parent 923659b commit 2cec424

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

packages/utils/src/lib/profiler/profiler.unit.test.ts

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ describe('Profiler', () => {
99
beforeEach(() => {
1010
performance.clearMarks();
1111
performance.clearMeasures();
12+
delete process.env.CP_PROFILING;
1213

1314
profiler = new Profiler({
1415
prefix: 'cp',
1516
track: 'test-track',
16-
color: 'primary',
1717
tracks: {},
1818
});
1919
});
@@ -183,11 +183,78 @@ describe('Profiler', () => {
183183
expect(marks).toHaveLength(0);
184184
});
185185

186+
it('marker should execute without error when enabled with default color', () => {
187+
performance.clearMarks();
188+
189+
const profilerWithColor = new Profiler({
190+
prefix: 'cp',
191+
track: 'test-track',
192+
color: 'primary',
193+
tracks: {},
194+
});
195+
profilerWithColor.setEnabled(true);
196+
197+
expect(() => {
198+
profilerWithColor.marker('test-marker-default-color', {
199+
tooltipText: 'Test marker with default color',
200+
});
201+
}).not.toThrow();
202+
203+
const marks = performance.getEntriesByType('mark');
204+
expect(marks).toStrictEqual([
205+
expect.objectContaining({
206+
name: 'test-marker-default-color',
207+
detail: {
208+
devtools: expect.objectContaining({
209+
dataType: 'marker',
210+
color: 'primary', // Should use default color
211+
tooltipText: 'Test marker with default color',
212+
}),
213+
},
214+
}),
215+
]);
216+
});
217+
218+
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+
});
224+
profilerNoColor.setEnabled(true);
225+
226+
expect(() => {
227+
profilerNoColor.marker('test-marker-no-color', {
228+
color: 'secondary',
229+
tooltipText: 'Test marker without default color',
230+
properties: [['key', 'value']],
231+
});
232+
}).not.toThrow();
233+
234+
const marks = performance.getEntriesByType('mark');
235+
expect(marks).toStrictEqual([
236+
expect.objectContaining({
237+
name: 'test-marker-no-color',
238+
detail: {
239+
devtools: expect.objectContaining({
240+
dataType: 'marker',
241+
color: 'secondary',
242+
tooltipText: 'Test marker without default color',
243+
properties: [['key', 'value']],
244+
}),
245+
},
246+
}),
247+
]);
248+
});
249+
186250
it('measure should execute work and return result when enabled', () => {
251+
performance.clearMarks();
252+
performance.clearMeasures();
253+
187254
profiler.setEnabled(true);
188255

189256
const workFn = vi.fn(() => 'result');
190-
const result = profiler.measure('test-event', workFn);
257+
const result = profiler.measure('test-event', workFn, { color: 'primary' });
191258

192259
expect(result).toBe('result');
193260
expect(workFn).toHaveBeenCalled();
@@ -203,7 +270,6 @@ describe('Profiler', () => {
203270
devtools: expect.objectContaining({
204271
dataType: 'track-entry',
205272
track: 'test-track',
206-
color: 'primary',
207273
}),
208274
},
209275
}),
@@ -213,7 +279,6 @@ describe('Profiler', () => {
213279
devtools: expect.objectContaining({
214280
dataType: 'track-entry',
215281
track: 'test-track',
216-
color: 'primary',
217282
}),
218283
},
219284
}),
@@ -226,7 +291,6 @@ describe('Profiler', () => {
226291
devtools: expect.objectContaining({
227292
dataType: 'track-entry',
228293
track: 'test-track',
229-
color: 'primary',
230294
}),
231295
},
232296
}),
@@ -280,7 +344,9 @@ describe('Profiler', () => {
280344
return 'async-result';
281345
});
282346

283-
const result = await profiler.measureAsync('test-async-event', workFn);
347+
const result = await profiler.measureAsync('test-async-event', workFn, {
348+
color: 'primary',
349+
});
284350

285351
expect(result).toBe('async-result');
286352
expect(workFn).toHaveBeenCalled();

packages/utils/src/lib/user-timing-extensibility-api-utils.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,7 @@ export function setupTracks<
356356
* @returns The mark options without dataType, tooltipText and properties.
357357
*/
358358
function toMarkMeasureOpts<T extends TrackEntryPayload>(devtools: T) {
359-
const {
360-
dataType: _,
361-
tooltipText: __,
362-
properties: ___,
363-
...markDevtools
364-
} = devtools;
359+
const { tooltipText: _, properties: __, ...markDevtools } = devtools;
365360
return { detail: { devtools: markDevtools } };
366361
}
367362

testing/test-utils/src/lib/utils/perf-hooks.mock.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,34 @@ export const createPerformanceMock = (timeOrigin = 500_000) => ({
3333

3434
now: vi.fn(() => nowMs),
3535

36-
mark: vi.fn((name: string) => {
36+
mark: vi.fn((name: string, options?: { detail?: unknown }) => {
3737
entries.push({
3838
name,
3939
entryType: 'mark',
4040
startTime: nowMs,
4141
duration: 0,
42+
...(options?.detail ? { detail: options.detail } : {}),
4243
} as PerformanceEntry);
4344
MockPerformanceObserver.globalEntries = entries;
4445
}),
4546

46-
measure: vi.fn((name: string, startMark?: string, endMark?: string) => {
47-
const entry = {
48-
name,
49-
entryType: 'measure',
50-
startTime: nowMs,
51-
duration: nowMs,
52-
} as PerformanceEntry;
53-
entries.push(entry);
54-
MockPerformanceObserver.globalEntries = entries;
55-
triggerObservers([entry]);
56-
}),
47+
measure: vi.fn(
48+
(
49+
name: string,
50+
options?: { start?: string; end?: string; detail?: unknown },
51+
) => {
52+
const entry = {
53+
name,
54+
entryType: 'measure',
55+
startTime: nowMs,
56+
duration: nowMs,
57+
...(options?.detail ? { detail: options.detail } : {}),
58+
} as PerformanceEntry;
59+
entries.push(entry);
60+
MockPerformanceObserver.globalEntries = entries;
61+
triggerObservers([entry]);
62+
},
63+
),
5764

5865
getEntries: vi.fn(() => entries.slice()),
5966

0 commit comments

Comments
 (0)