Testing contracts locally using SDK test utilities
Once you're done writing your contracts, it's time to test them! You can use the Hyperlane TestCoreApp and TestCoreDeployer to create an instance of Hyperlane for testing purposes and simulate interchain messaging.
Example Usage
In the example below, we simulate message passing between two chains. For more in depth examples see the tests in the Hyperlane template application.
import { SignerWithAddress } from'@nomiclabs/hardhat-ethers/signers';import { expect } from'chai';import { ethers } from'hardhat';import { ChainMap, ChainNameToDomainId, MultiProvider, TestChainNames, TestCoreApp, TestCoreDeployer, getChainToOwnerMap, getTestMultiProvider, testChainConnectionConfigs,} from'@hyperlane-xyz/sdk';import { HelloWorldConfig } from'../deploy/config';import { HelloWorldDeployer } from'../deploy/deploy';import { HelloWorld } from'../types';describe('HelloWorld',async () => {constlocalChain='test1';constremoteChain='test2';constlocalDomain= ChainNameToDomainId[localChain];constremoteDomain= ChainNameToDomainId[remoteChain];let signer:SignerWithAddress;let local:HelloWorld;let remote:HelloWorld;let multiProvider:MultiProvider<TestChainNames>;let coreApp:TestCoreApp;let config:ChainMap<TestChainNames,HelloWorldConfig>;before(async () => { [signer] =awaitethers.getSigners(); multiProvider =getTestMultiProvider(signer);constcoreDeployer=newTestCoreDeployer(multiProvider);constcoreContractsMaps=awaitcoreDeployer.deploy(); coreApp =newTestCoreApp(coreContractsMaps, multiProvider); config =coreApp.extendWithConnectionClientConfig(getChainToOwnerMap(testChainConnectionConfigs,signer.address), ); });beforeEach(async () => {consthelloWorld=newHelloWorldDeployer(multiProvider, config, coreApp);constcontracts=awaithelloWorld.deploy(); local = contracts[localChain].router; remote = contracts[remoteChain].router;// The all counts start emptyexpect(awaitlocal.sent()).to.equal(0);expect(awaitlocal.received()).to.equal(0);expect(awaitremote.sent()).to.equal(0);expect(awaitremote.received()).to.equal(0); });it('sends a message',async () => {awaitexpect(local.sendHelloWorld(remoteDomain,'Hello')).to.emit( local,'SentHelloWorld', );// The sent counts are correctexpect(awaitlocal.sent()).to.equal(1);expect(awaitlocal.sentTo(remoteDomain)).to.equal(1);// The received counts are correctexpect(awaitlocal.received()).to.equal(0); });it('handles a message',async () => {awaitlocal.sendHelloWorld(remoteDomain,'World');// Mock processing of the message by HyperlaneawaitcoreApp.processOutboundMessages(localChain);// The initial message has been dispatched.expect(awaitlocal.sent()).to.equal(1);// The initial message has been processed.expect(awaitremote.received()).to.equal(1);expect(awaitremote.receivedFrom(localDomain)).to.equal(1); });