Skip to content

Commit 6213c9e

Browse files
authored
Merge pull request #4208 from Saty248/loadFixture_hardhat_reset_bug_issue_#4151
adding a function and editing the error message for the issue #4151
2 parents 9e838b3 + 56938ed commit 6213c9e

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

.changeset/wise-cheetahs-confess.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-network-helpers": patch
3+
---
4+
5+
Improve an error message and add utility to clear all the existing snaphosts.

packages/hardhat-network-helpers/src/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class FixtureSnapshotError extends CustomError {
1717
super(
1818
`There was an error reverting the snapshot of the fixture.
1919
20-
This might be caused by using nested loadFixture calls in a test, for example by using multiple beforeEach calls. This is not supported yet.`,
20+
This might be caused by using hardhat_reset and loadFixture calls in a testcase.`,
2121
parent
2222
);
2323
}

packages/hardhat-network-helpers/src/helpers/reset.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { NumberLike } from "../types";
2+
import { clearSnapshots } from "../loadFixture";
3+
24
import { getHardhatProvider, toNumber } from "../utils";
35

46
export async function reset(
57
url?: string,
68
blockNumber?: NumberLike
79
): Promise<void> {
810
const provider = await getHardhatProvider();
9-
11+
await clearSnapshots();
1012
if (url === undefined) {
1113
await provider.request({
1214
method: "hardhat_reset",

packages/hardhat-network-helpers/src/loadFixture.ts

+7
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ export async function loadFixture<T>(fixture: Fixture<T>): Promise<T> {
6868
return data;
6969
}
7070
}
71+
72+
/**
73+
* Clears every existing snapshot.
74+
*/
75+
export async function clearSnapshots() {
76+
snapshots = [];
77+
}

packages/hardhat-network-helpers/test/helpers/reset.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { assert } from "chai";
2-
32
import * as hh from "../../src";
43
import { INFURA_URL } from "../setup";
54
import { useEnvironment } from "../test-utils";
6-
75
describe("resetWithoutFork", function () {
86
useEnvironment("simple");
97

@@ -56,3 +54,13 @@ describe("resetWithoutFork", function () {
5654
assert.equal(olderMainnetBlockNumber, mainnetBlockNumber - 1000);
5755
});
5856
});
57+
58+
describe("should clear snapshot upon reset", function () {
59+
useEnvironment("simple");
60+
it("checks if the snapshot is cleared upon hardhat_reset", async function () {
61+
const snapshotBeforeReset = await hh.takeSnapshot();
62+
await hh.reset();
63+
const snapshotAfterReset = await hh.takeSnapshot();
64+
assert.equal(snapshotBeforeReset.snapshotId, snapshotAfterReset.snapshotId);
65+
});
66+
});

0 commit comments

Comments
 (0)