diff --git a/packages/ua-utils-evm-hardhat/package.json b/packages/ua-utils-evm-hardhat/package.json index 1371e670d..4f61fc341 100644 --- a/packages/ua-utils-evm-hardhat/package.json +++ b/packages/ua-utils-evm-hardhat/package.json @@ -33,7 +33,7 @@ "build": "npx tsup", "clean": "rm -rf dist", "lint": "npx eslint '**/*.{js,ts,json}'", - "test": "jest" + "test": "jest --passWithNoTests" }, "devDependencies": { "@ethersproject/contracts": "^5.7.0", diff --git a/packages/ua-utils-evm-hardhat/src/index.ts b/packages/ua-utils-evm-hardhat/src/index.ts index ac219fd3c..e69de29bb 100644 --- a/packages/ua-utils-evm-hardhat/src/index.ts +++ b/packages/ua-utils-evm-hardhat/src/index.ts @@ -1 +0,0 @@ -export * from './omnigraph' diff --git a/packages/utils-evm-hardhat/hardhat.config.ts b/packages/utils-evm-hardhat/hardhat.config.ts index 3ed980e0b..4ff1ba6ac 100644 --- a/packages/utils-evm-hardhat/hardhat.config.ts +++ b/packages/utils-evm-hardhat/hardhat.config.ts @@ -9,16 +9,18 @@ import { HardhatUserConfig } from 'hardhat/types' const config: HardhatUserConfig = { networks: { 'ethereum-mainnet': { - url: 'https://eth.llamarpc.com', - saveDeployments: true, + url: 'no:///way', + saveDeployments: false, eid: EndpointId.ETHEREUM_MAINNET, }, 'ethereum-testnet': { - url: 'https://eth-goerli.public.blastapi.io', + url: 'no:///way', + saveDeployments: false, eid: EndpointId.ETHEREUM_TESTNET, }, 'bsc-testnet': { - url: 'https://bsc-testnet.publicnode.com', + url: 'no:///way', + saveDeployments: false, accounts: { mnemonic: 'test test test test test test test test test test test junk', }, diff --git a/packages/utils-evm-hardhat/jest.config.js b/packages/utils-evm-hardhat/jest.config.js new file mode 100644 index 000000000..16148cfb1 --- /dev/null +++ b/packages/utils-evm-hardhat/jest.config.js @@ -0,0 +1,8 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + moduleNameMapper: { + '^@/(.*)$': '/src/$1', + }, +}; diff --git a/packages/utils-evm-hardhat/package.json b/packages/utils-evm-hardhat/package.json index eb54df438..523d2a7c7 100644 --- a/packages/utils-evm-hardhat/package.json +++ b/packages/utils-evm-hardhat/package.json @@ -34,7 +34,7 @@ "clean": "rm -rf dist", "dev": "npx tsup --watch", "lint": "npx eslint '**/*.{js,ts,json}'", - "test": "npx hardhat test" + "test": "jest" }, "dependencies": { "micro-memoize": "~4.1.2", @@ -51,14 +51,14 @@ "@layerzerolabs/test-utils": "~0.0.1", "@layerzerolabs/utils-evm": "~0.0.1", "@types/chai-as-promised": "^7.1.7", - "@types/mocha": "^10.0.6", - "chai": "^4.3.10", - "chai-as-promised": "^7.1.1", + "@types/jest": "^29.5.10", "fast-check": "^3.14.0", "hardhat": "^2.19.0", "hardhat-deploy": "^0.11.22", + "jest": "^29.7.0", "p-memoize": "~4.0.1", "sinon": "^17.0.1", + "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "tsup": "~8.0.1", "typescript": "^5.2.2", diff --git a/packages/utils-evm-hardhat/src/index.ts b/packages/utils-evm-hardhat/src/index.ts index a66a5f857..e6503cd6b 100644 --- a/packages/utils-evm-hardhat/src/index.ts +++ b/packages/utils-evm-hardhat/src/index.ts @@ -5,6 +5,7 @@ import './type-extensions' export * from './config' export * from './internal' export * from './logger' +export * from './omnigraph' export * from './provider' export * from './runtime' export * from './signer' diff --git a/packages/utils-evm-hardhat/src/internal/assertions.ts b/packages/utils-evm-hardhat/src/internal/assertions.ts index de10656dd..7d700153b 100644 --- a/packages/utils-evm-hardhat/src/internal/assertions.ts +++ b/packages/utils-evm-hardhat/src/internal/assertions.ts @@ -7,7 +7,8 @@ export interface HardhatRuntimeEnvironmentWithDeployments extends HardhatRuntime deployments: DeploymentsExtension } -export const assertHardhatDeploy = ( +export function assertHardhatDeploy( hre: HardhatRuntimeEnvironment -): asserts hre is HardhatRuntimeEnvironmentWithDeployments => +): asserts hre is HardhatRuntimeEnvironmentWithDeployments { assert(hre.deployments, `You don't seem to be using hardhat-deploy in your project`) +} diff --git a/packages/ua-utils-evm-hardhat/src/omnigraph/builder.ts b/packages/utils-evm-hardhat/src/omnigraph/builder.ts similarity index 80% rename from packages/ua-utils-evm-hardhat/src/omnigraph/builder.ts rename to packages/utils-evm-hardhat/src/omnigraph/builder.ts index 736c81493..03f283918 100644 --- a/packages/ua-utils-evm-hardhat/src/omnigraph/builder.ts +++ b/packages/utils-evm-hardhat/src/omnigraph/builder.ts @@ -3,12 +3,12 @@ import type { OmniContractFactory, OmniGraphHardhat } from './types' import { OmniGraphBuilder } from '@layerzerolabs/utils' import { omniContractToPoint } from '@layerzerolabs/utils-evm' -export class OmniGraphBuilderHardhat extends OmniGraphBuilder { +export class OmniGraphBuilderHardhat { static async fromConfig( graph: OmniGraphHardhat, contractFactory: OmniContractFactory - ): Promise> { - const builder = new OmniGraphBuilderHardhat() + ): Promise> { + const builder = new OmniGraphBuilder() const nodes: OmniNode[] = await Promise.all( graph.contracts.map(async ({ contract, config }) => ({ diff --git a/packages/ua-utils-evm-hardhat/src/omnigraph/coordinates.ts b/packages/utils-evm-hardhat/src/omnigraph/coordinates.ts similarity index 85% rename from packages/ua-utils-evm-hardhat/src/omnigraph/coordinates.ts rename to packages/utils-evm-hardhat/src/omnigraph/coordinates.ts index 874ae4f10..976ef09cf 100644 --- a/packages/ua-utils-evm-hardhat/src/omnigraph/coordinates.ts +++ b/packages/utils-evm-hardhat/src/omnigraph/coordinates.ts @@ -7,11 +7,8 @@ import { OmniContract } from '@layerzerolabs/utils-evm' import { Contract } from '@ethersproject/contracts' import assert from 'assert' import { OmniContractFactory } from './types' -import { - assertHardhatDeploy, - createNetworkEnvironmentFactory, - getDefaultRuntimeEnvironment, -} from '@layerzerolabs/utils-evm-hardhat' +import { createNetworkEnvironmentFactory, getDefaultRuntimeEnvironment } from '@/runtime' +import { assertHardhatDeploy } from '@/internal/assertions' export interface OmniDeployment { eid: EndpointId @@ -33,11 +30,6 @@ export const createContractFactory = ( const env = await environmentFactory(eid) assertHardhatDeploy(env) - assert( - contractName != null || address != null, - 'At least one of contractName, address must be specified for OmniPointHardhat' - ) - // If we have both the contract name & address, we go off artifacts if (contractName != null && address != null) { const artifact = await env.deployments.getArtifact(contractName) @@ -49,7 +41,7 @@ export const createContractFactory = ( // If we have the contract name but no address, we need to get it from the deployments by name if (contractName != null && address == null) { const deployment = await env.deployments.getOrNull(contractName) - assert(deployment != null, `Could not find a deployment for contract '${contractName}`) + assert(deployment != null, `Could not find a deployment for contract '${contractName}'`) return omniDeploymentToContract({ eid, deployment }) } @@ -57,9 +49,11 @@ export const createContractFactory = ( // And if we only have the address, we need to go get it from deployments by address if (address != null) { const [deployment] = await env.deployments.getDeploymentsFromAddress(address) - assert(deployment != null, `Could not find a deployment for address '${address}`) + assert(deployment != null, `Could not find a deployment for address '${address}'`) return omniDeploymentToContract({ eid, deployment }) } + + assert(false, 'At least one of contractName, address must be specified for OmniPointHardhat') }) } diff --git a/packages/ua-utils-evm-hardhat/src/omnigraph/index.ts b/packages/utils-evm-hardhat/src/omnigraph/index.ts similarity index 100% rename from packages/ua-utils-evm-hardhat/src/omnigraph/index.ts rename to packages/utils-evm-hardhat/src/omnigraph/index.ts diff --git a/packages/ua-utils-evm-hardhat/src/omnigraph/types.ts b/packages/utils-evm-hardhat/src/omnigraph/types.ts similarity index 100% rename from packages/ua-utils-evm-hardhat/src/omnigraph/types.ts rename to packages/utils-evm-hardhat/src/omnigraph/types.ts diff --git a/packages/utils-evm-hardhat/test/config.test.ts b/packages/utils-evm-hardhat/test/config.test.ts index b609bc3df..38501d051 100644 --- a/packages/utils-evm-hardhat/test/config.test.ts +++ b/packages/utils-evm-hardhat/test/config.test.ts @@ -1,6 +1,4 @@ import { EndpointId } from '@layerzerolabs/lz-definitions' -import { expect } from 'chai' -import { describe } from 'mocha' import { withLayerZeroArtifacts, withLayerZeroDeployments } from '../src/config' import { dirname, join } from 'path' @@ -13,7 +11,7 @@ describe('config', () => { it('should add no external deployments if no networks have been specified', () => { const config = {} - expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({ + expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({ external: { deployments: {}, }, @@ -27,7 +25,7 @@ describe('config', () => { }, } - expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({ + expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({ networks: { 'vengaboys-testnet': {}, }, @@ -46,7 +44,7 @@ describe('config', () => { }, } - expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({ + expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({ networks: { 'vengaboys-testnet': { eid: 0, @@ -67,7 +65,7 @@ describe('config', () => { }, } - expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({ + expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({ networks: { 'vengaboys-testnet': { eid: EndpointId.ARBITRUM_MAINNET, @@ -96,7 +94,7 @@ describe('config', () => { )(config) const configWithSomePathAgain = withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(configWithSomePath) - expect(configWithSomePathAgain).to.eql({ + expect(configWithSomePathAgain).toEqual({ networks: { 'vengaboys-testnet': { eid: EndpointId.BSC_TESTNET, @@ -121,7 +119,7 @@ describe('config', () => { networks: {}, } - expect(withLayerZeroArtifacts('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({ + expect(withLayerZeroArtifacts('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({ networks: {}, external: { contracts: [ @@ -154,7 +152,7 @@ describe('config', () => { )(config) const configWithSomePathAgain = withLayerZeroArtifacts('@layerzerolabs/lz-evm-sdk-v1')(configWithSomePath) - expect(configWithSomePathAgain).to.eql({ + expect(configWithSomePathAgain).toEqual({ networks: {}, external: { contracts: [ diff --git a/packages/ua-utils-evm-hardhat/test/omnigraph/coordinates.test.ts b/packages/utils-evm-hardhat/test/omnigraph/coordinates.test.ts similarity index 71% rename from packages/ua-utils-evm-hardhat/test/omnigraph/coordinates.test.ts rename to packages/utils-evm-hardhat/test/omnigraph/coordinates.test.ts index d05b7cde0..85aeea656 100644 --- a/packages/ua-utils-evm-hardhat/test/omnigraph/coordinates.test.ts +++ b/packages/utils-evm-hardhat/test/omnigraph/coordinates.test.ts @@ -1,5 +1,6 @@ import fc from 'fast-check' import hre from 'hardhat' +import { DeploymentsManager } from 'hardhat-deploy/dist/src/DeploymentsManager' import { Deployment, DeploymentSubmission } from 'hardhat-deploy/dist/types' import { endpointArbitrary, evmAddressArbitrary } from '@layerzerolabs/test-utils' import { OmniDeployment, createContractFactory, omniDeploymentToContract, omniDeploymentToPoint } from '@/omnigraph' @@ -9,6 +10,8 @@ import { HardhatRuntimeEnvironment } from 'hardhat/types' import { Contract } from '@ethersproject/contracts' import { makeZero } from '@layerzerolabs/utils-evm' +jest.spyOn(DeploymentsManager.prototype, 'getChainId').mockResolvedValue('1') + describe('omnigraph/coordinates', () => { describe('omniDeploymentToPoint', () => { it('should just work', () => { @@ -39,47 +42,35 @@ describe('omnigraph/coordinates', () => { }) describe('createContractFactory', () => { - // Hardhat deploy will try to get the chain ID from the RPC so we can't let it - const mockSend = (env: HardhatRuntimeEnvironment) => { - env.network.provider.send = jest.fn().mockResolvedValue(1) - } - describe('when called with OmniPointContractName', () => { it('should reject when eid does not exist', async () => { - const deploymentFactory = createContractFactory(hre) + const contractFactory = createContractFactory(hre) await expect(() => - deploymentFactory({ eid: EndpointId.CANTO_TESTNET, contractName: 'MyContract' }) + contractFactory({ eid: EndpointId.CANTO_TESTNET, contractName: 'MyContract' }) ).rejects.toBeTruthy() }) it('should reject when contract has not been deployed', async () => { - const environmentFactory = createNetworkEnvironmentFactory(hre) - const deploymentFactory = createContractFactory(hre) - - const env = await environmentFactory(EndpointId.ETHEREUM_MAINNET) - mockSend(env) + const contractFactory = createContractFactory(hre) await expect(() => - deploymentFactory({ eid: EndpointId.ETHEREUM_MAINNET, contractName: 'MyContract' }) + contractFactory({ eid: EndpointId.ETHEREUM_MAINNET, contractName: 'MyContract' }) ).rejects.toBeTruthy() }) it('should resolve when contract has been deployed', async () => { const environmentFactory = createNetworkEnvironmentFactory(hre) - const deploymentFactory = createContractFactory(hre) + const contractFactory = createContractFactory(hre, environmentFactory) const env = await environmentFactory(EndpointId.ETHEREUM_MAINNET) - mockSend(env) - - // We'll create a dummy deployment first - await env.deployments.save('MyContract', { + jest.spyOn(env.deployments, 'getOrNull').mockResolvedValue({ address: makeZero(undefined), abi: [], - } as DeploymentSubmission) + }) // Then check whether the factory will get it for us - const deployment = await deploymentFactory({ + const deployment = await contractFactory({ eid: EndpointId.ETHEREUM_MAINNET, contractName: 'MyContract', }) @@ -88,6 +79,7 @@ describe('omnigraph/coordinates', () => { eid: EndpointId.ETHEREUM_MAINNET, contract: expect.any(Contract), }) + expect(env.deployments.getOrNull).toHaveBeenCalledWith('MyContract') }) }) @@ -95,10 +87,10 @@ describe('omnigraph/coordinates', () => { it('should reject when eid does not exist', async () => { await fc.assert( fc.asyncProperty(evmAddressArbitrary, async (address) => { - const deploymentFactory = createContractFactory(hre) + const contractFactory = createContractFactory(hre) await expect(() => - deploymentFactory({ eid: EndpointId.CANTO_TESTNET, address }) + contractFactory({ eid: EndpointId.CANTO_TESTNET, address }) ).rejects.toBeTruthy() }) ) @@ -107,11 +99,11 @@ describe('omnigraph/coordinates', () => { it('should reject when contract has not been deployed', async () => { await fc.assert( fc.asyncProperty(evmAddressArbitrary, async (address) => { - const deploymentFactory = createContractFactory(hre) + const contractFactory = createContractFactory(hre) await expect(() => - deploymentFactory({ eid: EndpointId.ETHEREUM_MAINNET, address }) - ).rejects.toBeTruthy() + contractFactory({ eid: EndpointId.ETHEREUM_MAINNET, address }) + ).rejects.toThrow(/Could not find a deployment for address/) }) ) }) @@ -120,16 +112,18 @@ describe('omnigraph/coordinates', () => { await fc.assert( fc.asyncProperty(evmAddressArbitrary, async (address) => { const environmentFactory = createNetworkEnvironmentFactory(hre) - const deploymentFactory = createContractFactory(hre) + const contractFactory = createContractFactory(hre, environmentFactory) const env = await environmentFactory(EndpointId.ETHEREUM_MAINNET) - mockSend(env) - - // We'll create a dummy deployment with the specified address first - await env.deployments.save('MyContract', { address, abi: [] } as DeploymentSubmission) + jest.spyOn(env.deployments, 'getDeploymentsFromAddress').mockResolvedValue([ + { + address: makeZero(undefined), + abi: [], + }, + ]) // Then check whether the factory will get it for us - const deployment = await deploymentFactory({ + const deployment = await contractFactory({ eid: EndpointId.ETHEREUM_MAINNET, address, }) @@ -138,6 +132,7 @@ describe('omnigraph/coordinates', () => { eid: EndpointId.ETHEREUM_MAINNET, contract: expect.any(Contract), }) + expect(env.deployments.getOrNull).toHaveBeenCalledWith('MyContract') }) ) }) diff --git a/packages/utils-evm-hardhat/test/provider.test.ts b/packages/utils-evm-hardhat/test/provider.test.ts index c9c0ed53f..068bb60fe 100644 --- a/packages/utils-evm-hardhat/test/provider.test.ts +++ b/packages/utils-evm-hardhat/test/provider.test.ts @@ -1,26 +1,34 @@ -import chai from 'chai' -import chaiAsPromised from 'chai-as-promised' -import { expect } from 'chai' import { getNetworkRuntimeEnvironment } from '../src/runtime' import hre from 'hardhat' import { EndpointId } from '@layerzerolabs/lz-definitions' import { createProviderFactory } from '../src/provider' import { Web3Provider } from '@ethersproject/providers' -chai.use(chaiAsPromised) +// Ethers calls the eth_chainId RPC method when initializing a provider so we mock the result +jest.spyOn(Web3Provider.prototype, 'send').mockResolvedValue('1') describe('provider', () => { + // Web3Provider.prototype.s describe('createProviderFactory', () => { + beforeAll(() => {}) + it('should reject with an endpoint that is not in the hardhat config', async () => { - await expect(createProviderFactory(hre)(EndpointId.CATHAY_TESTNET)).to.eventually.be.rejected + await expect(createProviderFactory(hre)(EndpointId.CATHAY_TESTNET)).rejects.toBeTruthy() }) it('should return a Web3Provider wrapping the network provider', async () => { const env = await getNetworkRuntimeEnvironment('ethereum-mainnet') const provider = await createProviderFactory(hre)(EndpointId.ETHEREUM_MAINNET) - expect(provider).to.be.instanceOf(Web3Provider) - expect(provider.provider).to.equal(env.network.provider) + expect(provider).toBeInstanceOf(Web3Provider) + expect(provider.provider).toEqual(env.network.provider) + + // Ethers has this ugly habit of importing files here and there, + // firing RPC requests and all. + // + // If we don't wait for the provider to be ready, jest will complain + // about requests being made after test teardown + await provider.ready }) }) }) diff --git a/packages/utils-evm-hardhat/test/runtime.test.ts b/packages/utils-evm-hardhat/test/runtime.test.ts index 49ac2129e..446363c13 100644 --- a/packages/utils-evm-hardhat/test/runtime.test.ts +++ b/packages/utils-evm-hardhat/test/runtime.test.ts @@ -1,32 +1,34 @@ -import chai from 'chai' -import chaiAsPromised from 'chai-as-promised' -import { expect } from 'chai' +import hre from 'hardhat' +import { DeploymentsManager } from 'hardhat-deploy/dist/src/DeploymentsManager' import { createNetworkEnvironmentFactory, getNetworkRuntimeEnvironment } from '../src/runtime' import type { DeploymentSubmission } from 'hardhat-deploy/dist/types' -import hre from 'hardhat' import { EndpointId } from '@layerzerolabs/lz-definitions' -chai.use(chaiAsPromised) +jest.spyOn(DeploymentsManager.prototype, 'getChainId').mockResolvedValue('1') describe('runtime', () => { describe('getNetworkRuntimeEnvironment', () => { it('should reject with an invalid network', async () => { - await expect(getNetworkRuntimeEnvironment('not-in-hardhat-config')).to.eventually.be.rejected + await expect(getNetworkRuntimeEnvironment('not-in-hardhat-config')).rejects.toBeTruthy() }) it('should return a HardhatRuntimeEnvironment with correct network', async () => { const runtime = await getNetworkRuntimeEnvironment('ethereum-mainnet') - expect(runtime.network.name).to.eql('ethereum-mainnet') - expect(runtime.deployments).to.be.an('object') + expect(runtime.network.name).toEqual('ethereum-mainnet') + expect(runtime.deployments).toMatchObject({ + get: expect.any(Function), + }) }) it('should have the config setup correctly', async () => { const ethRuntime = await getNetworkRuntimeEnvironment('ethereum-mainnet') const bscRuntime = await getNetworkRuntimeEnvironment('bsc-testnet') - expect(ethRuntime.network.config.saveDeployments).to.be.true - expect(bscRuntime.network.config.saveDeployments).to.be.undefined + expect(ethRuntime.network.name).toBe('ethereum-mainnet') + expect(ethRuntime.network.config).toBe(hre.config.networks['ethereum-mainnet']) + expect(bscRuntime.network.name).toBe('bsc-testnet') + expect(bscRuntime.network.config).toEqual(hre.config.networks['bsc-testnet']) }) it('should save the deployment to correct network', async () => { @@ -42,24 +44,26 @@ describe('runtime', () => { // Then we check whether it has been saved const deployment = await bscRuntime.deployments.get('Mock') - expect(deployment.args).to.eql(deploymentSubmission.args) + expect(deployment.args).toEqual(deploymentSubmission.args) // And finally we check whether it was not by accident saved for ethereum-mainnet const nonExistentDeployment = await ethRuntime.deployments.getOrNull('Mock') - expect(nonExistentDeployment?.args).not.to.eql(deploymentSubmission.args) + expect(nonExistentDeployment?.args).not.toEqual(deploymentSubmission.args) }) }) describe('createNetworkEnvironmentFactory', () => { it('should reject with an endpoint that is not in the hardhat config', async () => { - await expect(createNetworkEnvironmentFactory(hre)(EndpointId.CATHAY_TESTNET)).to.eventually.be.rejected + await expect(createNetworkEnvironmentFactory(hre)(EndpointId.CATHAY_TESTNET)).rejects.toBeTruthy() }) it('should return a HardhatRuntimeEnvironment with correct network', async () => { const runtime = await createNetworkEnvironmentFactory(hre)(EndpointId.ETHEREUM_MAINNET) - expect(runtime.network.name).to.eql('ethereum-mainnet') - expect(runtime.deployments).to.be.an('object') + expect(runtime.network.name).toEqual('ethereum-mainnet') + expect(runtime.deployments).toMatchObject({ + get: expect.any(Function), + }) }) }) }) diff --git a/packages/utils-evm-hardhat/test/signer.test.ts b/packages/utils-evm-hardhat/test/signer.test.ts index 065314d60..904f0ada1 100644 --- a/packages/utils-evm-hardhat/test/signer.test.ts +++ b/packages/utils-evm-hardhat/test/signer.test.ts @@ -1,25 +1,31 @@ -import chai from 'chai' -import chaiAsPromised from 'chai-as-promised' -import { expect } from 'chai' +import 'hardhat' import { EndpointId } from '@layerzerolabs/lz-definitions' import { createSignerFactory } from '../src/signer/factory' import { JsonRpcSigner, Web3Provider } from '@ethersproject/providers' import { OmniSignerEVM } from '@layerzerolabs/utils-evm' -chai.use(chaiAsPromised) +// Ethers calls the eth_chainId RPC method when initializing a provider so we mock the result +jest.spyOn(Web3Provider.prototype, 'send').mockResolvedValue('1') describe('signer', () => { describe('createSignerFactory', () => { it('should reject with an endpoint that is not in the hardhat config', async () => { - await expect(createSignerFactory()(EndpointId.CATHAY_TESTNET)).to.eventually.be.rejected + await expect(createSignerFactory()(EndpointId.CATHAY_TESTNET)).rejects.toBeTruthy() }) it('should return an OmniSignerEVM', async () => { const signer = await createSignerFactory()(EndpointId.ETHEREUM_MAINNET) - expect(signer).to.be.instanceOf(OmniSignerEVM) - expect(signer.signer).to.be.instanceOf(JsonRpcSigner) - expect(signer.signer.provider).to.be.instanceOf(Web3Provider) + expect(signer).toBeInstanceOf(OmniSignerEVM) + expect(signer.signer).toBeInstanceOf(JsonRpcSigner) + expect(signer.signer.provider).toBeInstanceOf(Web3Provider) + + // Ethers has this ugly habit of importing files here and there, + // firing RPC requests and all. + // + // If we don't wait for the provider to be ready, jest will complain + // about requests being made after test teardown + await (signer.signer.provider as Web3Provider)?.ready }) }) }) diff --git a/packages/utils-evm-hardhat/tsconfig.json b/packages/utils-evm-hardhat/tsconfig.json index 1e471dfdd..7da258243 100644 --- a/packages/utils-evm-hardhat/tsconfig.json +++ b/packages/utils-evm-hardhat/tsconfig.json @@ -4,6 +4,9 @@ "include": ["src", "test", "*.config.ts"], "compilerOptions": { "module": "commonjs", - "types": ["node", "mocha"] + "types": ["node", "jest"], + "paths": { + "@/*": ["./src/*"] + } } }