Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions packages/snaps-jest/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,125 @@ describe('installSnap', () => {
await close();
await closeServer();
});

it('mocks a JSON-RPC implementation', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onRpcRequest = async () => {
return await ethereum.request({
method: 'foo',
});
};
`,
manifest: getSnapManifest({
initialPermissions: {
'endowment:ethereum-provider': {},
},
}),
});

const { request, close, mockJsonRpc } = await installSnap(snapId);
const { unmock } = mockJsonRpc(({ method }) => {
return `${method}_mocked`;
});

const response = await request({
method: 'foo',
});

expect(response).toStrictEqual(
expect.objectContaining({
response: {
result: 'foo_mocked',
},
}),
);

unmock();

const unmockedResponse = await request({
method: 'foo',
});

expect(unmockedResponse).toStrictEqual(
expect.objectContaining({
response: {
error: expect.objectContaining({
code: -32601,
message: 'The method "foo" does not exist / is not available.',
}),
},
}),
);

// `close` is deprecated because the Jest environment will automatically
// close the Snap when the test finishes. However, we still need to close
// the Snap in this test because it's run outside the Jest environment.
await close();
await closeServer();
});
});

describe('mockJsonRpcOnce', () => {
it('mocks a JSON-RPC method', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onRpcRequest = async () => {
return await ethereum.request({
method: 'foo',
});
};
`,
manifest: getSnapManifest({
initialPermissions: {
'endowment:ethereum-provider': {},
},
}),
});

const { request, close, mockJsonRpcOnce } = await installSnap(snapId);
mockJsonRpcOnce({
method: 'foo',
result: 'mock',
});

const response = await request({
method: 'foo',
});

expect(response).toStrictEqual(
expect.objectContaining({
response: {
result: 'mock',
},
}),
);

const unmockedResponse = await request({
method: 'foo',
});

expect(unmockedResponse).toStrictEqual(
expect.objectContaining({
response: {
error: expect.objectContaining({
code: -32601,
message: 'The method "foo" does not exist / is not available.',
}),
},
}),
);

// `close` is deprecated because the Jest environment will automatically
// close the Snap when the test finishes. However, we still need to close
// the Snap in this test because it's run outside the Jest environment.
await close();
await closeServer();
});
});
});

Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-jest/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export async function installSnap<
onProtocolRequest,
onClientRequest,
mockJsonRpc,
mockJsonRpcOnce,
close,
} = await getEnvironment().installSnap(...resolvedOptions);
/* eslint-enable @typescript-eslint/unbound-method */
Expand All @@ -233,6 +234,7 @@ export async function installSnap<
onProtocolRequest,
onClientRequest,
mockJsonRpc,
mockJsonRpcOnce,
close: async () => {
log('Closing execution service.');
logInfo(
Expand Down
200 changes: 200 additions & 0 deletions packages/snaps-simulation/src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -875,5 +875,205 @@ describe('helpers', () => {
await close();
await closeServer();
});

it('mocks a JSON-RPC implementation', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onRpcRequest = async () => {
return await ethereum.request({
method: 'foo',
});
};
`,
manifest: getSnapManifest({
initialPermissions: {
'endowment:ethereum-provider': {},
},
}),
});

const { request, close, mockJsonRpc } = await installSnap(snapId);
const { unmock } = mockJsonRpc(({ method }) => {
return `${method}_mocked`;
});

const response = await request({
method: 'foo',
});

expect(response).toStrictEqual(
expect.objectContaining({
response: {
result: 'foo_mocked',
},
}),
);

unmock();

const unmockedResponse = await request({
method: 'foo',
});

expect(unmockedResponse).toStrictEqual(
expect.objectContaining({
response: {
error: expect.objectContaining({
code: -32601,
message: 'The method "foo" does not exist / is not available.',
}),
},
}),
);

// `close` is deprecated because the Jest environment will automatically
// close the Snap when the test finishes. However, we still need to close
// the Snap in this test because it's run outside the Jest environment.
await close();
await closeServer();
});
});

describe('mockJsonRpcOnce', () => {
it('mocks a JSON-RPC method once', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onRpcRequest = async () => {
return await ethereum.request({
method: 'foo',
});
};
`,
manifest: getSnapManifest({
initialPermissions: {
'endowment:ethereum-provider': {},
},
}),
});

const { request, close, mockJsonRpcOnce } = await installSnap(snapId);
mockJsonRpcOnce({
method: 'foo_2',
result: 'invalid_mock',
});

mockJsonRpcOnce({
method: 'foo',
result: 'mock',
});

const response = await request({
method: 'foo',
});

expect(response).toStrictEqual(
expect.objectContaining({
response: {
result: 'mock',
},
}),
);

const unmockedResponse = await request({
method: 'foo',
});

expect(unmockedResponse).toStrictEqual(
expect.objectContaining({
response: {
error: expect.objectContaining({
code: -32601,
message: 'The method "foo" does not exist / is not available.',
}),
},
}),
);

// `close` is deprecated because the Jest environment will automatically
// close the Snap when the test finishes. However, we still need to close
// the Snap in this test because it's run outside the Jest environment.
await close();
await closeServer();
});

it('supports queueing JSON-RPC mocks', async () => {
jest.spyOn(console, 'log').mockImplementation();

const { snapId, close: closeServer } = await getMockServer({
sourceCode: `
module.exports.onRpcRequest = async () => {
return await ethereum.request({
method: 'foo',
});
};
`,
manifest: getSnapManifest({
initialPermissions: {
'endowment:ethereum-provider': {},
},
}),
});

const { request, close, mockJsonRpcOnce } = await installSnap(snapId);

mockJsonRpcOnce({
method: 'foo',
result: 'mock',
});

mockJsonRpcOnce({
method: 'foo',
result: 'mock2',
});

const response1 = await request({
method: 'foo',
});

expect(response1).toStrictEqual(
expect.objectContaining({
response: {
result: 'mock',
},
}),
);

const response2 = await request({
method: 'foo',
});

expect(response2).toStrictEqual(
expect.objectContaining({
response: {
result: 'mock2',
},
}),
);

const unmockedResponse = await request({
method: 'foo',
});

expect(unmockedResponse).toStrictEqual(
expect.objectContaining({
response: {
error: expect.objectContaining({
code: -32601,
message: 'The method "foo" does not exist / is not available.',
}),
},
}),
);

// `close` is deprecated because the Jest environment will automatically
// close the Snap when the test finishes. However, we still need to close
// the Snap in this test because it's run outside the Jest environment.
await close();
await closeServer();
});
});
});
Loading
Loading