@metamask/snaps-jest 9.4.0 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -1
- package/dist/helpers.cjs +2 -1
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +2 -1
- package/dist/helpers.mjs.map +1 -1
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [9.5.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Allow mocking JSON-RPC implementations ([#3667](https://github.com/MetaMask/snaps/pull/3667))
|
|
15
|
+
|
|
16
|
+
## [9.4.1]
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- Bump MetaMask dependencies ([#3651](https://github.com/MetaMask/snaps/pull/3651), [#3638](https://github.com/MetaMask/snaps/pull/3638), [#3648](https://github.com/MetaMask/snaps/pull/3648), [#3630](https://github.com/MetaMask/snaps/pull/3630), [#3628](https://github.com/MetaMask/snaps/pull/3628), [#3629](https://github.com/MetaMask/snaps/pull/3629), [#3607](https://github.com/MetaMask/snaps/pull/3607), [#3623](https://github.com/MetaMask/snaps/pull/3623), [#3612](https://github.com/MetaMask/snaps/pull/3612))
|
|
21
|
+
|
|
10
22
|
## [9.4.0]
|
|
11
23
|
|
|
12
24
|
### Added
|
|
@@ -411,7 +423,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
411
423
|
- The version of the package no longer needs to match the version of all other
|
|
412
424
|
MetaMask Snaps packages.
|
|
413
425
|
|
|
414
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.
|
|
426
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.5.0...HEAD
|
|
427
|
+
[9.5.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.4.1...@metamask/snaps-jest@9.5.0
|
|
428
|
+
[9.4.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.4.0...@metamask/snaps-jest@9.4.1
|
|
415
429
|
[9.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.3.0...@metamask/snaps-jest@9.4.0
|
|
416
430
|
[9.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.2.0...@metamask/snaps-jest@9.3.0
|
|
417
431
|
[9.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-jest@9.1.0...@metamask/snaps-jest@9.2.0
|
package/dist/helpers.cjs
CHANGED
|
@@ -55,7 +55,7 @@ async function installSnap(snapId, options = {}) {
|
|
|
55
55
|
// TODO: Either fix this lint violation or explain why it's necessary to
|
|
56
56
|
// ignore.
|
|
57
57
|
/* eslint-disable @typescript-eslint/unbound-method */
|
|
58
|
-
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);
|
|
58
|
+
const { request, onTransaction, sendTransaction, onSignature, onCronjob, runCronjob, onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, onInstall, onUpdate, onStart, onNameLookup, onProtocolRequest, onClientRequest, mockJsonRpc, mockJsonRpcOnce, close, } = await (0, internals_1.getEnvironment)().installSnap(...resolvedOptions);
|
|
59
59
|
/* eslint-enable @typescript-eslint/unbound-method */
|
|
60
60
|
return {
|
|
61
61
|
request,
|
|
@@ -75,6 +75,7 @@ async function installSnap(snapId, options = {}) {
|
|
|
75
75
|
onProtocolRequest,
|
|
76
76
|
onClientRequest,
|
|
77
77
|
mockJsonRpc,
|
|
78
|
+
mockJsonRpcOnce,
|
|
78
79
|
close: async () => {
|
|
79
80
|
log('Closing execution service.');
|
|
80
81
|
(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.');
|
package/dist/helpers.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.cjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAYA,uDAAgD;AAChD,2CAA6D;AAO7D,qDAKqB;AAErB,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAU,EAAE,SAAS,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,SAAS,UAAU,CAKjB,MAAiE,EACjE,OAA6C;IAE7C,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC3B,CAAC;AAqGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACI,KAAK,UAAU,WAAW,CAK/B,MAAsD,EACtD,UAAgD,EAAE;IAElD,MAAM,eAAe,GAAG,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEpD,wEAAwE;IACxE,WAAW;IACX,sDAAsD;IACtD,MAAM,EACJ,OAAO,EACP,aAAa,EACb,eAAe,EACf,WAAW,EACX,SAAS,EACT,UAAU,EACV,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,gBAAgB,EAChB,SAAS,EACT,QAAQ,EACR,OAAO,EACP,YAAY,EACZ,iBAAiB,EACjB,eAAe,EACf,WAAW,EACX,KAAK,GACN,GAAG,MAAM,IAAA,0BAAc,GAAE,CAAC,WAAW,CAAC,GAAG,eAAe,CAAC,CAAC;IAC3D,qDAAqD;IAErD,OAAO;QACL,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AK,GAAG,KAAK,EACb,EAAE,GAAG,mBAAmB,EAAE,EAC1B,MAAM,GAAG,IAAA,+BAAmB,EAAC,MAAM,CAAC,GACd;IACtB,OAAO;QACL,OAAO;QACP,EAAE;QACF,MAAM;QACN,QAAQ;QACR,KAAK;QACL,MAAM;KACP,CAAC;AACJ,CAAC;AAhBD,wCAgBC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport type {\n AccountSelectorState,\n AssetSelectorState,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type {\n InstallSnapOptions,\n SimulationAccount,\n SimulationAsset,\n Snap,\n} from '@metamask/snaps-simulation';\nimport { logInfo } from '@metamask/snaps-utils';\nimport { assert, createModuleLogger } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipAssetType,\n CaipChainId,\n} from '@metamask/utils';\n\nimport {\n rootLogger,\n getEnvironment,\n getPseudoRandomUuidGenerator,\n getScopesFromAssets,\n} from './internals';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * Get the options for {@link installSnap}.\n *\n * @param snapId - The ID of the Snap, or the options.\n * @param options - The options, if any.\n * @returns The options.\n */\nfunction getOptions<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId | Partial<InstallSnapOptions<Service>> | undefined,\n options: Partial<InstallSnapOptions<Service>>,\n): [SnapId | undefined, Partial<InstallSnapOptions<Service>>] {\n if (typeof snapId === 'object') {\n return [undefined, snapId];\n }\n\n return [snapId, options];\n}\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap(): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(options: Partial<InstallSnapOptions<Service>>): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId,\n options?: Partial<InstallSnapOptions<Service>>,\n): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId?: SnapId | Partial<InstallSnapOptions<Service>>,\n options: Partial<InstallSnapOptions<Service>> = {},\n): Promise<Snap> {\n const resolvedOptions = getOptions(snapId, options);\n\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n /* eslint-disable @typescript-eslint/unbound-method */\n const {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n close,\n } = await getEnvironment().installSnap(...resolvedOptions);\n /* eslint-enable @typescript-eslint/unbound-method */\n\n return {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n close: async () => {\n log('Closing execution service.');\n logInfo(\n 'Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.',\n );\n\n await close();\n },\n };\n}\n\n/**\n * Get the state of an AccountSelector based on a {@link SimulationAccount}.\n *\n * @param account - The {@link SimulationAccount} to get the state from.\n * @returns The state of the AccountSelector.\n */\nexport function getStateFromAccount(\n account: SimulationAccount,\n): AccountSelectorState {\n const { address, scopes } = account;\n return {\n addresses: scopes.map((scope) => `${scope}:${address}`) as CaipAccountId[],\n accountId: account.id,\n };\n}\n\n/**\n * Get the state of an AssetSelector based on a {@link SimulationAsset}.\n *\n * @param id - The Asset id as a CAIP-19 asset type.\n * @param assets - The {@link SimulationAsset} to get the state from.\n * @returns The state of the AssetSelector.\n */\nexport function getStateFromAsset(\n id: CaipAssetType,\n assets: Record<CaipAssetType, SimulationAsset>,\n): AssetSelectorState {\n const asset = assets[id];\n\n assert(asset, `Asset with ID \"${id}\" not found in simulation assets.`);\n\n const { symbol, name } = asset;\n\n return {\n asset: id,\n symbol,\n name,\n };\n}\n\n/**\n * Generate a pseudo-random UUID.\n *\n * @returns A pseudo-random UUID string.\n */\nconst getPseudoRandomUuid = getPseudoRandomUuidGenerator();\n\n/**\n * The base options for the {@link getMockAccount} function.\n */\nexport type BaseMockAccountOptions = {\n /**\n * The address of the account.\n */\n address: string;\n\n /**\n * The ID of the account. If not provided, a pseudo-random UUID will be\n * generated.\n */\n id?: string;\n\n /**\n * Whether the account is selected by default.\n */\n selected?: boolean;\n\n /**\n * Whether the account is owned by the snap.\n */\n owned?: boolean;\n};\n\n/**\n * Options for creating a mock account with assets or scopes. If `scopes` are\n * not provided, they will be derived from the `assets`.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithAssets = BaseMockAccountOptions & {\n /**\n * The assets associated with the account. These should be in CAIP format.\n */\n assets: CaipAssetType[];\n\n /**\n * The scopes associated with the account. If not provided, they will be\n * derived from the `assets`.\n */\n scopes?: CaipChainId[];\n};\n\n/**\n * Options for creating a mock account with scopes, and optionally assets.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithScopes = BaseMockAccountOptions & {\n /**\n * The scopes associated with the account. These should be in CAIP format.\n */\n scopes: CaipChainId[];\n\n /**\n * The assets associated with the account. If not provided, it will default\n * to an empty array.\n */\n assets?: CaipAssetType[];\n};\n\nexport type GetMockAccountOptions =\n | MockAccountOptionsWithAssets\n | MockAccountOptionsWithScopes;\n\n/**\n * Get a mock account object for testing purposes.\n *\n * @param options - The options for creating the mock account.\n * @param options.address - The address of the account.\n * @param options.scopes - The scopes associated with the account, in CAIP\n * format. If not provided, they will be derived from the `assets`.\n * @param options.assets - The assets associated with the account, in CAIP\n * format. If not provided, it will default to an empty array.\n * @param options.selected - Whether the account is selected by default.\n * @param options.owned - Whether the account is owned by the snap.\n * @param options.id - The ID of the account. If not provided, a pseudo-random\n * UUID will be generated.\n * @returns A mock account object with the specified properties.\n */\nexport function getMockAccount({\n address,\n assets = [],\n selected = false,\n owned = false,\n id = getPseudoRandomUuid(),\n scopes = getScopesFromAssets(assets),\n}: GetMockAccountOptions): SimulationAccount {\n return {\n address,\n id,\n scopes,\n selected,\n owned,\n assets,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"helpers.cjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAYA,uDAAgD;AAChD,2CAA6D;AAO7D,qDAKqB;AAErB,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAU,EAAE,SAAS,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,SAAS,UAAU,CAKjB,MAAiE,EACjE,OAA6C;IAE7C,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,CAAC,MAAM,EAA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AS,EAAE,OAAO,CAAC,EAAE;KACtB,CAAC;AACJ,CAAC;AARD,kDAQC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,EAAiB,EACjB,MAA8C;IAE9C,MAAM,KAAK,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IAEzB,IAAA,cAAM,EAAC,KAAK,EAAE,kBAAkB,EAAE,mCAAmC,CAAC,CAAC;IAEvE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IAE/B,OAAO;QACL,KAAK,EAAE,EAAE;QACT,MAAM;QACN,IAAI;KACL,CAAC;AACJ,CAAC;AAfD,8CAeC;AAED;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,IAAA,wCAA4B,GAAE,CAAC;AAqE3D;;;;;;;;;;;;;;GAcG;AACH,SAAgB,cAAc,CAAC,EAC7B,OAAO,EACP,MAAM,GAAG,EAAE,EACX,QAAQ,GAAG,KAAK,EAChB,KAAK,GAAG,KAAK,EACb,EAAE,GAAG,mBAAmB,EAAE,EAC1B,MAAM,GAAG,IAAA,+BAAmB,EAAC,MAAM,CAAC,GACd;IACtB,OAAO;QACL,OAAO;QACP,EAAE;QACF,MAAM;QACN,QAAQ;QACR,KAAK;QACL,MAAM;KACP,CAAC;AACJ,CAAC;AAhBD,wCAgBC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport type {\n AccountSelectorState,\n AssetSelectorState,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type {\n InstallSnapOptions,\n SimulationAccount,\n SimulationAsset,\n Snap,\n} from '@metamask/snaps-simulation';\nimport { logInfo } from '@metamask/snaps-utils';\nimport { assert, createModuleLogger } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipAssetType,\n CaipChainId,\n} from '@metamask/utils';\n\nimport {\n rootLogger,\n getEnvironment,\n getPseudoRandomUuidGenerator,\n getScopesFromAssets,\n} from './internals';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * Get the options for {@link installSnap}.\n *\n * @param snapId - The ID of the Snap, or the options.\n * @param options - The options, if any.\n * @returns The options.\n */\nfunction getOptions<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId | Partial<InstallSnapOptions<Service>> | undefined,\n options: Partial<InstallSnapOptions<Service>>,\n): [SnapId | undefined, Partial<InstallSnapOptions<Service>>] {\n if (typeof snapId === 'object') {\n return [undefined, snapId];\n }\n\n return [snapId, options];\n}\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap(): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(options: Partial<InstallSnapOptions<Service>>): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId,\n options?: Partial<InstallSnapOptions<Service>>,\n): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId?: SnapId | Partial<InstallSnapOptions<Service>>,\n options: Partial<InstallSnapOptions<Service>> = {},\n): Promise<Snap> {\n const resolvedOptions = getOptions(snapId, options);\n\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n /* eslint-disable @typescript-eslint/unbound-method */\n const {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n mockJsonRpcOnce,\n close,\n } = await getEnvironment().installSnap(...resolvedOptions);\n /* eslint-enable @typescript-eslint/unbound-method */\n\n return {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n mockJsonRpcOnce,\n close: async () => {\n log('Closing execution service.');\n logInfo(\n 'Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.',\n );\n\n await close();\n },\n };\n}\n\n/**\n * Get the state of an AccountSelector based on a {@link SimulationAccount}.\n *\n * @param account - The {@link SimulationAccount} to get the state from.\n * @returns The state of the AccountSelector.\n */\nexport function getStateFromAccount(\n account: SimulationAccount,\n): AccountSelectorState {\n const { address, scopes } = account;\n return {\n addresses: scopes.map((scope) => `${scope}:${address}`) as CaipAccountId[],\n accountId: account.id,\n };\n}\n\n/**\n * Get the state of an AssetSelector based on a {@link SimulationAsset}.\n *\n * @param id - The Asset id as a CAIP-19 asset type.\n * @param assets - The {@link SimulationAsset} to get the state from.\n * @returns The state of the AssetSelector.\n */\nexport function getStateFromAsset(\n id: CaipAssetType,\n assets: Record<CaipAssetType, SimulationAsset>,\n): AssetSelectorState {\n const asset = assets[id];\n\n assert(asset, `Asset with ID \"${id}\" not found in simulation assets.`);\n\n const { symbol, name } = asset;\n\n return {\n asset: id,\n symbol,\n name,\n };\n}\n\n/**\n * Generate a pseudo-random UUID.\n *\n * @returns A pseudo-random UUID string.\n */\nconst getPseudoRandomUuid = getPseudoRandomUuidGenerator();\n\n/**\n * The base options for the {@link getMockAccount} function.\n */\nexport type BaseMockAccountOptions = {\n /**\n * The address of the account.\n */\n address: string;\n\n /**\n * The ID of the account. If not provided, a pseudo-random UUID will be\n * generated.\n */\n id?: string;\n\n /**\n * Whether the account is selected by default.\n */\n selected?: boolean;\n\n /**\n * Whether the account is owned by the snap.\n */\n owned?: boolean;\n};\n\n/**\n * Options for creating a mock account with assets or scopes. If `scopes` are\n * not provided, they will be derived from the `assets`.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithAssets = BaseMockAccountOptions & {\n /**\n * The assets associated with the account. These should be in CAIP format.\n */\n assets: CaipAssetType[];\n\n /**\n * The scopes associated with the account. If not provided, they will be\n * derived from the `assets`.\n */\n scopes?: CaipChainId[];\n};\n\n/**\n * Options for creating a mock account with scopes, and optionally assets.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithScopes = BaseMockAccountOptions & {\n /**\n * The scopes associated with the account. These should be in CAIP format.\n */\n scopes: CaipChainId[];\n\n /**\n * The assets associated with the account. If not provided, it will default\n * to an empty array.\n */\n assets?: CaipAssetType[];\n};\n\nexport type GetMockAccountOptions =\n | MockAccountOptionsWithAssets\n | MockAccountOptionsWithScopes;\n\n/**\n * Get a mock account object for testing purposes.\n *\n * @param options - The options for creating the mock account.\n * @param options.address - The address of the account.\n * @param options.scopes - The scopes associated with the account, in CAIP\n * format. If not provided, they will be derived from the `assets`.\n * @param options.assets - The assets associated with the account, in CAIP\n * format. If not provided, it will default to an empty array.\n * @param options.selected - Whether the account is selected by default.\n * @param options.owned - Whether the account is owned by the snap.\n * @param options.id - The ID of the account. If not provided, a pseudo-random\n * UUID will be generated.\n * @returns A mock account object with the specified properties.\n */\nexport function getMockAccount({\n address,\n assets = [],\n selected = false,\n owned = false,\n id = getPseudoRandomUuid(),\n scopes = getScopesFromAssets(assets),\n}: GetMockAccountOptions): SimulationAccount {\n return {\n address,\n id,\n scopes,\n selected,\n owned,\n assets,\n };\n}\n"]}
|
package/dist/helpers.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.cts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,oCAAoC;AAC5E,OAAO,KAAK,EACV,oBAAoB,EACpB,kBAAkB,EAClB,MAAM,EACP,4BAA4B;AAC7B,OAAO,KAAK,EACV,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,mCAAmC;AAGpC,OAAO,KAAK,EAEV,aAAa,EACb,WAAW,EACZ,wBAAwB;AAiCzB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,WAAW,CAC/B,OAAO,SAAS,KACd,GAAG,IAAI,EAAE,GAAG,EAAE,KACX,YAAY,CAAC,OAAO,wBAAwB,CAAC,EAClD,OAAO,EAAE,OAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAsB,WAAW,CAC/B,OAAO,SAAS,KACd,GAAG,IAAI,EAAE,GAAG,EAAE,KACX,YAAY,CAAC,OAAO,wBAAwB,CAAC,EAElD,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,OAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,GAC7C,OAAO,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BAAqB,GAC7B,4BAA4B,GAC5B,4BAA4B,CAAC;AAEjC;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,EAC7B,OAAO,EACP,MAAW,EACX,QAAgB,EAChB,KAAa,EACb,EAA0B,EAC1B,MAAoC,GACrC,EAAE,qBAAqB,GAAG,iBAAiB,CAS3C"}
|
package/dist/helpers.mjs
CHANGED
|
@@ -52,7 +52,7 @@ export async function installSnap(snapId, options = {}) {
|
|
|
52
52
|
// TODO: Either fix this lint violation or explain why it's necessary to
|
|
53
53
|
// ignore.
|
|
54
54
|
/* eslint-disable @typescript-eslint/unbound-method */
|
|
55
|
-
const { request, onTransaction, sendTransaction, onSignature, onCronjob, runCronjob, onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, onInstall, onUpdate, onStart, onNameLookup, onProtocolRequest, onClientRequest, mockJsonRpc, close, } = await getEnvironment().installSnap(...resolvedOptions);
|
|
55
|
+
const { request, onTransaction, sendTransaction, onSignature, onCronjob, runCronjob, onBackgroundEvent, onHomePage, onSettingsPage, onKeyringRequest, onInstall, onUpdate, onStart, onNameLookup, onProtocolRequest, onClientRequest, mockJsonRpc, mockJsonRpcOnce, close, } = await getEnvironment().installSnap(...resolvedOptions);
|
|
56
56
|
/* eslint-enable @typescript-eslint/unbound-method */
|
|
57
57
|
return {
|
|
58
58
|
request,
|
|
@@ -72,6 +72,7 @@ export async function installSnap(snapId, options = {}) {
|
|
|
72
72
|
onProtocolRequest,
|
|
73
73
|
onClientRequest,
|
|
74
74
|
mockJsonRpc,
|
|
75
|
+
mockJsonRpcOnce,
|
|
75
76
|
close: async () => {
|
|
76
77
|
log('Closing execution service.');
|
|
77
78
|
logInfo('Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.');
|
package/dist/helpers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.mjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AA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,KAAK,IAAI,EAAE;YAChB,GAAG,CAAC,4BAA4B,CAAC,CAAC;YAClC,OAAO,CACL,uIAAuI,CACxI,CAAC;YAEF,MAAM,KAAK,EAAE,CAAC;QAChB,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CACjC,OAA0B;IAE1B,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACpC,OAAO;QACL,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,OAAO,EAAE,CAAoB;QAC1E,SAAS,EAAE,OAAO,CAAC,EAAE;KACtB,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,EAAiB,EACjB,MAA8C;IAE9C,MAAM,KAAK,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IAEzB,MAAM,CAAC,KAAK,EAAE,kBAAkB,EAAE,mCAAmC,CAAC,CAAC;IAEvE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IAE/B,OAAO;QACL,KAAK,EAAE,EAAE;QACT,MAAM;QACN,IAAI;KACL,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,4BAA4B,EAAE,CAAC;AAqE3D;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,cAAc,CAAC,EAC7B,OAAO,EACP,MAAM,GAAG,EAAE,EACX,QAAQ,GAAG,KAAK,EAChB,KAAK,GAAG,KAAK,EACb,EAAE,GAAG,mBAAmB,EAAE,EAC1B,MAAM,GAAG,mBAAmB,CAAC,MAAM,CAAC,GACd;IACtB,OAAO;QACL,OAAO;QACP,EAAE;QACF,MAAM;QACN,QAAQ;QACR,KAAK;QACL,MAAM;KACP,CAAC;AACJ,CAAC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport type {\n AccountSelectorState,\n AssetSelectorState,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type {\n InstallSnapOptions,\n SimulationAccount,\n SimulationAsset,\n Snap,\n} from '@metamask/snaps-simulation';\nimport { logInfo } from '@metamask/snaps-utils';\nimport { assert, createModuleLogger } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipAssetType,\n CaipChainId,\n} from '@metamask/utils';\n\nimport {\n rootLogger,\n getEnvironment,\n getPseudoRandomUuidGenerator,\n getScopesFromAssets,\n} from './internals';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * Get the options for {@link installSnap}.\n *\n * @param snapId - The ID of the Snap, or the options.\n * @param options - The options, if any.\n * @returns The options.\n */\nfunction getOptions<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId | Partial<InstallSnapOptions<Service>> | undefined,\n options: Partial<InstallSnapOptions<Service>>,\n): [SnapId | undefined, Partial<InstallSnapOptions<Service>>] {\n if (typeof snapId === 'object') {\n return [undefined, snapId];\n }\n\n return [snapId, options];\n}\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap(): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(options: Partial<InstallSnapOptions<Service>>): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId,\n options?: Partial<InstallSnapOptions<Service>>,\n): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId?: SnapId | Partial<InstallSnapOptions<Service>>,\n options: Partial<InstallSnapOptions<Service>> = {},\n): Promise<Snap> {\n const resolvedOptions = getOptions(snapId, options);\n\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n /* eslint-disable @typescript-eslint/unbound-method */\n const {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n close,\n } = await getEnvironment().installSnap(...resolvedOptions);\n /* eslint-enable @typescript-eslint/unbound-method */\n\n return {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n close: async () => {\n log('Closing execution service.');\n logInfo(\n 'Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.',\n );\n\n await close();\n },\n };\n}\n\n/**\n * Get the state of an AccountSelector based on a {@link SimulationAccount}.\n *\n * @param account - The {@link SimulationAccount} to get the state from.\n * @returns The state of the AccountSelector.\n */\nexport function getStateFromAccount(\n account: SimulationAccount,\n): AccountSelectorState {\n const { address, scopes } = account;\n return {\n addresses: scopes.map((scope) => `${scope}:${address}`) as CaipAccountId[],\n accountId: account.id,\n };\n}\n\n/**\n * Get the state of an AssetSelector based on a {@link SimulationAsset}.\n *\n * @param id - The Asset id as a CAIP-19 asset type.\n * @param assets - The {@link SimulationAsset} to get the state from.\n * @returns The state of the AssetSelector.\n */\nexport function getStateFromAsset(\n id: CaipAssetType,\n assets: Record<CaipAssetType, SimulationAsset>,\n): AssetSelectorState {\n const asset = assets[id];\n\n assert(asset, `Asset with ID \"${id}\" not found in simulation assets.`);\n\n const { symbol, name } = asset;\n\n return {\n asset: id,\n symbol,\n name,\n };\n}\n\n/**\n * Generate a pseudo-random UUID.\n *\n * @returns A pseudo-random UUID string.\n */\nconst getPseudoRandomUuid = getPseudoRandomUuidGenerator();\n\n/**\n * The base options for the {@link getMockAccount} function.\n */\nexport type BaseMockAccountOptions = {\n /**\n * The address of the account.\n */\n address: string;\n\n /**\n * The ID of the account. If not provided, a pseudo-random UUID will be\n * generated.\n */\n id?: string;\n\n /**\n * Whether the account is selected by default.\n */\n selected?: boolean;\n\n /**\n * Whether the account is owned by the snap.\n */\n owned?: boolean;\n};\n\n/**\n * Options for creating a mock account with assets or scopes. If `scopes` are\n * not provided, they will be derived from the `assets`.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithAssets = BaseMockAccountOptions & {\n /**\n * The assets associated with the account. These should be in CAIP format.\n */\n assets: CaipAssetType[];\n\n /**\n * The scopes associated with the account. If not provided, they will be\n * derived from the `assets`.\n */\n scopes?: CaipChainId[];\n};\n\n/**\n * Options for creating a mock account with scopes, and optionally assets.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithScopes = BaseMockAccountOptions & {\n /**\n * The scopes associated with the account. These should be in CAIP format.\n */\n scopes: CaipChainId[];\n\n /**\n * The assets associated with the account. If not provided, it will default\n * to an empty array.\n */\n assets?: CaipAssetType[];\n};\n\nexport type GetMockAccountOptions =\n | MockAccountOptionsWithAssets\n | MockAccountOptionsWithScopes;\n\n/**\n * Get a mock account object for testing purposes.\n *\n * @param options - The options for creating the mock account.\n * @param options.address - The address of the account.\n * @param options.scopes - The scopes associated with the account, in CAIP\n * format. If not provided, they will be derived from the `assets`.\n * @param options.assets - The assets associated with the account, in CAIP\n * format. If not provided, it will default to an empty array.\n * @param options.selected - Whether the account is selected by default.\n * @param options.owned - Whether the account is owned by the snap.\n * @param options.id - The ID of the account. If not provided, a pseudo-random\n * UUID will be generated.\n * @returns A mock account object with the specified properties.\n */\nexport function getMockAccount({\n address,\n assets = [],\n selected = false,\n owned = false,\n id = getPseudoRandomUuid(),\n scopes = getScopesFromAssets(assets),\n}: GetMockAccountOptions): SimulationAccount {\n return {\n address,\n id,\n scopes,\n selected,\n owned,\n assets,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"helpers.mjs","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,OAAO,EAAE,8BAA8B;AAChD,OAAO,EAAE,MAAM,EAAE,kBAAkB,EAAE,wBAAwB;AAO7D,OAAO,EACL,UAAU,EACV,cAAc,EACd,4BAA4B,EAC5B,mBAAmB,EACpB,8BAAoB;AAErB,MAAM,GAAG,GAAG,kBAAkB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,SAAS,UAAU,CAKjB,MAAiE,EACjE,OAA6C;IAE7C,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC3B,CAAC;AAqGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAK/B,MAAsD,EACtD,UAAgD,EAAE;IAElD,M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AAC;IAEzB,MAAM,CAAC,KAAK,EAAE,kBAAkB,EAAE,mCAAmC,CAAC,CAAC;IAEvE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IAE/B,OAAO;QACL,KAAK,EAAE,EAAE;QACT,MAAM;QACN,IAAI;KACL,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,4BAA4B,EAAE,CAAC;AAqE3D;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,cAAc,CAAC,EAC7B,OAAO,EACP,MAAM,GAAG,EAAE,EACX,QAAQ,GAAG,KAAK,EAChB,KAAK,GAAG,KAAK,EACb,EAAE,GAAG,mBAAmB,EAAE,EAC1B,MAAM,GAAG,mBAAmB,CAAC,MAAM,CAAC,GACd;IACtB,OAAO;QACL,OAAO;QACP,EAAE;QACF,MAAM;QACN,QAAQ;QACR,KAAK;QACL,MAAM;KACP,CAAC;AACJ,CAAC","sourcesContent":["import type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport type {\n AccountSelectorState,\n AssetSelectorState,\n SnapId,\n} from '@metamask/snaps-sdk';\nimport type {\n InstallSnapOptions,\n SimulationAccount,\n SimulationAsset,\n Snap,\n} from '@metamask/snaps-simulation';\nimport { logInfo } from '@metamask/snaps-utils';\nimport { assert, createModuleLogger } from '@metamask/utils';\nimport type {\n CaipAccountId,\n CaipAssetType,\n CaipChainId,\n} from '@metamask/utils';\n\nimport {\n rootLogger,\n getEnvironment,\n getPseudoRandomUuidGenerator,\n getScopesFromAssets,\n} from './internals';\n\nconst log = createModuleLogger(rootLogger, 'helpers');\n\n/**\n * Get the options for {@link installSnap}.\n *\n * @param snapId - The ID of the Snap, or the options.\n * @param options - The options, if any.\n * @returns The options.\n */\nfunction getOptions<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId | Partial<InstallSnapOptions<Service>> | undefined,\n options: Partial<InstallSnapOptions<Service>>,\n): [SnapId | undefined, Partial<InstallSnapOptions<Service>>] {\n if (typeof snapId === 'object') {\n return [undefined, snapId];\n }\n\n return [snapId, options];\n}\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap(): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(options: Partial<InstallSnapOptions<Service>>): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId,\n options?: Partial<InstallSnapOptions<Service>>,\n): Promise<Snap>;\n\n/**\n * Load a snap into the environment. This is the main entry point for testing\n * snaps: It returns a {@link Snap} object that can be used to interact with the\n * snap.\n *\n * @example\n * import { installSnap } from '@metamask/snaps-jest';\n *\n * describe('My Snap', () => {\n * it('should do something', async () => {\n * const { request } = await installSnap('local:my-snap');\n * const response = await request({\n * method: 'foo',\n * params: ['bar'],\n * });\n * expect(response).toRespondWith('bar');\n * });\n * });\n * @param snapId - The ID of the snap, including the prefix (`local:`). Defaults\n * to the URL of the built-in server, if it is running. This supports both\n * local snap IDs and NPM snap IDs.\n * @param options - The options to use.\n * @param options.executionService - The execution service to use. Defaults to\n * {@link NodeThreadExecutionService}. You do not need to provide this unless\n * you are testing a custom execution service.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The snap.\n * @throws If the built-in server is not running, and no snap ID is provided.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId?: SnapId | Partial<InstallSnapOptions<Service>>,\n options: Partial<InstallSnapOptions<Service>> = {},\n): Promise<Snap> {\n const resolvedOptions = getOptions(snapId, options);\n\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n /* eslint-disable @typescript-eslint/unbound-method */\n const {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n mockJsonRpcOnce,\n close,\n } = await getEnvironment().installSnap(...resolvedOptions);\n /* eslint-enable @typescript-eslint/unbound-method */\n\n return {\n request,\n onTransaction,\n sendTransaction,\n onSignature,\n onCronjob,\n runCronjob,\n onBackgroundEvent,\n onHomePage,\n onSettingsPage,\n onKeyringRequest,\n onInstall,\n onUpdate,\n onStart,\n onNameLookup,\n onProtocolRequest,\n onClientRequest,\n mockJsonRpc,\n mockJsonRpcOnce,\n close: async () => {\n log('Closing execution service.');\n logInfo(\n 'Calling `snap.close()` is deprecated, and will be removed in a future release. Snaps are now automatically closed when the test ends.',\n );\n\n await close();\n },\n };\n}\n\n/**\n * Get the state of an AccountSelector based on a {@link SimulationAccount}.\n *\n * @param account - The {@link SimulationAccount} to get the state from.\n * @returns The state of the AccountSelector.\n */\nexport function getStateFromAccount(\n account: SimulationAccount,\n): AccountSelectorState {\n const { address, scopes } = account;\n return {\n addresses: scopes.map((scope) => `${scope}:${address}`) as CaipAccountId[],\n accountId: account.id,\n };\n}\n\n/**\n * Get the state of an AssetSelector based on a {@link SimulationAsset}.\n *\n * @param id - The Asset id as a CAIP-19 asset type.\n * @param assets - The {@link SimulationAsset} to get the state from.\n * @returns The state of the AssetSelector.\n */\nexport function getStateFromAsset(\n id: CaipAssetType,\n assets: Record<CaipAssetType, SimulationAsset>,\n): AssetSelectorState {\n const asset = assets[id];\n\n assert(asset, `Asset with ID \"${id}\" not found in simulation assets.`);\n\n const { symbol, name } = asset;\n\n return {\n asset: id,\n symbol,\n name,\n };\n}\n\n/**\n * Generate a pseudo-random UUID.\n *\n * @returns A pseudo-random UUID string.\n */\nconst getPseudoRandomUuid = getPseudoRandomUuidGenerator();\n\n/**\n * The base options for the {@link getMockAccount} function.\n */\nexport type BaseMockAccountOptions = {\n /**\n * The address of the account.\n */\n address: string;\n\n /**\n * The ID of the account. If not provided, a pseudo-random UUID will be\n * generated.\n */\n id?: string;\n\n /**\n * Whether the account is selected by default.\n */\n selected?: boolean;\n\n /**\n * Whether the account is owned by the snap.\n */\n owned?: boolean;\n};\n\n/**\n * Options for creating a mock account with assets or scopes. If `scopes` are\n * not provided, they will be derived from the `assets`.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithAssets = BaseMockAccountOptions & {\n /**\n * The assets associated with the account. These should be in CAIP format.\n */\n assets: CaipAssetType[];\n\n /**\n * The scopes associated with the account. If not provided, they will be\n * derived from the `assets`.\n */\n scopes?: CaipChainId[];\n};\n\n/**\n * Options for creating a mock account with scopes, and optionally assets.\n *\n * @see BaseMockAccountOptions\n */\nexport type MockAccountOptionsWithScopes = BaseMockAccountOptions & {\n /**\n * The scopes associated with the account. These should be in CAIP format.\n */\n scopes: CaipChainId[];\n\n /**\n * The assets associated with the account. If not provided, it will default\n * to an empty array.\n */\n assets?: CaipAssetType[];\n};\n\nexport type GetMockAccountOptions =\n | MockAccountOptionsWithAssets\n | MockAccountOptionsWithScopes;\n\n/**\n * Get a mock account object for testing purposes.\n *\n * @param options - The options for creating the mock account.\n * @param options.address - The address of the account.\n * @param options.scopes - The scopes associated with the account, in CAIP\n * format. If not provided, they will be derived from the `assets`.\n * @param options.assets - The assets associated with the account, in CAIP\n * format. If not provided, it will default to an empty array.\n * @param options.selected - Whether the account is selected by default.\n * @param options.owned - Whether the account is owned by the snap.\n * @param options.id - The ID of the account. If not provided, a pseudo-random\n * UUID will be generated.\n * @returns A mock account object with the specified properties.\n */\nexport function getMockAccount({\n address,\n assets = [],\n selected = false,\n owned = false,\n id = getPseudoRandomUuid(),\n scopes = getScopesFromAssets(assets),\n}: GetMockAccountOptions): SimulationAccount {\n return {\n address,\n id,\n scopes,\n selected,\n owned,\n assets,\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-jest",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.5.0",
|
|
4
4
|
"description": "A Jest preset for end-to-end testing MetaMask Snaps, including a Jest environment, and a set of Jest matchers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
"@jest/environment": "^29.5.0",
|
|
61
61
|
"@jest/expect": "^29.5.0",
|
|
62
62
|
"@jest/globals": "^29.5.0",
|
|
63
|
-
"@metamask/snaps-controllers": "^
|
|
64
|
-
"@metamask/snaps-sdk": "^
|
|
65
|
-
"@metamask/snaps-simulation": "^3.
|
|
63
|
+
"@metamask/snaps-controllers": "^15.0.2",
|
|
64
|
+
"@metamask/snaps-sdk": "^10.0.0",
|
|
65
|
+
"@metamask/snaps-simulation": "^3.5.0",
|
|
66
66
|
"@metamask/superstruct": "^3.2.1",
|
|
67
|
-
"@metamask/utils": "^11.
|
|
67
|
+
"@metamask/utils": "^11.8.1",
|
|
68
68
|
"express": "^5.1.0",
|
|
69
69
|
"jest-environment-node": "^29.5.0",
|
|
70
70
|
"jest-matcher-utils": "^29.5.0",
|
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@jest/types": "^29.6.3",
|
|
75
|
-
"@lavamoat/allow-scripts": "^3.
|
|
75
|
+
"@lavamoat/allow-scripts": "^3.4.0",
|
|
76
76
|
"@metamask/auto-changelog": "^5.0.2",
|
|
77
|
-
"@metamask/snaps-utils": "^11.
|
|
77
|
+
"@metamask/snaps-utils": "^11.6.0",
|
|
78
78
|
"@swc/core": "1.11.31",
|
|
79
79
|
"@swc/jest": "^0.2.38",
|
|
80
80
|
"@ts-bridge/cli": "^0.6.1",
|