Skip to content

Commit df0ee64

Browse files
authored
Merge pull request #1558 from onflow/cadence_mobile_docs_edit_cws
Cadence mobile and fork testing doc edits.
2 parents e8c2a44 + 479633e commit df0ee64

File tree

6 files changed

+253
-238
lines changed

6 files changed

+253
-238
lines changed

docs/blockchain-development-tutorials/cadence/fork-testing/index.md

Lines changed: 80 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,32 @@ keywords:
2929
- forked runtime
3030
---
3131

32-
# Fork Testing with Cadence
32+
# Fork testing with Cadence
3333

3434
This tutorial teaches you how to run your Cadence tests against a snapshot of Flow mainnet using `flow test --fork`. You'll learn how to test your contracts against real deployed contracts and production data without needing to deploy anything to a live network or bootstrap test accounts.
3535

36-
Fork testing bridges the gap between isolated local unit tests and testnet deployments. It enables you to validate your contracts work correctly with real on-chain state, test integrations with deployed contracts, and debug issues using historical blockchain data—all in a safe, local environment.
36+
Fork testing bridges the gap between isolated local unit tests and testnet deployments. It allows you to validate your contracts work correctly with real on-chain state, test integrations with deployed contracts, and debug issues with historical blockchain data—all in a safe, local environment.
3737

3838
## What You'll Learn
3939

40-
After completing this tutorial, you'll be able to:
40+
After you complete this tutorial, you'll be able to:
4141

42-
- **Run Cadence tests against forked networks** using `flow test --fork`
43-
- **Test contracts that depend on real mainnet contracts** without manual setup
44-
- **Use account impersonation** to execute transactions as any mainnet account
45-
- **Read from production blockchain state** in your test suite
46-
- **Pin tests to specific block heights** for historical debugging
47-
- **Integrate fork testing** into your development workflow
42+
- **Run Cadence tests against forked networks** with `flow test --fork`.
43+
- **Test contracts that depend on real mainnet contracts** without manual setup.
44+
- **Use account impersonation** to execute transactions as any mainnet account.
45+
- **Read from production blockchain state** in your test suite.
46+
- **Pin tests to specific block heights** for historical debugging.
47+
- **Integrate fork testing** into your development workflow.
4848

4949
## What You'll Build
5050

5151
You'll create a complete fork testing setup that demonstrates:
5252

53-
- Reading from the live FlowToken contract on mainnet
54-
- Deploying your own contract that interacts with mainnet contracts
55-
- Testing custom logic against real account balances and state
56-
- Executing transactions using impersonated mainnet accounts
57-
- A reusable pattern for integration testing your Flow applications
53+
- Reading from the live FlowToken contract on mainnet.
54+
- Deploying your own contract that interacts with mainnet contracts.
55+
- Testing custom logic against real account balances and state.
56+
- Executing transactions using impersonated mainnet accounts.
57+
- A reusable pattern for integration testing your Flow applications.
5858

5959
### Reproducibility first
6060

@@ -64,7 +64,7 @@ Pin a specific block height when you need reproducible results:
6464
flow test --fork mainnet --fork-height <BLOCK_HEIGHT>
6565
```
6666

67-
Document the pin heights you rely on (for example in CI variables or a simple file in the repo) and update them via a dedicated freshness PR. For best results, keep a per‑spork stable pin and also run a "latest" freshness job.
67+
Document the pin heights you rely on (for example, in CI variables or a simple file in the repo) and update them via a dedicated freshness PR. For best results, keep a per‑spork stable pin and also run a "latest" freshness job.
6868

6969
## Prerequisites
7070

@@ -78,11 +78,11 @@ brew install flow-cli
7878

7979
For other operating systems, refer to the [installation guide].
8080

81-
### Basic Cadence Testing Knowledge
81+
### Basic Cadence testing knowledge
8282

8383
You should be familiar with writing basic Cadence tests. If you're new to Cadence testing, start with [Testing Smart Contracts] first.
8484

85-
### Network Access
85+
### Network access
8686

8787
You'll need network access to Flow's public access nodes. The tutorial uses these endpoints, which are freely available:
8888

@@ -95,7 +95,7 @@ This tutorial covers `flow test --fork` (running tests against forked network st
9595

9696
:::
9797

98-
## Create Your Project
98+
## Create your project
9999

100100
Navigate to your development directory and create a new Flow project:
101101

@@ -114,7 +114,7 @@ flow init fork-testing-demo --yes
114114
cd fork-testing-demo
115115
```
116116

