0Pricing
Web3 & DApp Development Fundamentals · Pelajaran

Fixture

Penyiapan pengujian yang dapat digunakan ulang

Fixture adalah pelajaran Web3 & DApp Development Fundamentals gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Web3 & DApp Development Fundamentals, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Web3 & DApp Development Fundamentals mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.

  • loadFixture runs 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.

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Fixture” gratis?

Ya — teks lengkap “Fixture” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Web3 & DApp Development Fundamentals, upgrade ke CoddyKit PRO. Kursus Web3 & DApp Development Fundamentals mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Fixture”?

Penyiapan pengujian yang dapat digunakan ulang Kamu berlatih Web3 & DApp Development Fundamentals dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Web3 & DApp Development Fundamentals?

Tidak diperlukan pengalaman sebelumnya. Web3 & DApp Development Fundamentals di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Fixture” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Web3 & DApp Development Fundamentals ini?

Ya. Setiap pelajaran Web3 & DApp Development Fundamentals menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Menulis Pengujian
  2. Menguji Revert dan Event
  3. Laporan Coverage dan Gas
  4. Fixture
← Kembali ke Web3 & DApp Development Fundamentals