@metamask/snaps-jest
Version:
A Jest preset for end-to-end testing MetaMask Snaps, including a Jest environment, and a set of Jest matchers
150 lines • 5.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMockAccount = exports.getStateFromAsset = exports.getStateFromAccount = exports.installSnap = void 0;
const snaps_utils_1 = require("@metamask/snaps-utils");
const utils_1 = require("@metamask/utils");
const internals_1 = require("./internals/index.cjs");
const log = (0, utils_1.createModuleLogger)(internals_1.rootLogger, 'helpers');
/**
* Get the options for {@link installSnap}.
*
* @param snapId - The ID of the Snap, or the options.
* @param options - The options, if any.
* @returns The options.
*/
function getOptions(snapId, options) {
if (typeof snapId === 'object') {
return [undefined, snapId];
}
return [snapId, options];
}
/**
* 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.
*/
async function installSnap(snapId, options = {}) {
const resolvedOptions = getOptions(snapId, options);
// TODO: Either fix this lint violation or explain why it's necessary to
// ignore.
/* eslint-disable @typescript-eslint/unbound-method */
const { request, onTransaction, sendTransaction, onSignature, onCronjob, runCronjob, onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, onInstall, onUpdate, onStart, onNameLookup, onProtocolRequest, onClientRequest, mockJsonRpc, close, } = await (0, internals_1.getEnvironment)().installSnap(...resolvedOptions);
/* eslint-enable @typescript-eslint/unbound-method */
return {
request,
onTransaction,
sendTransaction,
onSignature,
onCronjob,
runCronjob,
onBackgroundEvent,
onHomePage,
onSettingsPage,
onKeyringRequest,
onInstall,
onUpdate,
onStart,
onNameLookup,
onProtocolRequest,
onClientRequest,
mockJsonRpc,
close: async () => {
log('Closing execution service.');
(0, snaps_utils_1.logInfo)('Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.');
await close();
},
};
}
exports.installSnap = installSnap;
/**
* 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.
*/
function getStateFromAccount(account) {
const { address, scopes } = account;
return {
addresses: scopes.map((scope) => `${scope}:${address}`),
accountId: account.id,
};
}
exports.getStateFromAccount = getStateFromAccount;
/**
* 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.
*/
function getStateFromAsset(id, assets) {
const asset = assets[id];
(0, utils_1.assert)(asset, `Asset with ID "${id}" not found in simulation assets.`);
const { symbol, name } = asset;
return {
asset: id,
symbol,
name,
};
}
exports.getStateFromAsset = getStateFromAsset;
/**
* Generate a pseudo-random UUID.
*
* @returns A pseudo-random UUID string.
*/
const getPseudoRandomUuid = (0, internals_1.getPseudoRandomUuidGenerator)();
/**
* 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.
*/
function getMockAccount({ address, assets = [], selected = false, owned = false, id = getPseudoRandomUuid(), scopes = (0, internals_1.getScopesFromAssets)(assets), }) {
return {
address,
id,
scopes,
selected,
owned,
assets,
};
}
exports.getMockAccount = getMockAccount;
//# sourceMappingURL=helpers.cjs.map