117-
## Install Dependencies
117+
## Install dependencies
118118

119119
Use the [Dependency Manager] to install the `FlowToken` and `FungibleToken` contracts:
120120

@@ -124,7 +124,7 @@ flow dependencies install FlowToken FungibleToken
124124

125125
This downloads the contracts and their dependencies into the `imports/` folder and updates your `flow.json` with the correct addresses and aliases across all networks (mainnet, testnet, emulator).
126126

127-
Your `flow.json` will now include an entry like:
127+
Your `flow.json` now includes an entry like:
128128

129129
```json
130130
{
@@ -141,7 +141,7 @@ Your `flow.json` will now include an entry like:
141141
}
142142
```
143143

144-
Your `flow.json` should now have the mainnet and testnet networks configured from `flow init`. In fork mode, contract imports automatically resolve to the correct network addresses.
144+
Your `flow.json` now has the mainnet and testnet networks configured from `flow init`. In fork mode, contract imports automatically resolve to the correct network addresses.
145145

146146
## Test Reading Live State
147147

@@ -185,12 +185,15 @@ access(all) fun testFlowTokenSupplyIsPositive() {
185185
}
186186
```
187187

188-
Notes:
188+
:::info
189+
189190
- Use `Test.executeScript()` to read contract state
190-
- The script imports `FlowToken` by name - the dependency manager handles address resolution
191-
- In fork mode, this automatically uses the mainnet FlowToken contract
192-
- Extract the return value with proper type casting and assert on it
193-
- File paths in `Test.readFile()` are relative to the test file location (use `../scripts/` from `cadence/tests/`)
191+
- The script imports `FlowToken` by name - the dependency manager handles address resolution.
192+
- In fork mode, this automatically uses the mainnet FlowToken contract.
193+
- Extract the return value with proper type casting and assert on it.
194+
- File paths in `Test.readFile()` are relative to the test file location (use `../scripts/` from `cadence/tests/`).
195+
196+
:::
194197

195198
#### Quick verify
196199

@@ -206,7 +209,7 @@ Target testnet instead:
206209
flow test cadence/tests/FlowToken_test.cdc --fork testnet
207210
```
208211

209-
You should see the test PASS. If not, verify your network host in `flow.json` and that dependencies are installed.
212+
You will see the test PASS. If not, verify your network host in `flow.json` and that dependencies are installed.
210213

211214
## Deploy and Test Your Contract
212215

@@ -221,12 +224,13 @@ flow accounts create
221224
```
222225

223226
Follow the prompts:
224-
- Select "mainnet" for the network
225-
- Name your account as desired
227+
- Select "mainnet" for the network.
228+
- Name your account as desired.
226229

227230
This will output the new account address. Use this address as the mainnet alias for your contract in flow.json.
228231

229232
:::note
233+
230234
This creates a local account with a mainnet-format address for fork testing. When you're ready to deploy to actual mainnet, you'll use this same account—see the [Deploying Contracts guide](pathname:///build/cadence/smart-contracts/deploying) for details.
231235
:::
232236

@@ -261,11 +265,11 @@ access(all) contract TokenChecker {
261265
}
262266
```
263267

264-
### Configure Contract in flow.json
268+
### Configure contract in flow.json
265269

266270
Add the `TokenChecker` contract configuration to `flow.json`. The contract needs a **mainnet alias** so that imports can resolve properly during fork testing.
267271

268-
Update your `flow.json` to include the contract with aliases, using the address you generated in the previous step:
272+
Update your `flow.json` to include the contract with aliases, and use the address you generated in the previous step:
269273

270274
```json
271275
{
@@ -286,9 +290,13 @@ Update your `flow.json` to include the contract with aliases, using the address
286290
}
287291
```
288292

289-
**Note:** No local private key is required for forked tests. The accounts entry above is included so you can copy/reference the address in your config; keys can be omitted for fork tests. Contracts deploy to the testing environment at `testing` alias, and transactions that interact with forked state can use impersonation. The `Test.deployContract` function will automatically deploy your contract to the testing environment during test execution.
293+
:::info
294+
295+
No local private key is required for forked tests. The accounts entry above is included so you can copy and reference the address in your config. You can also omit keys for fork tests. Contracts deploy to the testing environment at `testing` alias, and transactions that interact with forked state can use impersonation. The `Test.deployContract` function will automatically deploy your contract to the testing environment during test execution.
296+
297+
:::
290298

