Skip to content

Commit f23a5ae

Browse files
authored
chore: Improve makeRequest test utility function (#7297)
## Explanation The `makeRequest` utility function is now easier to control the return type of using a type parameter, while still defaulting to the correct type by default most of the time. The previous version of this utility function frequently required a type cast to prevent the return type from being resolved to `never`. It was technically possible to specify the return type with a type parameter, but it was onerous because the exact input type would also be required as the first parameter. This was done to simplify a later PR (#7296) that enables a lint rule preventing unnecessary type assertions. The rule ended up breaking these tests because the cast impacted how the type was inferred in a way that the rule didn't predict. ## References Relates to #7296 ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Refactors the `makeRequest` test util to a simpler generic signature and updates tests to use explicit generics and local type aliases instead of casts. > > - **Tests/Utils**: > - **`makeRequest`**: Simplify to `<Request extends JsonRpcRequest = JsonRpcRequest>(request: Partial<Request>) => Request`; remove dual generics and `never`-prone casting. > - **Tests**: > - Replace casts with generic usage (e.g., `makeRequest<JsonRpcRequest>()`). > - Introduce local alias `NumericIdRequest` and use `makeRequest<NumericIdRequest>({...})` where needed. > - Minor assertion updates to align with new typing (no runtime behavior changes). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 554f7d8. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 7bb160f commit f23a5ae

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

packages/json-rpc-engine/src/v2/JsonRpcEngineV2.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('JsonRpcEngineV2', () => {
7272
// between these two cases:
7373
// - JsonRpcMiddleware<JsonRpcRequest> | JsonRpcMiddleware<JsonRpcNotification> (invalid)
7474
// - JsonRpcMiddleware<JsonRpcRequest> | JsonRpcMiddleware<JsonRpcCall> (valid)
75-
expect(await engine.handle(makeRequest() as JsonRpcRequest)).toBe('foo');
75+
expect(await engine.handle(makeRequest())).toBe('foo');
7676
});
7777
});
7878

@@ -811,19 +811,20 @@ describe('JsonRpcEngineV2', () => {
811811

812812
it('eagerly processes requests in parallel, i.e. without queueing them', async () => {
813813
const queue = makeArbitraryQueue(3);
814-
const middleware: JsonRpcMiddleware<
815-
JsonRpcRequest & { id: number }
816-
> = async ({ request }) => {
814+
type NumericIdRequest = JsonRpcRequest & { id: number };
815+
const middleware: JsonRpcMiddleware<NumericIdRequest> = async ({
816+
request,
817+
}) => {
817818
await queue.enqueue(request.id);
818819
return null;
819820
};
820821
const engine = JsonRpcEngineV2.create({
821822
middleware: [middleware],
822823
});
823824

824-
const p0 = engine.handle(makeRequest({ id: 0 }));
825-
const p1 = engine.handle(makeRequest({ id: 1 }));
826-
const p2 = engine.handle(makeRequest({ id: 2 }));
825+
const p0 = engine.handle(makeRequest<NumericIdRequest>({ id: 0 }));
826+
const p1 = engine.handle(makeRequest<NumericIdRequest>({ id: 1 }));
827+
const p2 = engine.handle(makeRequest<NumericIdRequest>({ id: 2 }));
827828

828829
await queue.filled();
829830

@@ -1210,7 +1211,7 @@ describe('JsonRpcEngineV2', () => {
12101211
);
12111212
await expect(
12121213
// @ts-expect-error - Invalid at runtime and should cause a type error
1213-
engine.handle(makeRequest() as JsonRpcRequest),
1214+
engine.handle(makeRequest<JsonRpcRequest>()),
12141215
).rejects.toThrow(
12151216
new JsonRpcEngineError(
12161217
`Nothing ended request: ${stringify(makeRequest())}`,

packages/json-rpc-engine/tests/utils.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import type { JsonRpcNotification } from '../src/v2/utils';
66

77
const jsonrpc = '2.0' as const;
88

9-
export const makeRequest = <
10-
Input extends Partial<JsonRpcRequest>,
11-
Output extends Input & JsonRpcRequest,
12-
>(
13-
request: Input = {} as Input,
9+
export const makeRequest = <Request extends JsonRpcRequest = JsonRpcRequest>(
10+
request: Partial<Request> = {},
1411
) =>
1512
({
1613
jsonrpc,
@@ -19,7 +16,7 @@ export const makeRequest = <
1916

2017
params: request.params === undefined ? [] : request.params,
2118
...request,
22-
}) as Output;
19+
}) as Request;
2320

2421
export const makeNotification = <Request extends Partial<JsonRpcRequest>>(
2522
params: Request = {} as Request,

0 commit comments

Comments
 (0)