Skip to content

Commit 8854c9f

Browse files
feat: Support onClientRequest in snaps-jest (#3445)
Add support for `onClientRequest` in `snaps-jest` to ease testing of internal Snap functionality. Closes #3401
1 parent 8637cda commit 8854c9f

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

packages/snaps-jest/src/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export async function installSnap<
189189
onUpdate,
190190
onNameLookup,
191191
onProtocolRequest,
192+
onClientRequest,
192193
mockJsonRpc,
193194
close,
194195
} = await getEnvironment().installSnap(...resolvedOptions);
@@ -209,6 +210,7 @@ export async function installSnap<
209210
onUpdate,
210211
onNameLookup,
211212
onProtocolRequest,
213+
onClientRequest,
212214
mockJsonRpc,
213215
close: async () => {
214216
log('Closing execution service.');

packages/snaps-simulation/src/helpers.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,34 @@ describe('helpers', () => {
761761
});
762762
});
763763

764+
describe('onClientRequest', () => {
765+
it('sends a onClientRequest request and returns the result', async () => {
766+
jest.spyOn(console, 'log').mockImplementation();
767+
768+
const { snapId, close: closeServer } = await getMockServer({
769+
sourceCode: `
770+
module.exports.onClientRequest = async ({ request }) => {
771+
return request.method;
772+
};
773+
`,
774+
});
775+
776+
const { onClientRequest, close } = await installSnap(snapId);
777+
const response = await onClientRequest({ method: 'foo' });
778+
779+
expect(response).toStrictEqual(
780+
expect.objectContaining({
781+
response: {
782+
result: 'foo',
783+
},
784+
}),
785+
);
786+
787+
await close();
788+
await closeServer();
789+
});
790+
});
791+
764792
describe('mockJsonRpc', () => {
765793
it('mocks a JSON-RPC method', async () => {
766794
jest.spyOn(console, 'log').mockImplementation();

packages/snaps-simulation/src/helpers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ export type SnapHelpers = {
180180
request: RequestOptions,
181181
): Promise<SnapResponseWithoutInterface>;
182182

183+
/**
184+
* Send a JSON-RPC client request to the Snap.
185+
*
186+
* @param request - The JSON-RPC request.
187+
* @returns The response promise, with extra {@link SnapRequestObject} fields.
188+
*/
189+
onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;
190+
183191
/**
184192
* Mock a JSON-RPC request. This will cause the snap to respond with the
185193
* specified response when a request with the specified method is sent.
@@ -491,6 +499,22 @@ export function getHelpers({
491499
return response;
492500
},
493501

502+
// This can't be async because it returns a `SnapRequest`.
503+
// eslint-disable-next-line @typescript-eslint/promise-function-async
504+
onClientRequest: (request) => {
505+
log('Sending client request.');
506+
507+
return handleRequest({
508+
snapId,
509+
store,
510+
executionService,
511+
controllerMessenger,
512+
runSaga,
513+
handler: HandlerType.OnClientRequest,
514+
request,
515+
});
516+
},
517+
494518
mockJsonRpc(mock: JsonRpcMockOptions) {
495519
log('Mocking JSON-RPC request %o.', mock);
496520

packages/snaps-simulation/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,14 @@ export type Snap = {
514514
request: RequestOptions,
515515
): Promise<SnapResponseWithoutInterface>;
516516

517+
/**
518+
* Send a JSON-RPC client request to the Snap.
519+
*
520+
* @param request - The JSON-RPC request.
521+
* @returns The response promise, with extra {@link SnapRequestObject} fields.
522+
*/
523+
onClientRequest(request: Omit<RequestOptions, 'origin'>): SnapRequest;
524+
517525
/**
518526
* Mock a JSON-RPC request. This will cause the snap to respond with the
519527
* specified response when a request with the specified method is sent.

0 commit comments

Comments
 (0)