291-
### Create Scripts for Testing
299+
### Create scripts for testing
292300

293301
Generate the scripts:
294302

@@ -317,7 +325,7 @@ access(all) fun main(addr: Address, min: UFix64): Bool {
317325
}
318326
```
319327

320-
### Test Your Contract with Forked State
328+
### Test Your contract with forked state
321329

322330
Generate the test file:
323331

@@ -367,14 +375,14 @@ access(all) fun testHasMinimumBalance() {
367375
}
368376
```
369377

370-
### What's Happening Here
378+
### What's happening here
371379

372-
1. **Your contract uses FlowToken**: `TokenChecker` imports and interacts with the real FlowToken contract
373-
2. **No bootstrapping needed**: When you run with `--fork`, real mainnet accounts (like `0x1654653399040a61`, the Flow service account) already have balances
374-
3. **Test against real state**: You can query actual accounts and verify your contract logic works with production data
375-
4. **Local deployment**: Your `TokenChecker` contract is deployed locally to the test environment, but it reads from forked mainnet state
380+
1. **Your contract uses FlowToken**: `TokenChecker` imports and interacts with the real FlowToken contract.
381+
2. **No bootstrapping needed**: When you run with `--fork`, real mainnet accounts (like `0x1654653399040a61`, the Flow service account) already have balances.
382+
3. **Test against real state**: You can query actual accounts and verify your contract logic works with production data.
383+
4. **Local deployment**: Your `TokenChecker` contract is deployed locally to the test environment, but it reads from forked mainnet state.
376384

377-
## Execute Transactions with Account Impersonation
385+
## Execute transactions with account impersonation
378386

379387
Fork testing includes built-in account impersonation—you can execute transactions as **any mainnet account** without needing private keys. This lets you test interactions with real accounts and their existing state.
380388

