Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a function and editing the error message for the issue #4151 #4208

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});