0Pricing
Web3 & DApp Development Fundamentals · レッスン

フィクスチャ

再利用可能なテスト設定

「フィクスチャ」はCoddyKit上の無料Web3 & DApp Development Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.

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

よくある質問

「フィクスチャ」レッスンは無料ですか?

はい。「フィクスチャ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web3 & DApp Development Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。

「フィクスチャ」で何を学びますか?

再利用可能なテスト設定 ブラウザで直接実行するハンズオンコードでWeb3 & DApp Development Fundamentalsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web3 & DApp Development Fundamentalsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb3 & DApp Development Fundamentalsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「フィクスチャ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb3 & DApp Development Fundamentalsレッスンでコードを書いて実行できますか?

はい。すべてのWeb3 & DApp Development Fundamentalsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. テストの作成
  2. リバートとイベントのテスト
  3. カバレッジとGasレポート
  4. フィクスチャ
← Web3 & DApp Development Fundamentalsに戻る