Skip to content

Commit 0db0eba

Browse files
Add test
1 parent 9005464 commit 0db0eba

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

packages/snaps-controllers/src/services/AbstractExecutionService.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,39 @@ describe('AbstractExecutionService', () => {
164164
}),
165165
).rejects.toThrow(`${MOCK_SNAP_ID} failed to start.`);
166166
});
167+
168+
it('skips metamask_chainChanged JSON-RPC notifications', async () => {
169+
const { service } = createService(MockExecutionService);
170+
171+
await service.executeSnap({
172+
snapId: 'TestSnap',
173+
sourceCode: `
174+
module.exports.onRpcRequest = () => null;
175+
`,
176+
endowments: [],
177+
});
178+
179+
const { streams, worker } = service.getJobs().values().next().value;
180+
const postMessageSpy = jest.spyOn(worker, 'postMessage');
181+
182+
streams.rpc.write({
183+
name: 'metamask-provider',
184+
data: { method: 'metamask_chainChanged' },
185+
});
186+
187+
streams.rpc.write({
188+
name: 'metamask-provider',
189+
data: { id: 'foo', result: '1' },
190+
});
191+
192+
expect(postMessageSpy).toHaveBeenCalledTimes(1);
193+
expect(postMessageSpy).toHaveBeenCalledWith({
194+
data: {
195+
name: 'jsonRpc',
196+
data: { data: { id: 'foo', result: '1' }, name: 'metamask-provider' },
197+
},
198+
});
199+
200+
await service.terminateAllSnaps();
201+
});
167202
});

packages/snaps-execution-environments/src/common/endowments/network.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,12 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
256256
} finally {
257257
if (openFetchConnection !== undefined) {
258258
openConnections.delete(openFetchConnection);
259+
260+
await notify({
261+
method: 'OutboundResponse',
262+
params: { source: 'fetch' },
263+
});
259264
}
260-
await notify({
261-
method: 'OutboundResponse',
262-
params: { source: 'fetch' },
263-
});
264265
}
265266

266267
if (res.body !== null) {

0 commit comments

Comments
 (0)