Skip to content

Commit 1fa3aca

Browse files
do not share the snapshots + tests
1 parent 942d9af commit 1fa3aca

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

v-next/hardhat-network-helpers/src/internal/network-helpers/helpers/load-fixture.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import type { Fixture, NetworkHelpers, Snapshot } from "../../../types.js";
22

33
import { HardhatError } from "@nomicfoundation/hardhat-errors";
44

5-
let snapshots: Array<Snapshot<any>> = [];
6-
75
export async function loadFixture<T>(
86
networkHelpers: NetworkHelpers,
97
fixture: Fixture<T>,
8+
snapshots: Array<Snapshot<any>>,
109
): Promise<T> {
1110
if (fixture.name === "") {
1211
throw new HardhatError(
@@ -20,10 +19,14 @@ export async function loadFixture<T>(
2019
try {
2120
await snapshot.restorer.restore();
2221

23-
snapshots = snapshots.filter(
22+
const filteredSnapshots = snapshots.filter(
2423
(s) =>
2524
Number(s.restorer.snapshotId) <= Number(snapshot.restorer.snapshotId),
2625
);
26+
27+
// Modify the array by reference
28+
snapshots.length = 0;
29+
snapshots.push(...filteredSnapshots);
2730
} catch (e) {
2831
if (
2932
HardhatError.isHardhatError(e) &&
@@ -52,6 +55,7 @@ export async function loadFixture<T>(
5255
}
5356
}
5457

55-
export function clearSnapshots(): void {
56-
snapshots = [];
58+
export function clearSnapshots(snapshots: Array<Snapshot<any>>): void {
59+
// Modify the array by reference
60+
snapshots.length = 0;
5761
}

v-next/hardhat-network-helpers/src/internal/network-helpers/network-helpers.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
Fixture,
44
NetworkHelpers as NetworkHelpersI,
55
NumberLike,
6+
Snapshot,
67
SnapshotRestorer,
78
} from "../../types.js";
89
import type { EthereumProvider } from "hardhat/types/providers";
@@ -36,6 +37,7 @@ const SUPPORTED_TEST_NETWORKS = ["hardhat", "zksync", "anvil"];
3637
export class NetworkHelpers implements NetworkHelpersI {
3738
readonly #provider: EthereumProvider;
3839
readonly #networkName: string;
40+
readonly #snapshots: Array<Snapshot<any>> = [];
3941

4042
#isDevelopmentNetwork: boolean | undefined;
4143
#version: string | undefined;
@@ -50,7 +52,7 @@ export class NetworkHelpers implements NetworkHelpersI {
5052
}
5153

5254
public clearSnapshots(): void {
53-
clearSnapshots();
55+
clearSnapshots(this.#snapshots);
5456
}
5557

5658
public async dropTransaction(txHash: string): Promise<boolean> {
@@ -74,7 +76,7 @@ export class NetworkHelpers implements NetworkHelpersI {
7476

7577
public async loadFixture<T>(fixture: Fixture<T>): Promise<T> {
7678
await this.throwIfNotDevelopmentNetwork();
77-
return loadFixture(this, fixture);
79+
return loadFixture(this, fixture, this.#snapshots);
7880
}
7981

8082
public async mine(

v-next/hardhat-network-helpers/test/network-helpers/load-fixture.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NetworkHelpers } from "../../src/types.js";
22

33
import assert from "node:assert/strict";
4-
import { before, describe, it } from "node:test";
4+
import { before, beforeEach, describe, it } from "node:test";
55

66
import {
77
assertHardhatInvariant,
@@ -153,4 +153,34 @@ describe("network-helpers - loadFixture", () => {
153153
{},
154154
);
155155
});
156+
157+
describe("multiple different connections should use different fixtures", () => {
158+
// This suite verifies that each new network connection gets its own "snapshots" array.
159+
// Because we create a fresh "networkHelpers" instance for every test,
160+
// the fixture should run for each test and not be cached across connections.
161+
162+
let calledCount = 0;
163+
164+
async function mineBlockFixture() {
165+
calledCount++;
166+
}
167+
168+
beforeEach(async () => {
169+
({ networkHelpers } = await initializeNetwork());
170+
});
171+
172+
it("should execute the fixture the first time", async () => {
173+
await networkHelpers.loadFixture(mineBlockFixture);
174+
175+
// Executing the fixture should increment `calledCount` to 1.
176+
assert.equal(calledCount, 1);
177+
});
178+
179+
it("should execute the fixture the second time", async () => {
180+
await networkHelpers.loadFixture(mineBlockFixture);
181+
182+
// Loading the fixture again (with a new connection) should increment `calledCount` to 2.
183+
assert.equal(calledCount, 2);
184+
});
185+
});
156186
});

0 commit comments

Comments
 (0)