UNPKG

@metamask/snaps-jest

Version:

A Jest preset for end-to-end testing MetaMask Snaps, including a Jest environment, and a set of Jest matchers

177 lines 7.12 kB
import type { AbstractExecutionService } from "@metamask/snaps-controllers"; import type { AccountSelectorState, AssetSelectorState, SnapId } from "@metamask/snaps-sdk"; import type { InstallSnapOptions, SimulationAccount, SimulationAsset, Snap } from "@metamask/snaps-simulation"; import type { CaipAssetType, CaipChainId } from "@metamask/utils"; /** * Load a snap into the environment. This is the main entry point for testing * snaps: It returns a {@link Snap} object that can be used to interact with the * snap. * * @example * import { installSnap } from '@metamask/snaps-jest'; * * describe('My Snap', () => { * it('should do something', async () => { * const { request } = await installSnap('local:my-snap'); * const response = await request({ * method: 'foo', * params: ['bar'], * }); * expect(response).toRespondWith('bar'); * }); * }); * @returns The snap. * @throws If the built-in server is not running, and no snap ID is provided. */ export declare function installSnap(): Promise<Snap>; /** * Load a snap into the environment. This is the main entry point for testing * snaps: It returns a {@link Snap} object that can be used to interact with the * snap. * * @example * import { installSnap } from '@metamask/snaps-jest'; * * describe('My Snap', () => { * it('should do something', async () => { * const { request } = await installSnap('local:my-snap'); * const response = await request({ * method: 'foo', * params: ['bar'], * }); * expect(response).toRespondWith('bar'); * }); * }); * @param options - The options to use. * @param options.executionService - The execution service to use. Defaults to * {@link NodeThreadExecutionService}. You do not need to provide this unless * you are testing a custom execution service. * @param options.executionServiceOptions - The options to use when creating the * execution service, if any. This should only include options specific to the * provided execution service. * @param options.options - The simulation options. * @returns The snap. * @throws If the built-in server is not running, and no snap ID is provided. */ export declare function installSnap<Service extends new (...args: any[]) => InstanceType<typeof AbstractExecutionService>>(options: Partial<InstallSnapOptions<Service>>): Promise<Snap>; /** * Load a snap into the environment. This is the main entry point for testing * snaps: It returns a {@link Snap} object that can be used to interact with the * snap. * * @example * import { installSnap } from '@metamask/snaps-jest'; * * describe('My Snap', () => { * it('should do something', async () => { * const { request } = await installSnap('local:my-snap'); * const response = await request({ * method: 'foo', * params: ['bar'], * }); * expect(response).toRespondWith('bar'); * }); * }); * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults * to the URL of the built-in server, if it is running. This supports both * local snap IDs and NPM snap IDs. * @param options - The options to use. * @param options.executionService - The execution service to use. Defaults to * {@link NodeThreadExecutionService}. You do not need to provide this unless * you are testing a custom execution service. * @param options.executionServiceOptions - The options to use when creating the * execution service, if any. This should only include options specific to the * provided execution service. * @param options.options - The simulation options. * @returns The snap. * @throws If the built-in server is not running, and no snap ID is provided. */ export declare function installSnap<Service extends new (...args: any[]) => InstanceType<typeof AbstractExecutionService>>(snapId: SnapId, options?: Partial<InstallSnapOptions<Service>>): Promise<Snap>; /** * Get the state of an AccountSelector based on a {@link SimulationAccount}. * * @param account - The {@link SimulationAccount} to get the state from. * @returns The state of the AccountSelector. */ export declare function getStateFromAccount(account: SimulationAccount): AccountSelectorState; /** * Get the state of an AssetSelector based on a {@link SimulationAsset}. * * @param id - The Asset id as a CAIP-19 asset type. * @param assets - The {@link SimulationAsset} to get the state from. * @returns The state of the AssetSelector. */ export declare function getStateFromAsset(id: CaipAssetType, assets: Record<CaipAssetType, SimulationAsset>): AssetSelectorState; /** * The base options for the {@link getMockAccount} function. */ export type BaseMockAccountOptions = { /** * The address of the account. */ address: string; /** * The ID of the account. If not provided, a pseudo-random UUID will be * generated. */ id?: string; /** * Whether the account is selected by default. */ selected?: boolean; /** * Whether the account is owned by the snap. */ owned?: boolean; }; /** * Options for creating a mock account with assets or scopes. If `scopes` are * not provided, they will be derived from the `assets`. * * @see BaseMockAccountOptions */ export type MockAccountOptionsWithAssets = BaseMockAccountOptions & { /** * The assets associated with the account. These should be in CAIP format. */ assets: CaipAssetType[]; /** * The scopes associated with the account. If not provided, they will be * derived from the `assets`. */ scopes?: CaipChainId[]; }; /** * Options for creating a mock account with scopes, and optionally assets. * * @see BaseMockAccountOptions */ export type MockAccountOptionsWithScopes = BaseMockAccountOptions & { /** * The scopes associated with the account. These should be in CAIP format. */ scopes: CaipChainId[]; /** * The assets associated with the account. If not provided, it will default * to an empty array. */ assets?: CaipAssetType[]; }; export type GetMockAccountOptions = MockAccountOptionsWithAssets | MockAccountOptionsWithScopes; /** * Get a mock account object for testing purposes. * * @param options - The options for creating the mock account. * @param options.address - The address of the account. * @param options.scopes - The scopes associated with the account, in CAIP * format. If not provided, they will be derived from the `assets`. * @param options.assets - The assets associated with the account, in CAIP * format. If not provided, it will default to an empty array. * @param options.selected - Whether the account is selected by default. * @param options.owned - Whether the account is owned by the snap. * @param options.id - The ID of the account. If not provided, a pseudo-random * UUID will be generated. * @returns A mock account object with the specified properties. */ export declare function getMockAccount({ address, assets, selected, owned, id, scopes, }: GetMockAccountOptions): SimulationAccount; //# sourceMappingURL=helpers.d.mts.map