@gala-chain/test ∙ API
API > TestChaincode
Class: TestChaincode
Test harness for Hyperledger Fabric chaincode contracts.
Provides a testing environment that simulates blockchain interactions without requiring a full Fabric network. Supports transaction invocation, state management, and user identity simulation.
Contents
- Example
- Constructors
- new TestChaincode(contracts, state, writes, callingUser, callingUserMsp, callHistory)
- Properties
- callHistory
- callingUser
- callingUserMsp
- chaincode
- state
- writes
- Methods
- getContractInstance()
- getState()
- getStateAll()
- getWrites()
- invoke()
- query()
- setCallingUser()
- setCallingUserMsp()
Example
// Create test chaincode with contract classes
const testChaincode = new TestChaincode([MyContract, TokenContract]);
// Set calling user context
testChaincode.setCallingUser("client|alice");
// Invoke contract methods
const result = await testChaincode.invoke("CreateTokenClass", tokenClassDto);
// Query contract methods (read-only)
const balance = await testChaincode.query("FetchBalance", balanceDto);
// Access contract instances for direct testing
const contractInstance = testChaincode.getContractInstance(MyContract);
Constructors
new TestChaincode(contracts, state, writes, callingUser, callingUserMsp, callHistory)
new TestChaincode(
contracts,state,writes,callingUser,callingUserMsp,callHistory):TestChaincode
Creates a new test chaincode instance.
Parameters
▪ contracts: ClassConstructor\<Contract>[]
Array of contract classes to include in the chaincode
▪ state: Record\<string, string>= {}
Initial blockchain state as a key-value map
▪ writes: Record\<string, string>[]= []
Storage for tracking state writes during tests
▪ callingUser: string= "client|admin"
Default calling user in format "prefix|userId" (e.g., "client|alice")
▪ callingUserMsp: string= "CuratorOrg"
MSP (Membership Service Provider) ID for the calling user
▪ callHistory: unknown[]= []
Array to track method invocation history for testing
Source
chain-test/src/unit/TestChaincode.ts:95
Properties
callHistory
readonlycallHistory:unknown[] =[]
Array to track method invocation history for testing
Source
chain-test/src/unit/TestChaincode.ts:101
callingUser
callingUser:
string="client|admin"
Default calling user in format "prefix|userId" (e.g., "client|alice")
Source
chain-test/src/unit/TestChaincode.ts:99
callingUserMsp
callingUserMsp:
string="CuratorOrg"
MSP (Membership Service Provider) ID for the calling user
Source
chain-test/src/unit/TestChaincode.ts:100
chaincode
privatereadonlychaincode:ChaincodeFromContractClassType
Source
chain-test/src/unit/TestChaincode.ts:83
state
privatereadonlystate:Record\<string,string> ={}
Initial blockchain state as a key-value map
Source
chain-test/src/unit/TestChaincode.ts:97
writes
privatereadonlywrites:Record\<string,string>[] =[]
Storage for tracking state writes during tests
Source
chain-test/src/unit/TestChaincode.ts:98
Methods
getContractInstance()
getContractInstance\<
T>(contractClass):T
Gets a direct reference to a contract instance for low-level testing. Useful for accessing contract internals or calling methods directly.
Type parameters
▪ T extends Contract
Type of the contract class
Parameters
▪ contractClass: (...args) => T & Function
Constructor function of the contract class
Returns
The contract instance
Throws
NotImplementedError if the contract class is not found in the chaincode
Source
chain-test/src/unit/TestChaincode.ts:225
getState()
getState(
skipKeysStartingWith):Record\<string,string>
Parameters
▪ skipKeysStartingWith: string[]= undefined
Source
chain-test/src/unit/TestChaincode.ts:248
getStateAll()
getStateAll():
Record\<string,string>
Source
chain-test/src/unit/TestChaincode.ts:244
getWrites()
getWrites(
skipKeysStartingWith):Record\<string,string>
Parameters
▪ skipKeysStartingWith: string[]= undefined
Source
chain-test/src/unit/TestChaincode.ts:257
invoke()
invoke\<
T>(method, ...args):Promise\<T>
Invokes a chaincode method with write capabilities (submit transaction). Changes made during invocation are persisted to the state.
Type parameters
▪ T = InvokeResponse
Expected return type of the invocation
Parameters
▪ method: string
Name of the contract method to invoke
▪ ...args: (string | object)[]
Arguments to pass to the method (strings or serializable objects)
Returns
Promise resolving to the method's return value
Throws
Error if the invocation fails
Source
chain-test/src/unit/TestChaincode.ts:168
query()
query\<
T>(method, ...args):Promise\<T>
Queries a chaincode method with read-only access (evaluate transaction). Changes made during query are not persisted to the state.
Type parameters
▪ T = InvokeResponse
Expected return type of the query
Parameters
▪ method: string
Name of the contract method to query
▪ ...args: (string | object)[]
Arguments to pass to the method (strings or serializable objects)
Returns
Promise resolving to the method's return value
Throws
Error if the query fails
Source
chain-test/src/unit/TestChaincode.ts:197
setCallingUser()
setCallingUser(
user):TestChaincode
Sets the calling user for subsequent chaincode invocations.
Parameters
▪ user: string
User identifier in format "prefix|userId" (e.g., "client|alice", "curator|admin")
Returns
This TestChaincode instance for method chaining
Source
chain-test/src/unit/TestChaincode.ts:142
setCallingUserMsp()
setCallingUserMsp(
msp):TestChaincode
Sets the MSP (Membership Service Provider) for the calling user.
Parameters
▪ msp: string
MSP ID (e.g., "CuratorOrg", "UserOrg")
Returns
This TestChaincode instance for method chaining