Skip to content

Commit 2aff877

Browse files
author
John Doe
committed
refactor: add APIs
1 parent 5d779e8 commit 2aff877

File tree

2 files changed

+87
-55
lines changed

2 files changed

+87
-55
lines changed

packages/utils/src/perf-hooks.d.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ type DetailPayloadWithDevtools = WithDevToolsPayload<
1111
>;
1212

1313
declare module 'node:perf_hooks' {
14+
interface PerformanceEntry {
15+
readonly detail?: DetailPayloadWithDevtools;
16+
}
17+
18+
interface MarkEntry extends PerformanceMark {
19+
readonly entryType: 'mark';
20+
readonly detail?: DetailPayloadWithDevtools;
21+
}
22+
23+
interface MeasureEntry extends PerformanceMeasure {
24+
readonly entryType: 'measure';
25+
readonly detail?: DetailPayloadWithDevtools;
26+
}
27+
28+
interface PerformanceMark extends PerformanceEntry {}
29+
interface PerformanceMeasure extends PerformanceEntry {}
30+
1431
export interface PerformanceMarkOptions {
1532
detail?: DetailPayloadWithDevtools;
1633
startTime?: DOMHighResTimeStamp;
@@ -24,12 +41,12 @@ declare module 'node:perf_hooks' {
2441
}
2542

2643
const performance: {
27-
mark(name: string, options?: PerformanceMarkOptions): PerformanceMark;
44+
mark: (name: string, options?: PerformanceMarkOptions) => PerformanceMark;
2845

29-
measure(
46+
measure: (
3047
name: string,
3148
startOrOptions?: string | number | PerformanceMeasureOptions,
3249
end?: string | number,
33-
): PerformanceMeasure;
50+
) => PerformanceMeasure;
3451
};
3552
}
Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,90 @@
11
import { type PerformanceMarkOptions, performance } from 'node:perf_hooks';
2-
import { describe, expectTypeOf, it } from 'vitest';
2+
import { describe, expect, expectTypeOf, it } from 'vitest';
33

44
describe('perf-hooks definitions', () => {
55
it('PerformanceMarkOptions should be type safe', () => {
6-
expectTypeOf<{
7-
startTime: number;
8-
detail: {
9-
devtools: {
10-
dataType: 'marker';
11-
color: 'error';
6+
expect(() =>
7+
expectTypeOf<{
8+
startTime: number;
9+
detail: {
10+
devtools: {
11+
dataType: 'marker';
12+
color: 'error';
13+
};
1214
};
13-
};
14-
}>().toMatchTypeOf<PerformanceMarkOptions>();
15-
expectTypeOf<{
16-
startTime: number;
17-
detail: {
18-
devtools: {
19-
dataType: 'markerr';
15+
}>().toMatchTypeOf<PerformanceMarkOptions>(),
16+
).not.toThrow();
17+
18+
expect(() =>
19+
expectTypeOf<{
20+
startTime: number;
21+
detail: {
22+
devtools: {
23+
dataType: 'markerr';
24+
};
2025
};
21-
};
22-
}>().not.toMatchTypeOf<PerformanceMarkOptions>();
26+
}>().not.toMatchTypeOf<PerformanceMarkOptions>(),
27+
).not.toThrow();
2328
});
2429

2530
it('perf_hooks.mark should be type safe', () => {
26-
performance.mark('name', {
27-
detail: {
28-
devtools: {
29-
dataType: 'marker',
30-
color: 'error',
31+
expect(() =>
32+
performance.mark('name', {
33+
detail: {
34+
devtools: {
35+
dataType: 'marker',
36+
color: 'error',
37+
},
3138
},
32-
},
33-
});
39+
}),
40+
).not.toThrow();
3441

35-
performance.mark('name', {
36-
detail: {
37-
devtools: {
38-
/* @ts-expect-error - dataType should be marker | track */
39-
dataType: 'markerrr',
40-
color: 'error',
42+
expect(() =>
43+
performance.mark('name', {
44+
detail: {
45+
devtools: {
46+
// @ts-expect-error - dataType should be marker | track
47+
dataType: 'markerrr',
48+
color: 'error',
49+
},
4150
},
42-
},
43-
});
51+
}),
52+
).not.toThrow();
4453
});
4554

4655
it('PerformanceMeasureOptions should be type safe', () => {
47-
expectTypeOf<{
48-
start: string;
49-
end: string;
50-
detail: {
51-
devtools: {
52-
dataType: 'track-entry';
53-
track: 'test-track';
54-
color: 'primary';
56+
expect(() =>
57+
expectTypeOf<{
58+
start: string;
59+
end: string;
60+
detail: {
61+
devtools: {
62+
dataType: 'track-entry';
63+
track: 'test-track';
64+
color: 'primary';
65+
};
5566
};
56-
};
57-
}>().toMatchTypeOf<PerformanceMeasureOptions>();
67+
}>().toMatchTypeOf<PerformanceMeasureOptions>(),
68+
).not.toThrow();
5869
});
5970

6071
it('perf_hooks.measure should be type safe', () => {
61-
performance.measure('measure-name', 'start-mark', 'end-mark');
72+
expect(() =>
73+
performance.measure('measure-name', 'start-mark', 'end-mark'),
74+
).not.toThrow();
6275

63-
performance.measure('measure-name', {
64-
start: 'start-mark',
65-
end: 'end-mark',
66-
detail: {
67-
/* @ts-expect-error - track is required */
68-
devtools: {
69-
dataType: 'track-entry',
70-
color: 'primary',
76+
expect(() =>
77+
performance.measure('measure-name', {
78+
start: 'start-mark',
79+
end: 'end-mark',
80+
detail: {
81+
// @ts-expect-error - track is required
82+
devtools: {
83+
dataType: 'track-entry',
84+
color: 'primary',
85+
},
7186
},
72-
},
73-
});
87+
}),
88+
).not.toThrow();
7489
});
7590
});

0 commit comments

Comments
 (0)