UNPKG

@api3/contracts

Version:

Contracts through which API3 services are delivered

141 lines 6.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chains_1 = require("./generated/chains"); const hardhat_config_1 = require("./hardhat-config"); const strings_1 = require("./utils/strings"); const OLD_ENV = process.env; beforeEach(() => { jest.resetModules(); // Most important - it clears the cache process.env = { ...OLD_ENV }; // Make a copy }); afterAll(() => { process.env = OLD_ENV; // Restore old environment }); describe(hardhat_config_1.getEnvVariableNames.name, () => { it('returns an array with expected env variables', () => { const apiKeyEnvName = (0, hardhat_config_1.etherscanApiKeyName)(); const networkRpcUrlNames = chains_1.CHAINS.map((chain) => (0, hardhat_config_1.networkHttpRpcUrlName)(chain)); const expected = ['MNEMONIC', 'KEYCARD_ACCOUNT', apiKeyEnvName, ...networkRpcUrlNames]; expect((0, hardhat_config_1.getEnvVariableNames)()).toStrictEqual(expected); }); }); describe(hardhat_config_1.blockscout.name, () => { beforeEach(() => { // eslint-disable-next-line jest/no-standalone-expect expect(global.window).toBeUndefined(); }); afterEach(() => { delete global.window; }); it('throws an error if called in a browser-like environment', () => { global.window = {}; expect(() => (0, hardhat_config_1.blockscout)()).toThrow('Cannot be called outside of a Node.js environment'); }); describe('customChains', () => { it('ignores chains without an explorer API', () => { const { customChains } = (0, hardhat_config_1.blockscout)(); const ids = chains_1.CHAINS.filter((c) => !c.verificationApi).map((c) => c.id); customChains.forEach((c) => { expect(ids).not.toContain(c.chainId); }); }); it('includes all other chains', () => { const { customChains } = (0, hardhat_config_1.blockscout)(); const chains = chains_1.CHAINS.filter((c) => !!c.verificationApi); const chainsWithoutAlias = chains.filter((c) => c.verificationApi?.type); customChains.forEach((customChain) => { const chain = chainsWithoutAlias.find((c) => c.id === customChain.chainId.toString()); expect(customChain).toStrictEqual({ network: chain.alias, chainId: Number(chain.id), urls: { apiURL: chain.verificationApi?.type === 'blockscout' || chain.verificationApi?.type === 'other' ? chain.verificationApi?.url : '', browserURL: chain.blockExplorerUrl, }, }); }); }); }); }); describe(hardhat_config_1.etherscan.name, () => { beforeEach(() => { // eslint-disable-next-line jest/no-standalone-expect expect(global.window).toBeUndefined(); }); afterEach(() => { delete global.window; }); it('throws an error if called in a browser-like environment', () => { global.window = {}; expect(() => (0, hardhat_config_1.etherscan)()).toThrow('Cannot be called outside of a Node.js environment'); }); describe('customChains', () => { it('ignores chains without an explorer API', () => { const { customChains } = (0, hardhat_config_1.etherscan)(); const ids = chains_1.CHAINS.filter((c) => !c.verificationApi).map((c) => c.id); customChains.forEach((c) => { expect(ids).not.toContain(c.chainId); }); }); }); }); describe(hardhat_config_1.networks.name, () => { beforeEach(() => { // eslint-disable-next-line jest/no-standalone-expect expect(global.window).toBeUndefined(); }); afterEach(() => { delete global.window; }); it('throws an error if called in a browser-like environment', () => { global.window = {}; expect(() => (0, hardhat_config_1.networks)()).toThrow('Cannot be called outside of a Node.js environment'); }); it('builds a network object for each chain', () => { const result = (0, hardhat_config_1.networks)(); expect(Object.keys(result)).toHaveLength(chains_1.CHAINS.length); chains_1.CHAINS.forEach((chain) => { const defaultProvider = chain.providers.find((p) => p.alias === 'default'); const overrides = chain.hardhatConfigOverrides?.networks ?? {}; expect(result[chain.alias]).toStrictEqual({ accounts: { mnemonic: '' }, chainId: Number(chain.id), url: defaultProvider.rpcUrl, ...overrides, }); }); }); it('sets the mnemonic using the MNEMONIC env variable if it exists', () => { process.env.MNEMONIC = 'test test test test test test test test test test test junk'; const result = (0, hardhat_config_1.networks)(); chains_1.CHAINS.forEach((chain) => { const defaultProvider = chain.providers.find((p) => p.alias === 'default'); const overrides = chain.hardhatConfigOverrides?.networks ?? {}; expect(result[chain.alias]).toStrictEqual({ accounts: { mnemonic: 'test test test test test test test test test test test junk' }, chainId: Number(chain.id), url: defaultProvider.rpcUrl, ...overrides, }); }); }); it('sets the provider URL using the chain alias env variable if it exists', () => { chains_1.CHAINS.forEach((chain) => { const alias = (0, strings_1.toUpperSnakeCase)(chain.alias); process.env[`HARDHAT_HTTP_RPC_URL_${alias}`] = `https://${chain.id}.xyz`; }); const result = (0, hardhat_config_1.networks)(); chains_1.CHAINS.forEach((chain) => { const overrides = chain.hardhatConfigOverrides?.networks ?? {}; expect(result[chain.alias]).toStrictEqual({ accounts: { mnemonic: '' }, chainId: Number(chain.id), url: `https://${chain.id}.xyz`, ...overrides, }); }); }); }); //# sourceMappingURL=hardhat-config.test.js.map