Skip to content

Chrome DevTools for coding agents

test: share HeapSnapshotManager across read-only snapshot tests (#3165)

Alex Rudenko1dcb7f121bfc
diff --git a/tests/processors/HeapSnapshotManager.test.ts b/tests/processors/HeapSnapshotManager.test.ts
index 8a7da94f..c24cb886 100644
--- a/tests/processors/HeapSnapshotManager.test.ts
+++ b/tests/processors/HeapSnapshotManager.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {describe, it, afterEach} from 'node:test';
+import {describe, it, after, afterEach, before} from 'node:test';
import sinon from 'sinon';
@@ -46,12 +46,23 @@ describe('HeapSnapshotManager', () => {
sinon.assert.calledOnce(disposeSpy);
});
- it('assigns class diff indices that resolve to the same detailed diff', async () => {
- const manager = new HeapSnapshotManager();
- try {
- const filePathA = 'tests/fixtures/heap-1.heapsnapshot';
- const filePathB = 'tests/fixtures/heap-2.heapsnapshot';
+ // Each loaded snapshot starts a new worker, which takes seconds. Tests that
+ // only read snapshots share one manager, so each fixture loads one time.
+ describe('with heap-1, heap-2 and heap-3 fixtures', () => {
+ const filePathA = 'tests/fixtures/heap-1.heapsnapshot';
+ const filePathB = 'tests/fixtures/heap-2.heapsnapshot';
+ const filePathC = 'tests/fixtures/heap-3.heapsnapshot';
+ let manager: HeapSnapshotManager;
+
+ before(() => {
+ manager = new HeapSnapshotManager();
+ });
+
+ after(() => {
+ manager.dispose();
+ });
+ it('assigns class diff indices that resolve to the same detailed diff', async () => {
const {classDiffs} = await manager.getClassDiffs(filePathA, filePathB);
assert.deepStrictEqual(
classDiffs.map(diff => diff.index),
@@ -67,23 +78,14 @@ describe('HeapSnapshotManager', () => {
);
assert.strictEqual(detailedDiff.index, lastDiff.index);
assert.strictEqual(detailedDiff.className, lastDiff.className);
- } finally {
- manager.dispose();
- }
- });
-
- // Verifies that the caching mechanism in HeapSnapshotManager correctly
- // distinguishes comparisons when the same "current" snapshot is compared
- // against different "base" snapshots. If the cache key (diffCacheKey) is
- // not unique per base snapshot, the second comparison might incorrectly
- // return cached results from the first comparison.
- it('compares the same current snapshot against different bases', async () => {
- const manager = new HeapSnapshotManager();
- try {
- const filePathA = 'tests/fixtures/heap-1.heapsnapshot';
- const filePathB = 'tests/fixtures/heap-2.heapsnapshot';
- const filePathC = 'tests/fixtures/heap-3.heapsnapshot';
-
+ });
+
+ // Verifies that the caching mechanism in HeapSnapshotManager correctly
+ // distinguishes comparisons when the same "current" snapshot is compared
+ // against different "base" snapshots. If the cache key (diffCacheKey) is
+ // not unique per base snapshot, the second comparison might incorrectly
+ // return cached results from the first comparison.
+ it('compares the same current snapshot against different bases', async () => {
const firstDiff = await manager.getClassDiffs(filePathA, filePathC);
const secondDiff = await manager.getClassDiffs(filePathB, filePathC);
const firstNewObjectDiff = firstDiff.classDiffs.find(
@@ -96,102 +98,92 @@ describe('HeapSnapshotManager', () => {
assert.ok(secondNewObjectDiff);
assert.equal(firstNewObjectDiff.addedCount, 7);
assert.equal(secondNewObjectDiff.addedCount, 5);
- } finally {
- manager.dispose();
- }
+ });
+
+ it('throws when getDetailedClassDiff is called with an invalid classIndex', async () => {
+ await assert.rejects(
+ manager.getDetailedClassDiff(filePathA, filePathB, 99),
+ /Invalid classIndex: 99. Total classes with changes: 10/,
+ );
+ });
});
- it('throws when getNodesById is called with a non-existent class ID', async () => {
- const manager = new HeapSnapshotManager();
- try {
- const filePath = 'tests/fixtures/example.heapsnapshot';
+ describe('with example fixture', () => {
+ const filePath = 'tests/fixtures/example.heapsnapshot';
+ let manager: HeapSnapshotManager;
+
+ before(() => {
+ manager = new HeapSnapshotManager();
+ });
+
+ after(() => {
+ manager.dispose();
+ });
+
+ it('throws when getNodesById is called with a non-existent class ID', async () => {
await manager.getAggregates(filePath);
await assert.rejects(manager.getNodesById(filePath, 999999), {
message: 'Class with ID 999999 not found in heap snapshot',
});
- } finally {
- manager.dispose();
- }
- });
-
- it('resolves class IDs in getNodesById without calling getAggregates first', async () => {
- const manager1 = new HeapSnapshotManager();
- const manager2 = new HeapSnapshotManager();
- try {
- const filePath = 'tests/fixtures/example.heapsnapshot';
- const {aggregates} = await manager1.getAggregates(filePath);
- const functionAggregate = aggregates.find(
- a => a.className === 'Function',
- );
- assert.ok(functionAggregate);
- const classId = functionAggregate.id;
-
- const expectedNodes = await manager1.getNodesById(filePath, classId);
- const nodesWithoutPriorAggregates = await manager2.getNodesById(
- filePath,
- classId,
- );
-
- assert.ok(nodesWithoutPriorAggregates.items.length > 0);
- assert.deepStrictEqual(
- nodesWithoutPriorAggregates.items,
- expectedNodes.items,
- );
- } finally {
- manager1.dispose();
- manager2.dispose();
- }
- });
-
- it('assigns deterministic class IDs even when getAggregates is called with a filter first', async () => {
- const unfilteredFirstManager = new HeapSnapshotManager();
- const filteredFirstManager = new HeapSnapshotManager();
- try {
- const filePath = 'tests/fixtures/example.heapsnapshot';
- const unfilteredFirst =
- await unfilteredFirstManager.getAggregates(filePath);
- const unfilteredById = new Map(
- unfilteredFirst.aggregates.map(a => [a.id, a.className]),
- );
+ });
+
+ it('resolves class IDs in getNodesById without calling getAggregates first', async () => {
+ const freshManager = new HeapSnapshotManager();
+ try {
+ const {aggregates} = await manager.getAggregates(filePath);
+ const functionAggregate = aggregates.find(
+ a => a.className === 'Function',
+ );
+ assert.ok(functionAggregate);
+ const classId = functionAggregate.id;
- const filteredFirst = await filteredFirstManager.getAggregates(
- filePath,
- 'sharedNativeContext',
- );
- const unfilteredSecond =
- await filteredFirstManager.getAggregates(filePath);
+ const expectedNodes = await manager.getNodesById(filePath, classId);
+ const nodesWithoutPriorAggregates = await freshManager.getNodesById(
+ filePath,
+ classId,
+ );
- for (const aggregate of filteredFirst.aggregates) {
- assert.strictEqual(
- aggregate.className,
- unfilteredById.get(aggregate.id),
+ assert.ok(nodesWithoutPriorAggregates.items.length > 0);
+ assert.deepStrictEqual(
+ nodesWithoutPriorAggregates.items,
+ expectedNodes.items,
);
+ } finally {
+ freshManager.dispose();
}
+ });
+
+ it('assigns deterministic class IDs even when getAggregates is called with a filter first', async () => {
+ const filteredFirstManager = new HeapSnapshotManager();
+ try {
+ const unfilteredFirst = await manager.getAggregates(filePath);
+ const unfilteredById = new Map(
+ unfilteredFirst.aggregates.map(a => [a.id, a.className]),
+ );
- assert.deepStrictEqual(
- unfilteredSecond.aggregates,
- unfilteredFirst.aggregates,
- );
- } finally {
- unfilteredFirstManager.dispose();
- filteredFirstManager.dispose();
- }
- });
-
- it('throws when getDetailedClassDiff is called with an invalid classIndex', async () => {
- const manager = new HeapSnapshotManager();
- try {
- const filePathA = 'tests/fixtures/heap-1.heapsnapshot';
- const filePathB = 'tests/fixtures/heap-2.heapsnapshot';
-
- await assert.rejects(
- manager.getDetailedClassDiff(filePathA, filePathB, 99),
- /Invalid classIndex: 99. Total classes with changes: 10/,
- );
- } finally {
- manager.dispose();
- }
+ const filteredFirst = await filteredFirstManager.getAggregates(
+ filePath,
+ 'sharedNativeContext',
+ );
+ const unfilteredSecond =
+ await filteredFirstManager.getAggregates(filePath);
+
+ for (const aggregate of filteredFirst.aggregates) {
+ assert.strictEqual(
+ aggregate.className,
+ unfilteredById.get(aggregate.id),
+ );
+ }
+
+ assert.deepStrictEqual(
+ unfilteredSecond.aggregates,
+ unfilteredFirst.aggregates,
+ );
+ } finally {
+ filteredFirstManager.dispose();
+ }
+ });
});
it('gets stats and staticData, and disposes snapshot', async () => {