Fikstürler
Yeniden kullanılabilir test kurulumu
Fikstürler, CoddyKit'te ücretsiz bir Web3 & DApp Development Fundamentals dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Web3 & DApp Development Fundamentals öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Web3 & DApp Development Fundamentals kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Yapay zeka eğitmeniyle Web3 & DApp Development Fundamentals öğren — ücretsiz
Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.
- Kurslar
- 29
- Dersler
- 105
Sıkça Sorulan Sorular
“Fikstürler” dersi ücretsiz mi?
Evet — “Fikstürler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Web3 & DApp Development Fundamentals kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Web3 & DApp Development Fundamentals kursu toplamda 4 dersten oluşur.
“Fikstürler” dersinde ne öğreneceğim?
Yeniden kullanılabilir test kurulumu Web3 & DApp Development Fundamentals ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Web3 & DApp Development Fundamentals öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Web3 & DApp Development Fundamentals, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Fikstürler” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Web3 & DApp Development Fundamentals dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Web3 & DApp Development Fundamentals dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.