Fixtures
Wiederverwendbares Test-Setup
Fixtures ist eine kostenlose Web3 & DApp Development Fundamentals-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web3 & DApp Development Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
The Setup Problem
Many tests need the same starting state — a deployed contract, funded accounts, initial config. Repeating this setup in every beforeEach is slow and repetitive.
Fixtures solve this by running setup once and snapshotting the result.
What Is loadFixture
Hardhat provides loadFixture in its network helpers. The first time it runs a fixture function and takes an EVM snapshot. Later calls simply revert to that snapshot instead of re-running everything.
This makes tests both fast and perfectly isolated.
Importing the Helper
Import loadFixture from the network helpers package (included in the Toolbox):
const {
loadFixture,
} = require("@nomicfoundation/hardhat-network-helpers");
const { expect } = require("chai");
const { ethers } = require("hardhat");const {
loadFixture,
} = require("@nomicfoundation/hardhat-network-helpers");
const { expect } = require("chai");
const { ethers } = require("hardhat");Writing a Fixture Function
A fixture is an async function that performs setup and returns the objects tests need:
async function deployTokenFixture() {
const [owner, alice] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
return { token, owner, alice };
}async function deployTokenFixture() {
const [owner, alice] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
return { token, owner, alice };
}Using a Fixture in a Test
Call loadFixture at the start of each test and destructure what you need:
it("assigns supply to owner", async function () {
const { token, owner } = await loadFixture(deployTokenFixture);
expect(await token.balanceOf(owner.address))
.to.equal(await token.totalSupply());
});it("assigns supply to owner", async function () {
const { token, owner } = await loadFixture(deployTokenFixture);
expect(await token.balanceOf(owner.address))
.to.equal(await token.totalSupply());
});Why Fixtures Are Fast
Re-running deployment for every test is expensive. With fixtures:
- The setup transactions execute only once.
- Subsequent tests revert the EVM to the snapshot — a near-instant operation.
Large suites can run dramatically faster than equivalent beforeEach setups.
Isolation Guarantee
Because each loadFixture call reverts to the original snapshot, tests cannot leak state into each other. One test transferring tokens does not affect the next test's starting balances.
This isolation is what makes test results reliable and order-independent.
Fixtures with Parameters
loadFixture takes a function reference, not a call, so it cannot accept arguments directly. To vary setup, define multiple fixtures:
async function deployPausedFixture() {
const base = await deployTokenFixture();
await base.token.pause();
return base;
}Each variant gets its own snapshot.
async function deployPausedFixture() {
const base = await deployTokenFixture();
await base.token.pause();
return base;
}Fixtures vs beforeEach
When should you use each?
- loadFixture — preferred for deployment-heavy setup; faster and snapshot-based.
- beforeEach — fine for trivial setup or when you need per-test side effects.
The Hardhat team recommends fixtures for most contract deployment setup.
A Common Pitfall
Do not call the fixture function directly inside loadFixture:
// Wrong: passes the result, not the function
await loadFixture(deployTokenFixture());
// Right: passes the function reference
await loadFixture(deployTokenFixture);Passing the reference lets Hardhat manage the snapshot lifecycle.
// Wrong: passes the result, not the function
await loadFixture(deployTokenFixture());
// Right: passes the function reference
await loadFixture(deployTokenFixture);Sharing Fixtures Across Files
You can move a fixture into its own module and import it wherever needed:
// fixtures.js
module.exports = { deployTokenFixture };
// in a test file
const { deployTokenFixture } = require("./fixtures");
const { token } = await loadFixture(deployTokenFixture);The snapshot caching still works across files in the same run.
// fixtures.js
module.exports = { deployTokenFixture };
// in a test file
const { deployTokenFixture } = require("./fixtures");
const { token } = await loadFixture(deployTokenFixture);Quick Check
Test your understanding of fixtures.
Recap
You learned reusable test setup with fixtures.
loadFixtureruns setup once and snapshots the EVM.- Later calls revert to the snapshot — fast and fully isolated.
- A fixture is an async function returning the objects tests need.
- Pass the function reference, never call it.
- Define multiple fixtures for different starting states.
Häufig gestellte Fragen
Ist die Lektion „Fixtures“ kostenlos?
Ja — der vollständige Text von „Fixtures“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web3 & DApp Development Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Fixtures“?
Wiederverwendbares Test-Setup Du übst Web3 & DApp Development Fundamentals mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Web3 & DApp Development Fundamentals zu starten?
Keine Vorkenntnisse erforderlich. Web3 & DApp Development Fundamentals auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Fixtures“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Web3 & DApp Development Fundamentals-Lektion Code schreiben und ausführen?
Ja. Jede Web3 & DApp Development Fundamentals-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.