Concepts
The following sections provide an overview of key concepts and features in the Algorand TypeScript Testing framework.
Test Context
The main abstraction for interacting with the testing framework is the TestExecutionContext
. It creates an emulated Algorand environment that closely mimics AVM behavior relevant to unit testing the contracts and provides a TypeScript interface for interacting with the emulated environment.
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing';import { afterEach, describe, it } from 'vitest';
describe('MyContract', () => { // Recommended way to instantiate the test context const ctx = new TestExecutionContext(); afterEach(() => { // ctx should be reset after each test is executed ctx.reset(); });
it('test my contract', () => { // Your test code here });});
The context manager interface exposes four main properties:
contract
: An instance ofContractContext
for creating instances of Contract under test and register them with the test execution context.ledger
: An instance ofLedgerContext
for interacting with and querying the emulated Algorand ledger state.txn
: An instance ofTransactionContext
for creating and managing transaction groups, submitting transactions, and accessing transaction results.any
: An instance ofAlgopyValueGenerator
for generating randomized test data.
The any
property provides access to different value generators:
AvmValueGenerator
: Base abstractions for AVM types. All methods are available directly on the instance returned fromany
.TxnValueGenerator
: Accessible viaany.txn
, for transaction-related data.Arc4ValueGenerator
: Accessible viaany.arc4
, for ARC4 type data.
These generators allow creation of constrained random values for various AVM entities (accounts, assets, applications, etc.) when specific values are not required.
Value generators are powerful tools for generating test data for specified AVM types. They allow further constraints on random value generation via arguments, making it easier to generate test data when exact values are not necessary.
When used with the 'Arrange, Act, Assert' pattern, value generators can be especially useful in setting up clear and concise test data in arrange steps.
Types of algorand-typescript
stub implementations
As explained in the introduction, algorand-typescript-testing
injects test implementations for stubs available in the algorand-typescript
package. However, not all of the stubs are implemented in the same manner:
-
Native: Fully matches AVM computation in Python. For example,
op.sha256
and other cryptographic operations behave identically in AVM and unit tests. This implies that the majority of opcodes that are ‘pure’ functions in AVM also have a native TypeScript implementation provided by this package. These abstractions and opcodes can be used within and outside of the testing context. -
Emulated: Uses
TestExecutionContext
to mimic AVM behavior. For example,Box.put
on anBox
within a test context stores data in the test manager, not the real Algorand network, but provides the same interface. -
Mockable: Not implemented, but can be mocked or patched. For example,
op.onlineStake
can be mocked to return specific values or behaviors; otherwise, it raises aNotImplementedError
. This category covers cases where native or emulated implementation in a unit test context is impractical or overly complex.