How to Debug a Move Test That Passes Locally but Fails On-Chain
The short answer: the difference is almost always in the environment, not the logic. A locally passing Move test and an on-chain failure are separated by how you initialize state, who the signer is, how much gas you’re willing to spend, and what assumptions you made about the ledger that the test harness quietly ignores. You debug by reproducing the on-chain conditions as closely as possible in your local test, then bisecting the delta.
Here’s how to work through it methodically.
Start by Ruling Out the Obvious: Gas and Limits
The first thing to check is whether your transaction is even getting to the point where your Move code runs. On both Sui and Aptos, a local test environment typically has no meaningful gas constraints. On-chain, every instruction costs something, and complex operations can blow past limits before your code ever finishes.
- Reproduce the call with a generous budget. On Sui, that means explicitly setting a
--gas-budgetwell above your estimate. On Aptos, check themax_gasfield in your transaction submission. - Look at the failure output. A
MoveAbortwith a code you recognize is different from an out-of-gas error. The latter often surfaces as aTransactionExpiredorExecutionFailurewithout a clean abort code. - If you suspect gas, simplify the transaction: reduce loop counts, trim vector operations, or break a large module call into smaller pieces.
If gas checks out, move on to state initialization.
Compare your test setup to on-chain reality
Local tests often start from a blank slate. You create an account, maybe mint some coins, and call the function. On-chain, the state is messy. There are existing objects, previous versions of modules, and - critically - different signer addresses.
The most common culprit is a hardcoded signer. Your test might do:
let signer = account("Alice");
That works locally because the test framework lets you create an arbitrary signer. On-chain, the signer is the transaction sender, and its address is fixed. If your code checks signer::address_of(signer) == @admin or does anything that assumes a specific address, it will pass locally with a made-up account and fail on-chain with the real one.
Fix: Replace hardcoded addresses with a call to tx_context::sender(ctx) where possible, or pass the expected address as a parameter and verify it in the test with the actual address you intend to use.
Next, check for object ownership assumptions. On Sui, if your function takes an object by value and your test passes a fresh one, it will work. On-chain, if that object is already owned by someone else, or if it has been frozen, the call aborts. The error message will usually say something about an "object not found" or "unauthorized" - but not always. Sometimes it just aborts with a generic code.
On Aptos, the equivalent is a resource that doesn't exist yet. The Move test harness lets you call move_to freely. On-chain, if that resource was never created, you get a RESOURCE_ALREADY_EXISTS or a "move_to" failure that doesn't exist in your test.
Fix: Write a test that uses the exact same genesis state as your on-chain deployment. That means creating the same modules, initializing the same resources, and using the same signer addresses. If you can't do that, at least write a test that starts from a "pre-existing" state rather than an empty one.
Check module upgrade and compatibility warnings
If you already have a module deployed on-chain, and you're testing a new version locally, the local test runs against your source code directly. On-chain, you're running against the deployed bytecode plus whatever compatibility checks the chain enforces.
On Sui, if you're calling a function in a package that was upgraded, the caller might be using an old type layout. A struct that gained a field in the new version will deserialize differently on-chain than in your test. You'll often get an abort that looks like a type mismatch, but the real issue is ABI drift.
On Aptos, the equivalent is a module that was upgraded and changed a resource's fields. The on-chain data is still in the old format. Your new code tries to read it and fails.
Fix: Before debugging further, check whether your local test is actually using the same module version as what's deployed. If you recently upgraded, redeploy to a testnet, rerun the failing transaction, and compare the abort codes. If the codes match, it's a compatibility issue. If they differ, it's something else.
Use assert and Event Logs Deliberately
When you're stuck, add temporary assertions and events to the on-chain code path. The Move test runner shows you the abort code, but not the intermediate state. On-chain, you can emit events before every check to see which condition fails.
A practical pattern:
public entry fun do_thing(...) {
// Temporary
event::emit(MyEvent { step: 1 });
assert!(condition1, 1001);
event::emit(MyEvent { step: 2 });
assert!(condition2, 1002);
}
Run the on-chain transaction, read the events from the explorer, and see which step fails. Then add the same events to your local test and force the same conditions. The step that fails locally will match the one on-chain if the state is truly equivalent.
Check for Timestamp and Clock Dependencies
Move tests run in a simulated environment where tx_context::epoch and timestamps are controllable. On-chain, they're real. If your code checks the time, or an epoch, or a sequence number, and your test sets these manually, the on-chain value might be different.
Common failure: a test that calls assert!(tx_context::epoch(ctx) == 0) passes locally because you never advance the epoch. On-chain, after a validator upgrade or a long gap, the epoch is not zero.
Fix: Either write your test to set the clock/epoch explicitly to the value you expect on-chain, or write a test that doesn't depend on the exact value - use ranges instead of equality.
When all else fails: diff the exact error
If you're still stuck, the last step is to reproduce the exact bytecode. Both Sui and Aptos let you publish a module, then call it with a specific set of inputs. Instead of guessing, write a small script that:
- Publishes your module to a testnet.
- Submits the exact transaction that failed.
- Catches the abort code and the line number (if available).
Then compare that to your local test. The difference will be in the inputs or the environment. The error message is often more precise on-chain than in the test harness, especially if the failure involves a move_to or a borrow that violates storage rules.
The likely culprit, summarized
In practice, the single most common cause of "passes locally, fails on-chain" in Move is a signer or address assumption. The second is object or resource ownership. The third is gas. The fourth is timestamp/epoch. Work through them in that order and you'll find it quickly.
If none of those match, then the issue is probably in how you're calling the function - a vector length that's off by one, a string that isn't valid UTF-8, or a bcs-serialization mismatch. Re-encode all inputs on-chain, and compare the bytes to what your test used.
Debugging this class of bug is mostly about being systematic. The Move compiler doesn't hide anything from you on purpose; the gap between test and mainnet is just a lot wider than most people expect.
Not financial advice. suiboxer.xyz publishes market data and general information about digital assets. Crypto assets are volatile and you can lose everything you put in. Nothing here is a recommendation to buy, sell or hold, and we make no price predictions.
Prices are sourced from third parties and may be delayed or wrong. Verify anything you intend to act on against a primary source.