@ton/sandbox) is a local blockchain emulator that allows you to test smart contracts from TypeScript, without deploying them to a real network, and without running a blockchain node. It provides a complete testing environment that closely mimics the behavior of the actual TON blockchain. Blueprint project template already includes the @ton/sandbox dependency.
The companion @ton/test-utils helper library provides matchers for writing tests with Jest.
This guide covers everything you need to know about writing, running, and debugging tests for your smart contracts. For other programming languages consult documentation of corresponding SDKs.
Sandbox vs real network
| Feature | Sandbox | Real Network |
|---|---|---|
| Speed | Instant execution | ~5-15 second finality |
| Cost | Free | Requires real TON |
| Deterministic | Yes | No (depends on network conditions) |
| Debugging | Full introspection | Limited visibility |
| State Control | Complete control | Immutable history |
Sandbox limitations
While Sandbox closely emulates the real network, there are some differences to be aware of:- Time-dependent contracts: Sandbox time is controlled, not real-time
- External dependencies: Cannot interact with real external contracts, but can get their state and emulate them
- Blockchain imitation: Because there is no concept of blocks in Sandbox, things like sharding do not work.
Writing tests
Blueprint uses Jest as the default testing framework, providing powerful assertion capabilities and excellent TypeScript support.Basic test setup
Every Blueprint project includes a test template. Here’s the standard structure:tests/MyContract.spec.ts
Test isolation
Each test should include a freshBlockchain instance to ensure:
Test isolation
- No state leakage between tests
- Predictable initial conditions
- Independent contract deployments
- Fresh treasury wallets
- Reset logical time and configuration
- Clear transaction history
Understanding transaction results
When you send a message to a contract, you receive aSendMessageResult containing:
Transaction matchers
Blueprint provides powerful matchers for validating transactions:Running tests
Common pitfalls
Avoid These Common Mistakes
- Shared State: Don’t reuse blockchain instances between tests
- Async Issues: Always await blockchain operations
- Time Dependencies: Use
blockchain.nowfor time-sensitive tests - Gas Limits: Be aware of the computation and action phase limits
- Message Ordering: Remember that message processing is sequential
- Treasury Reuse: Use unique seeds for different test scenarios
Debugging checklist
When tests fail, check these common issues:- ✅ Contract is properly deployed before testing
- ✅ Treasury has sufficient balance for operations
- ✅ Transaction matchers use correct field names
- ✅ Exit codes match expected error conditions
- ✅ Message bodies are correctly formatted
- ✅ Time-sensitive operations account for blockchain time
Next steps
Comprehensive Testing- Testing Reference — Complete API documentation
- Testing Guidelines — Best practices and patterns
- Testing Coverage — Testing coverage
- Blueprint Benchmarks — Performance testing and optimization
- Debug Guide — Advanced debugging techniques
- TON Dev Wallet — Visual debugging tools