Skip to content

Commit

Permalink
Merge pull request #4208 from Saty248/loadFixture_hardhat_reset_bug_i…
Browse files Browse the repository at this point in the history
…ssue_#4151

adding a function and editing the error message for the issue #4151
  • Loading branch information
alcuadrado committed May 17, 2024
2 parents 9e838b3 + 56938ed commit 6213c9e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-cheetahs-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/hardhat-network-helpers": patch
---

Improve an error message and add utility to clear all the existing snaphosts.
2 changes: 1 addition & 1 deletion packages/hardhat-network-helpers/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class FixtureSnapshotError extends CustomError {
super(
`There was an error reverting the snapshot of the fixture.
This might be caused by using nested loadFixture calls in a test, for example by using multiple beforeEach calls. This is not supported yet.`,
This might be caused by using hardhat_reset and loadFixture calls in a testcase.`,
parent
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/hardhat-network-helpers/src/helpers/reset.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { NumberLike } from "../types";
import { clearSnapshots } from "../loadFixture";

import { getHardhatProvider, toNumber } from "../utils";

export async function reset(
url?: string,
blockNumber?: NumberLike
): Promise<void> {
const provider = await getHardhatProvider();

await clearSnapshots();
if (url === undefined) {
await provider.request({
method: "hardhat_reset",
Expand Down
7 changes: 7 additions & 0 deletions packages/hardhat-network-helpers/src/loadFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ export async function loadFixture<T>(fixture: Fixture<T>): Promise<T> {
return data;
}
}

/**
* Clears every existing snapshot.
*/
export async function clearSnapshots() {
snapshots = [];
}
12 changes: 10 additions & 2 deletions packages/hardhat-network-helpers/test/helpers/reset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { assert } from "chai";

import * as hh from "../../src";
import { INFURA_URL } from "../setup";
import { useEnvironment } from "../test-utils";

describe("resetWithoutFork", function () {
useEnvironment("simple");

Expand Down Expand Up @@ -56,3 +54,13 @@ describe("resetWithoutFork", function () {
assert.equal(olderMainnetBlockNumber, mainnetBlockNumber - 1000);
});
});

describe("should clear snapshot upon reset", function () {
useEnvironment("simple");
it("checks if the snapshot is cleared upon hardhat_reset", async function () {
const snapshotBeforeReset = await hh.takeSnapshot();
await hh.reset();
const snapshotAfterReset = await hh.takeSnapshot();
assert.equal(snapshotBeforeReset.snapshotId, snapshotAfterReset.snapshotId);
});
});

0 comments on commit 6213c9e

Please sign in to comment.