ฟิกซ์เจอร์
การตั้งค่าการทดสอบที่นำกลับมาใช้ใหม่ได้
ฟิกซ์เจอร์ เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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.
เรียนรู้ Web3 & DApp Development Fundamentals ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 29
- บทเรียน
- 105
คำถามที่พบบ่อย
บทเรียน “ฟิกซ์เจอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ฟิกซ์เจอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ฟิกซ์เจอร์”
การตั้งค่าการทดสอบที่นำกลับมาใช้ใหม่ได้ คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ฟิกซ์เจอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