Skip to content

Commit 313a489

Browse files
author
John Doe
committed
refactor: wip
1 parent e4f326b commit 313a489

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

packages/utils/src/lib/trace-file-utils.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const getInstantEvent = (opt: {
4848
ts?: number;
4949
pid?: number;
5050
tid?: number;
51-
args: InstantEventArgs;
51+
args?: InstantEventArgs;
5252
}): InstantEvent => ({
5353
cat: 'blink.user_timing',
5454
ph: 'i',
@@ -113,7 +113,7 @@ type SpanOpt = {
113113
ts?: number;
114114
pid?: number;
115115
tid?: number;
116-
args: SpanEventArgs;
116+
args?: SpanEventArgs;
117117
};
118118

119119
export function getSpanEvent(ph: 'b', opt: SpanOpt): BeginEvent;
@@ -138,7 +138,7 @@ export const getSpan = (opt: {
138138
id2?: { local: string };
139139
pid?: number;
140140
tid?: number;
141-
args: SpanEventArgs;
141+
args?: SpanEventArgs;
142142
tsMarkerPadding?: number;
143143
}): [BeginEvent, EndEvent] => {
144144
// tsMarkerPadding is here to make the measure slightly smaller so the markers align perfectly.
@@ -154,15 +154,11 @@ export const getSpan = (opt: {
154154
...opt,
155155
id2,
156156
ts: opt.tsB + pad,
157-
name: opt.name,
158-
args: opt.args,
159157
}),
160158
getSpanEvent('e', {
161159
...opt,
162160
id2,
163161
ts: opt.tsE - pad,
164-
name: opt.name,
165-
args: opt.args,
166162
}),
167163
];
168164
};
@@ -175,7 +171,7 @@ export const markToInstantEvent = (
175171
...opt,
176172
name: opt?.name ?? entry.name,
177173
ts: defaultClock.fromEntryStartTimeMs(entry.startTime),
178-
args: entry.detail ? { detail: entry.detail } : {},
174+
args: entry.detail ? { detail: entry.detail } : undefined,
179175
});
180176

181177
export const measureToSpanEvents = (
@@ -187,7 +183,7 @@ export const measureToSpanEvents = (
187183
name: opt?.name ?? entry.name,
188184
tsB: entryToTraceTimestamp(entry),
189185
tsE: entryToTraceTimestamp(entry, true),
190-
args: entry.detail ? { data: { detail: entry.detail } } : {},
186+
args: entry.detail ? { data: { detail: entry.detail } } : undefined,
191187
});
192188

193189
export const getTraceFile = (opt: {

packages/utils/src/lib/trace-file-utils.unit.test.ts

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,41 @@ describe('getTraceFile', () => {
6666
startTime: '2023-01-01T00:00:00.000Z',
6767
});
6868

69-
expect(result.metadata?.startTime).toBe('2023-01-01T00:00:00.000Z');
69+
expect(result).toHaveProperty(
70+
'metadata',
71+
expect.objectContaining({
72+
startTime: '2023-01-01T00:00:00.000Z',
73+
}),
74+
);
7075
});
7176

7277
it('should include hardware concurrency', () => {
73-
expect(
74-
getTraceFile({ traceEvents: [] }).metadata?.hardwareConcurrency,
75-
).toBeGreaterThan(0);
78+
expect(getTraceFile({ traceEvents: [] })).toHaveProperty(
79+
'metadata',
80+
expect.objectContaining({
81+
hardwareConcurrency: expect.any(Number),
82+
}),
83+
);
7684
});
7785
});
7886

7987
describe('frameTreeNodeId', () => {
80-
it('should generate correct frame tree node ID', () => {
81-
expect(frameTreeNodeId(123, 456)).toBe(1_230_456);
82-
expect(frameTreeNodeId(1, 2)).toBe(102);
83-
expect(frameTreeNodeId(999, 999)).toBe(9_990_999);
88+
it.each([
89+
[123, 456, 1_230_456],
90+
[1, 2, 102],
91+
[999, 999, 9_990_999],
92+
])('should generate correct frame tree node ID', (pid, tid, expected) => {
93+
expect(frameTreeNodeId(pid, tid)).toBe(expected);
8494
});
8595
});
8696

8797
describe('frameName', () => {
88-
it('should generate correct frame name', () => {
89-
expect(frameName(123, 456)).toBe('FRAME0P123T456');
90-
expect(frameName(1, 2)).toBe('FRAME0P1T2');
91-
expect(frameName(999, 999)).toBe('FRAME0P999T999');
98+
it.each([
99+
[123, 456],
100+
[1, 2],
101+
[999, 999],
102+
])('should generate correct frame name', (pid, tid) => {
103+
expect(frameName(pid, tid)).toBe(`FRAME0P${pid}T${tid}`);
92104
});
93105
});
94106

@@ -338,13 +350,26 @@ describe('measureToSpanEvents', () => {
338350
},
339351
);
340352

341-
expect(result).toHaveLength(2);
342-
expect(result[0].name).toBe('custom-measure');
343-
expect(result[0].pid).toBe(777);
344-
expect(result[0].tid).toBe(666);
345-
expect(result[0].args).toStrictEqual({
346-
data: { detail: { measurement: 'data' } },
347-
});
353+
expect(result).toStrictEqual([
354+
expect.objectContaining({
355+
name: 'custom-measure',
356+
pid: 777,
357+
tid: 666,
358+
args: { data: { detail: { measurement: 'data' } } },
359+
}),
360+
expect.objectContaining({
361+
name: 'custom-measure',
362+
pid: 777,
363+
tid: 666,
364+
args: { data: { detail: { measurement: 'data' } } },
365+
}),
366+
expect.objectContaining({
367+
name: 'custom-measure',
368+
pid: 777,
369+
tid: 666,
370+
args: { data: { detail: { measurement: 'data' } } },
371+
}),
372+
]);
348373
});
349374
});
350375

@@ -430,6 +455,7 @@ describe('getSpan', () => {
430455
tsB: 1000,
431456
tsE: 1500,
432457
tsMarkerPadding: 5,
458+
args: {},
433459
});
434460

435461
expect(result).toStrictEqual([

0 commit comments

Comments
 (0)