diff --git a/packages/snaps-jest/src/helpers.ts b/packages/snaps-jest/src/helpers.ts index f357e30554..2300147be5 100644 --- a/packages/snaps-jest/src/helpers.ts +++ b/packages/snaps-jest/src/helpers.ts @@ -189,6 +189,7 @@ export async function installSnap< onUpdate, onNameLookup, onProtocolRequest, + onClientRequest, mockJsonRpc, close, } = await getEnvironment().installSnap(...resolvedOptions); @@ -209,6 +210,7 @@ export async function installSnap< onUpdate, onNameLookup, onProtocolRequest, + onClientRequest, mockJsonRpc, close: async () => { log('Closing execution service.'); diff --git a/packages/snaps-simulation/src/helpers.test.tsx b/packages/snaps-simulation/src/helpers.test.tsx index f461ada035..74dcb839ca 100644 --- a/packages/snaps-simulation/src/helpers.test.tsx +++ b/packages/snaps-simulation/src/helpers.test.tsx @@ -761,6 +761,34 @@ describe('helpers', () => { }); }); + describe('onClientRequest', () => { + it('sends a onClientRequest request and returns the result', async () => { + jest.spyOn(console, 'log').mockImplementation(); + + const { snapId, close: closeServer } = await getMockServer({ + sourceCode: ` + module.exports.onClientRequest = async ({ request }) => { + return request.method; + }; + `, + }); + + const { onClientRequest, close } = await installSnap(snapId); + const response = await onClientRequest({ method: 'foo' }); + + expect(response).toStrictEqual( + expect.objectContaining({ + response: { + result: 'foo', + }, + }), + ); + + await close(); + await closeServer(); + }); + }); + describe('mockJsonRpc', () => { it('mocks a JSON-RPC method', async () => { jest.spyOn(console, 'log').mockImplementation(); diff --git a/packages/snaps-simulation/src/helpers.ts b/packages/snaps-simulation/src/helpers.ts index f7911cb0f4..889e2ba3f8 100644 --- a/packages/snaps-simulation/src/helpers.ts +++ b/packages/snaps-simulation/src/helpers.ts @@ -180,6 +180,14 @@ export type SnapHelpers = { request: RequestOptions, ): Promise; + /** + * Send a JSON-RPC client request to the Snap. + * + * @param request - The JSON-RPC request. + * @returns The response promise, with extra {@link SnapRequestObject} fields. + */ + onClientRequest(request: Omit): SnapRequest; + /** * Mock a JSON-RPC request. This will cause the snap to respond with the * specified response when a request with the specified method is sent. @@ -491,6 +499,22 @@ export function getHelpers({ return response; }, + // This can't be async because it returns a `SnapRequest`. + // eslint-disable-next-line @typescript-eslint/promise-function-async + onClientRequest: (request) => { + log('Sending client request.'); + + return handleRequest({ + snapId, + store, + executionService, + controllerMessenger, + runSaga, + handler: HandlerType.OnClientRequest, + request, + }); + }, + mockJsonRpc(mock: JsonRpcMockOptions) { log('Mocking JSON-RPC request %o.', mock); diff --git a/packages/snaps-simulation/src/types.ts b/packages/snaps-simulation/src/types.ts index b4dfcf7379..37aa907730 100644 --- a/packages/snaps-simulation/src/types.ts +++ b/packages/snaps-simulation/src/types.ts @@ -514,6 +514,14 @@ export type Snap = { request: RequestOptions, ): Promise; + /** + * Send a JSON-RPC client request to the Snap. + * + * @param request - The JSON-RPC request. + * @returns The response promise, with extra {@link SnapRequestObject} fields. + */ + onClientRequest(request: Omit): SnapRequest; + /** * Mock a JSON-RPC request. This will cause the snap to respond with the * specified response when a request with the specified method is sent.