@@ -433,9 +441,9 @@ transaction(amount: UFix64, to: Address) {
433441
}
434442
```
435443

436-
### Test Transaction Execution with Impersonation
444+
### Test transaction execution with impersonation
437445

438-
Add this test function to the existing `cadence/tests/TokenChecker_test.cdc` file:
446+
Add this test function to the current `cadence/tests/TokenChecker_test.cdc` file:
439447

440448
```cadence
441449
access(all) fun testTransactionAsMainnetAccount() {
@@ -501,23 +509,23 @@ access(all) fun testTransactionAsMainnetAccount() {
501509
}
502510
```
503511

504-
### Key Points About Account Impersonation
512+
### Key points about account impersonation
505513

506-
1. **Any account can be used**: Call `Test.getAccount(address)` with any mainnet address
507-
2. **No private keys needed**: Fork testing has built-in impersonation—you can sign transactions as any account
508-
3. **Real account state**: The account has its actual mainnet balance, storage, and capabilities
509-
4. **Mutations are local**: Changes only affect your test environment, not the real network
510-
5. **Test complex scenarios**: Impersonate whale accounts, protocol accounts, or any user to test edge cases
514+
1. **Any account can be used**: Call `Test.getAccount(address)` with any mainnet address.
515+
2. **No private keys needed**: Fork testing has built-in impersonation—you can sign transactions as any account.
516+
3. **Real account state**: The account has its actual mainnet balance, storage, and capabilities.
517+
4. **Mutations are local**: Changes only affect your test environment, not the real network.
518+
5. **Test complex scenarios**: Impersonate whale accounts, protocol accounts, or any user to test edge cases.
511519

512-
## Run All Tests Together
520+
## Run all tests together
513521

514522
Now that you have multiple test files, run them all against the forked network:
515523

516524
```zsh
517525
flow test --fork mainnet
518526
```
519527

520-
This runs all `*_test.cdc` files in your project against mainnet. You should see:
528+
This runs all `*_test.cdc` files in your project against mainnet. You will see:
521529

522530
```
523531
Test results: "cadence/tests/FlowToken_test.cdc"
@@ -529,7 +537,7 @@ Test results: "cadence/tests/TokenChecker_test.cdc"
529537
- PASS: testTransactionAsMainnetAccount
530538
```
531539

532-
### Additional Options
540+
### Additional options
533541

534542
You can also fork from testnet (`flow test --fork testnet`) or pin to a specific block height (`--fork-height`). See the [Fork Testing Flags] reference for all available options.
535543

@@ -548,7 +556,7 @@ Fork tests run against Flow chain state only:
548556
- For end-to-end, combine with `flow emulator --fork` and a local stub service
549557
:::
550558

551-
### Select Tests Quickly
559+
### Select tests quickly
552560

553561
- Run specific files or directories:
554562

@@ -564,41 +572,41 @@ flow test cadence/tests/TokenChecker_test.cdc --name _smoke --fork mainnet
564572

565573
- Optional: suffix a few functions with `_smoke` for quick PR runs; run the full suite nightly or on protected branches.
566574

567-
## When to Use Fork Testing
575+
## When to use fork testing
568576

569577
Fork testing is most valuable for:
570578

571-
- Integration testing with real onchain contracts and data
572-
- Pre-deployment validation before mainnet releases
573-
- Upgrade testing against production state
574-
- Reproducing issues at a specific block height
575-
- Testing interactions with high-value or protocol accounts
576-
- Validating contract behavior with real-world data patterns
579+
- Integration testing with real onchain contracts and data.
580+
- Pre-deployment validation before mainnet releases.
581+
- Upgrade testing against production state.
582+
- Reproducing issues at a specific block height.
583+
- Testing interactions with high-value or protocol accounts.
584+
- Validating contract behavior with real-world data patterns.
577585

578586
For strategy, limitations, and best practices, see the guide: [Testing Smart Contracts].
579587

580588
## Conclusion
581589

582590
In this tutorial, you learned how to use fork testing to validate your Cadence contracts against live Flow network state. You created tests that read from real mainnet contracts, deployed custom contracts that interact with production data, and executed transactions using account impersonation—all without deploying to a live network or bootstrapping test accounts.
583591

584-
Now that you have completed this tutorial, you should be able to:
592+
Now that you have completed this tutorial, you will be able to:
585593

586-
- **Run Cadence tests against forked networks** using `flow test --fork`
587-
- **Test contracts that depend on real mainnet contracts** without manual setup
588-
- **Use account impersonation** to execute transactions as any mainnet account
589-
- **Read from production blockchain state** in your test suite
590-
- **Pin tests to specific block heights** for historical debugging
591-
- **Integrate fork testing** into your development workflow
594+
- **Run Cadence tests against forked networks** with `flow test --fork`.
595+
- **Test contracts that depend on real mainnet contracts** without manual setup.
596+
- **Use account impersonation** to execute transactions as any mainnet account.
597+
- **Read from production blockchain state** in your test suite.
598+
- **Pin tests to specific block heights** for historical debugging.
599+
- **Integrate fork testing** into your development workflow.
592600

593-
Fork testing bridges the gap between local unit tests and testnet deployments, enabling you to catch integration issues early and test against real-world conditions. Use it as part of your pre-deployment validation process, alongside emulator unit tests for determinism and isolation, and testnet deployments for final verification.
601+
Fork testing bridges the gap between local unit tests and testnet deployments, allowing you to catch integration issues early and test against real-world conditions. Use it as part of your pre-deployment validation process, alongside emulator unit tests for determinism and isolation, and testnet deployments for final verification.
594602

595603
### Next Steps
596604

597-
- Explore additional assertions and helpers in the [Cadence Testing Framework]
598-
- Add more real-world tests that read from standard contracts like Flow NFT
599-
- Keep unit tests on the emulator for determinism and isolation; run forked integration tests selectively in CI
600-
- Review the [Fork Testing Flags] reference for advanced options
601-
- Learn about [Flow Networks] and public access nodes
605+
- Explore additional assertions and helpers in the [Cadence Testing Framework].
606+
- Add more real-world tests that read from standard contracts like Flow NFT.
607+
- Keep unit tests on the emulator for determinism and isolation; run forked integration tests selectively in CI.
608+
- Review the [Fork Testing Flags] reference for advanced options.
609+
- Learn about [Flow Networks] and public access nodes.
602610

603611
<!-- Reference-style links -->
604612

0 commit comments

Comments
 (0)