-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathHeapSnapshotFormatter.test.ts
More file actions
97 lines (90 loc) · 2.53 KB
/
HeapSnapshotFormatter.test.ts
File metadata and controls
97 lines (90 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {describe, it} from 'node:test';
import {HeapSnapshotFormatter} from '../../src/formatters/HeapSnapshotFormatter.js';
import type {DevTools} from '../../src/third_party/index.js';
import {stableIdSymbol} from '../../src/utils/id.js';
describe('HeapSnapshotFormatter', () => {
const mockAggregates: Record<
string,
DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo
> = {
ObjectA: {
name: 'ObjectA',
count: 10,
self: 100,
maxRet: 1000,
distance: 1,
idxs: [],
[stableIdSymbol]: 1,
} as unknown as DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo,
ObjectB: {
name: 'ObjectB',
count: 5,
self: 50,
maxRet: 500,
distance: 2,
idxs: [],
[stableIdSymbol]: 2,
} as unknown as DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo,
};
describe('toString', () => {
it('formats data as CSV and sorts by retained size', t => {
const formatter = new HeapSnapshotFormatter(mockAggregates);
const result = formatter.toString();
t.assert.snapshot?.(result);
});
});
describe('toJSON', () => {
it('returns structured data sorted by retained size', () => {
const formatter = new HeapSnapshotFormatter(mockAggregates);
const result = formatter.toJSON();
assert.deepStrictEqual(result, [
{
uid: 1,
className: 'ObjectA',
count: 10,
selfSize: 100,
retainedSize: 1000,
},
{
uid: 2,
className: 'ObjectB',
count: 5,
selfSize: 50,
retainedSize: 500,
},
]);
});
});
describe('sort', () => {
it('sorts aggregates by retained size descending', () => {
const unsortedAggregates: Record<
string,
DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo
> = {
ObjectB: {
name: 'ObjectB',
self: 50,
maxRet: 500,
},
ObjectA: {
name: 'ObjectA',
self: 100,
maxRet: 1000,
},
} as unknown as Record<
string,
DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo
>;
const result = HeapSnapshotFormatter.sort(unsortedAggregates);
assert.strictEqual(result.length, 2);
assert.strictEqual(result[0][0], 'ObjectA');
assert.strictEqual(result[1][0], 'ObjectB');
});
});
});