Skip to content

Commit 5b5ccc0

Browse files
authored
Merge pull request #2922 from NomicFoundation/francovictorio/hh-631/detect-anonymous-fixtures
Don't allow using anonymous functions as fixtures
2 parents 5f87473 + d9fb21a commit 5b5ccc0

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

.changeset/quick-hairs-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-network-helpers": patch
3+
---
4+
5+
- Disallow using anonymous functions as fixtures

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ This might be caused by using nested loadFixture calls in a test, for example by
2323
}
2424
}
2525

26+
export class FixtureAnonymousFunctionError extends CustomError {
27+
constructor() {
28+
super(`Anonymous functions cannot be used as fixtures.
29+
30+
You probably did something like this:
31+
32+
loadFixture(async () => { ... });
33+
34+
Instead, define a fixture function and refer to that same function in each call to loadFixture.
35+
36+
Learn more at https://hardhat.org/hardhat-network-helpers/docs/reference#fixtures`);
37+
}
38+
}
39+
2640
export class OnlyHardhatNetworkError extends CustomError {
2741
constructor(networkName: string, version?: string) {
2842
let errorMessage: string = ``;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { SnapshotRestorer } from "./helpers/takeSnapshot";
22

3-
import { FixtureSnapshotError, InvalidSnapshotError } from "./errors";
3+
import {
4+
FixtureAnonymousFunctionError,
5+
FixtureSnapshotError,
6+
InvalidSnapshotError,
7+
} from "./errors";
48

59
type Fixture<T> = () => Promise<T>;
610

@@ -27,6 +31,10 @@ const snapshots: Array<Snapshot<any>> = [];
2731
* - Incorrect usage: `loadFixture(async () => { ... })`
2832
*/
2933
export async function loadFixture<T>(fixture: Fixture<T>): Promise<T> {
34+
if (fixture.name === "") {
35+
throw new FixtureAnonymousFunctionError();
36+
}
37+
3038
const snapshot = snapshots.find((s) => s.fixture === fixture);
3139

3240
const { takeSnapshot } = await import("./helpers/takeSnapshot");

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { assert } from "chai";
22

3-
import { FixtureSnapshotError } from "../src/errors";
3+
import {
4+
FixtureAnonymousFunctionError,
5+
FixtureSnapshotError,
6+
} from "../src/errors";
47
import { loadFixture } from "../src/loadFixture";
58
import { useEnvironment, rpcQuantityToNumber } from "./test-utils";
69

@@ -90,4 +93,18 @@ describe("loadFixture", function () {
9093
FixtureSnapshotError
9194
);
9295
});
96+
97+
it("should throw when an anonymous regular function is used", async function () {
98+
await assert.isRejected(
99+
loadFixture(async function () {}),
100+
FixtureAnonymousFunctionError
101+
);
102+
});
103+
104+
it("should throw when an anonymous arrow function is used", async function () {
105+
await assert.isRejected(
106+
loadFixture(async () => {}),
107+
FixtureAnonymousFunctionError
108+
);
109+
});
93110
});

0 commit comments

Comments
 (0)