11import { JsonRpcEngine } from '@metamask/json-rpc-engine' ;
2- import { Json , JsonRpcFailure , JsonRpcSuccess } from '@metamask/utils' ;
2+ import {
3+ Json ,
4+ JsonRpcFailure ,
5+ JsonRpcParams ,
6+ JsonRpcRequest ,
7+ JsonRpcSuccess ,
8+ } from '@metamask/utils' ;
39
410import { createRpcWarningMiddleware } from './createRpcWarningMiddleware' ;
511import messages from '../messages' ;
612
7- const affected = [
13+ type Scenario = {
14+ scenario : string ;
15+ method : string ;
16+ warning ?: string ;
17+ params ?: JsonRpcParams ;
18+ } ;
19+
20+ const affected : Scenario [ ] = [
821 {
922 scenario : 'eth_decrypt' ,
1023 method : 'eth_decrypt' ,
@@ -35,7 +48,7 @@ const affected = [
3548 } ,
3649] ;
3750
38- const unaffected = [
51+ const unaffected : Scenario [ ] = [
3952 {
4053 scenario : 'eth_chainId' ,
4154 method : 'eth_chainId' ,
@@ -51,67 +64,84 @@ const unaffected = [
5164] ;
5265
5366describe ( 'createRpcWarningMiddleware' , ( ) => {
54- describe . each ( affected ) ( '$scenario' , ( { method, params = { } , warning } ) => {
55- it ( 'should warn the first time the method is called' , async ( ) => {
56- const consoleWarnSpy = jest . spyOn ( globalThis . console , 'warn' ) ;
57- const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
58- const engine = new JsonRpcEngine ( ) ;
59- engine . push ( middleware ) ;
60-
61- await engine . handle ( { jsonrpc : '2.0' , id : 1 , method, params } ) ;
62-
63- expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( warning ) ;
64- expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 ) ;
65- } ) ;
66-
67- it ( 'should not warn the second time the method is called' , async ( ) => {
68- const consoleWarnSpy = jest . spyOn ( globalThis . console , 'warn' ) ;
69- const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
70- const engine = new JsonRpcEngine ( ) ;
71- engine . push ( middleware ) ;
72-
73- await engine . handle ( { jsonrpc : '2.0' , id : 1 , method, params } ) ;
74- await engine . handle ( { jsonrpc : '2.0' , id : 1 , method, params } ) ;
75-
76- expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( warning ) ;
77- expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 ) ;
78- } ) ;
79-
80- it ( 'should allow the method to succeed' , async ( ) => {
81- const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
82- const engine = new JsonRpcEngine ( ) ;
83- engine . push ( middleware ) ;
84- engine . push ( ( _req , res , _next , end ) => {
85- res . result = 'success!' ;
86- end ( ) ;
67+ describe . each ( affected ) (
68+ '$scenario' ,
69+ ( { method, params = { } , warning } : Scenario ) => {
70+ it ( 'should warn the first time the method is called' , async ( ) => {
71+ const consoleWarnSpy = jest . spyOn ( globalThis . console , 'warn' ) ;
72+ const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
73+ const engine = new JsonRpcEngine ( ) ;
74+ engine . push ( middleware ) ;
75+
76+ await engine . handle ( {
77+ jsonrpc : '2.0' ,
78+ id : 1 ,
79+ method,
80+ params,
81+ } as JsonRpcRequest ) ;
82+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( warning ) ;
83+ expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 ) ;
8784 } ) ;
8885
89- const response = ( await engine . handle ( {
90- jsonrpc : '2.0' ,
91- id : 1 ,
92- method,
93- } ) ) as JsonRpcSuccess < Json > ;
94-
95- expect ( response . result ) . toBe ( 'success!' ) ;
96- } ) ;
97-
98- it ( 'should allow the method to fail' , async ( ) => {
99- const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
100- const engine = new JsonRpcEngine ( ) ;
101- engine . push ( middleware ) ;
102- engine . push ( ( ) => {
103- throw new Error ( 'Failure!' ) ;
86+ it ( 'should not warn the second time the method is called' , async ( ) => {
87+ const consoleWarnSpy = jest . spyOn ( globalThis . console , 'warn' ) ;
88+ const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
89+ const engine = new JsonRpcEngine ( ) ;
90+ engine . push ( middleware ) ;
91+
92+ await engine . handle ( {
93+ jsonrpc : '2.0' ,
94+ id : 1 ,
95+ method,
96+ params,
97+ } as JsonRpcRequest ) ;
98+ await engine . handle ( {
99+ jsonrpc : '2.0' ,
100+ id : 1 ,
101+ method,
102+ params,
103+ } as JsonRpcRequest ) ;
104+
105+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( warning ) ;
106+ expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 ) ;
104107 } ) ;
105108
106- const result = ( await engine . handle ( {
107- jsonrpc : '2.0' ,
108- id : 1 ,
109- method,
110- } ) ) as JsonRpcFailure ;
109+ it ( 'should allow the method to succeed' , async ( ) => {
110+ const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
111+ const engine = new JsonRpcEngine ( ) ;
112+ engine . push ( middleware ) ;
113+ engine . push ( ( _req , res , _next , end ) => {
114+ res . result = 'success!' ;
115+ end ( ) ;
116+ } ) ;
117+
118+ const response = ( await engine . handle ( {
119+ jsonrpc : '2.0' ,
120+ id : 1 ,
121+ method,
122+ } ) ) as JsonRpcSuccess < Json > ;
123+
124+ expect ( response . result ) . toBe ( 'success!' ) ;
125+ } ) ;
111126
112- expect ( result . error . message ) . toBe ( 'Internal JSON-RPC error.' ) ;
113- } ) ;
114- } ) ;
127+ it ( 'should allow the method to fail' , async ( ) => {
128+ const middleware = createRpcWarningMiddleware ( globalThis . console ) ;
129+ const engine = new JsonRpcEngine ( ) ;
130+ engine . push ( middleware ) ;
131+ engine . push ( ( ) => {
132+ throw new Error ( 'Failure!' ) ;
133+ } ) ;
134+
135+ const result = ( await engine . handle ( {
136+ jsonrpc : '2.0' ,
137+ id : 1 ,
138+ method,
139+ } ) ) as JsonRpcFailure ;
140+
141+ expect ( result . error . message ) . toBe ( 'Internal JSON-RPC error.' ) ;
142+ } ) ;
143+ } ,
144+ ) ;
115145
116146 describe . each ( unaffected ) ( '$scenario' , ( { method, params = { } } ) => {
117147 it ( 'should not issue a warning' , async ( ) => {
@@ -120,7 +150,12 @@ describe('createRpcWarningMiddleware', () => {
120150 const engine = new JsonRpcEngine ( ) ;
121151 engine . push ( middleware ) ;
122152
123- await engine . handle ( { jsonrpc : '2.0' , id : 1 , method, params } ) ;
153+ await engine . handle ( {
154+ jsonrpc : '2.0' ,
155+ id : 1 ,
156+ method,
157+ params,
158+ } as JsonRpcRequest ) ;
124159
125160 expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( ) ;
126161 } ) ;
@@ -139,7 +174,7 @@ describe('createRpcWarningMiddleware', () => {
139174 id : 1 ,
140175 method,
141176 params,
142- } ) ) as JsonRpcSuccess < Json > ;
177+ } as JsonRpcRequest ) ) as JsonRpcSuccess < Json > ;
143178
144179 expect ( response . result ) . toBe ( 'success!' ) ;
145180 } ) ;
@@ -157,7 +192,7 @@ describe('createRpcWarningMiddleware', () => {
157192 id : 1 ,
158193 method,
159194 params,
160- } ) ) as JsonRpcFailure ;
195+ } as JsonRpcRequest ) ) as JsonRpcFailure ;
161196
162197 expect ( result . error . message ) . toBe ( 'Internal JSON-RPC error.' ) ;
163198 } ) ;
0 commit comments