@metamask/snaps-jest 0.35.1-flask.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +587 -0
- package/dist/cjs/environment.js +205 -0
- package/dist/cjs/environment.js.map +1 -0
- package/dist/cjs/helpers.js +67 -0
- package/dist/cjs/helpers.js.map +1 -0
- package/dist/cjs/index.js +42 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/internals/environment.js +19 -0
- package/dist/cjs/internals/environment.js.map +1 -0
- package/dist/cjs/internals/index.js +28 -0
- package/dist/cjs/internals/index.js.map +1 -0
- package/dist/cjs/internals/interface.js +103 -0
- package/dist/cjs/internals/interface.js.map +1 -0
- package/dist/cjs/internals/logger.js +14 -0
- package/dist/cjs/internals/logger.js.map +1 -0
- package/dist/cjs/internals/network.js +146 -0
- package/dist/cjs/internals/network.js.map +1 -0
- package/dist/cjs/internals/request.js +121 -0
- package/dist/cjs/internals/request.js.map +1 -0
- package/dist/cjs/internals/server.js +75 -0
- package/dist/cjs/internals/server.js.map +1 -0
- package/dist/cjs/internals/structs.js +124 -0
- package/dist/cjs/internals/structs.js.map +1 -0
- package/dist/cjs/internals/types.js +6 -0
- package/dist/cjs/internals/types.js.map +1 -0
- package/dist/cjs/internals/wait-for.js +63 -0
- package/dist/cjs/internals/wait-for.js.map +1 -0
- package/dist/cjs/matchers.js +117 -0
- package/dist/cjs/matchers.js.map +1 -0
- package/dist/cjs/options.js +29 -0
- package/dist/cjs/options.js.map +1 -0
- package/dist/cjs/setup.js +10 -0
- package/dist/cjs/setup.js.map +1 -0
- package/dist/cjs/types.js +13 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/environment.js +182 -0
- package/dist/esm/environment.js.map +1 -0
- package/dist/esm/helpers.js +82 -0
- package/dist/esm/helpers.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internals/environment.js +14 -0
- package/dist/esm/internals/environment.js.map +1 -0
- package/dist/esm/internals/index.js +12 -0
- package/dist/esm/internals/index.js.map +1 -0
- package/dist/esm/internals/interface.js +100 -0
- package/dist/esm/internals/interface.js.map +1 -0
- package/dist/esm/internals/logger.js +4 -0
- package/dist/esm/internals/logger.js.map +1 -0
- package/dist/esm/internals/network.js +141 -0
- package/dist/esm/internals/network.js.map +1 -0
- package/dist/esm/internals/request.js +120 -0
- package/dist/esm/internals/request.js.map +1 -0
- package/dist/esm/internals/server.js +68 -0
- package/dist/esm/internals/server.js.map +1 -0
- package/dist/esm/internals/structs.js +100 -0
- package/dist/esm/internals/structs.js.map +1 -0
- package/dist/esm/internals/types.js +3 -0
- package/dist/esm/internals/types.js.map +1 -0
- package/dist/esm/internals/wait-for.js +63 -0
- package/dist/esm/internals/wait-for.js.map +1 -0
- package/dist/esm/matchers.js +108 -0
- package/dist/esm/matchers.js.map +1 -0
- package/dist/esm/options.js +26 -0
- package/dist/esm/options.js.map +1 -0
- package/dist/esm/setup.js +6 -0
- package/dist/esm/setup.js.map +1 -0
- package/dist/esm/types.js +3 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/types/environment.d.ts +53 -0
- package/dist/types/helpers.d.ts +28 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/internals/environment.d.ts +7 -0
- package/dist/types/internals/index.d.ts +9 -0
- package/dist/types/internals/interface.d.ts +25 -0
- package/dist/types/internals/logger.d.ts +2 -0
- package/dist/types/internals/network.d.ts +87 -0
- package/dist/types/internals/request.d.ts +58 -0
- package/dist/types/internals/server.d.ts +14 -0
- package/dist/types/internals/structs.d.ts +164 -0
- package/dist/types/internals/types.d.ts +19 -0
- package/dist/types/internals/wait-for.d.ts +38 -0
- package/dist/types/matchers.d.ts +29 -0
- package/dist/types/options.d.ts +92 -0
- package/dist/types/setup.d.ts +1 -0
- package/dist/types/types.d.ts +241 -0
- package/jest-preset.js +18 -0
- package/package.json +120 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
export declare const TransactionOptionsStruct: import("superstruct").Struct<{
|
|
2
|
+
data: `0x${string}`;
|
|
3
|
+
from: `0x${string}`;
|
|
4
|
+
origin: string;
|
|
5
|
+
value: `0x${string}`;
|
|
6
|
+
chainId: string;
|
|
7
|
+
nonce: `0x${string}`;
|
|
8
|
+
maxPriorityFeePerGas: `0x${string}`;
|
|
9
|
+
maxFeePerGas: `0x${string}`;
|
|
10
|
+
to: `0x${string}`;
|
|
11
|
+
gasLimit: `0x${string}`;
|
|
12
|
+
}, {
|
|
13
|
+
/**
|
|
14
|
+
* The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.
|
|
15
|
+
*/
|
|
16
|
+
chainId: import("superstruct").Struct<string, null>;
|
|
17
|
+
/**
|
|
18
|
+
* The origin to send the transaction from. Defaults to `metamask.io`.
|
|
19
|
+
*/
|
|
20
|
+
origin: import("superstruct").Struct<string, null>;
|
|
21
|
+
/**
|
|
22
|
+
* The address to send the transaction from. Defaults to a randomly generated
|
|
23
|
+
* address.
|
|
24
|
+
*/
|
|
25
|
+
from: import("superstruct").Struct<`0x${string}`, null>;
|
|
26
|
+
/**
|
|
27
|
+
* The address to send the transaction to. Defaults to a randomly generated
|
|
28
|
+
* address.
|
|
29
|
+
*/
|
|
30
|
+
to: import("superstruct").Struct<`0x${string}`, null>;
|
|
31
|
+
/**
|
|
32
|
+
* The value to send with the transaction. The value may be specified as a
|
|
33
|
+
* `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.
|
|
34
|
+
*/
|
|
35
|
+
value: import("superstruct").Struct<`0x${string}`, null>;
|
|
36
|
+
/**
|
|
37
|
+
* The gas limit to use for the transaction. The gas limit may be specified
|
|
38
|
+
* as a `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `21_000`.
|
|
39
|
+
*/
|
|
40
|
+
gasLimit: import("superstruct").Struct<`0x${string}`, null>;
|
|
41
|
+
/**
|
|
42
|
+
* The max fee per gas (in Wei) to use for the transaction. The max fee per
|
|
43
|
+
* gas may be specified as a `number`, `bigint`, `string`, or `Uint8Array`.
|
|
44
|
+
* Defaults to `1`.
|
|
45
|
+
*/
|
|
46
|
+
maxFeePerGas: import("superstruct").Struct<`0x${string}`, null>;
|
|
47
|
+
/**
|
|
48
|
+
* The max priority fee per gas (in Wei) to use for the transaction. The max
|
|
49
|
+
* priority fee per gas may be specified as a `number`, `bigint`, `string`,
|
|
50
|
+
* or `Uint8Array`. Defaults to `1`.
|
|
51
|
+
*/
|
|
52
|
+
maxPriorityFeePerGas: import("superstruct").Struct<`0x${string}`, null>;
|
|
53
|
+
/**
|
|
54
|
+
* The nonce to use for the transaction. The nonce may be specified as a
|
|
55
|
+
* `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.
|
|
56
|
+
*/
|
|
57
|
+
nonce: import("superstruct").Struct<`0x${string}`, null>;
|
|
58
|
+
/**
|
|
59
|
+
* The data to send with the transaction. The data may be specified as a
|
|
60
|
+
* `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0x`.
|
|
61
|
+
*/
|
|
62
|
+
data: import("superstruct").Struct<`0x${string}`, null>;
|
|
63
|
+
}>;
|
|
64
|
+
export declare const SnapOptionsStruct: import("superstruct").Struct<{
|
|
65
|
+
timeout?: number | undefined;
|
|
66
|
+
}, {
|
|
67
|
+
/**
|
|
68
|
+
* The timeout in milliseconds to use for requests to the snap. Defaults to
|
|
69
|
+
* `1000`.
|
|
70
|
+
*/
|
|
71
|
+
timeout: import("superstruct").Struct<number | undefined, null>;
|
|
72
|
+
}>;
|
|
73
|
+
export declare const InterfaceStruct: import("superstruct").Struct<{
|
|
74
|
+
content?: import("@metamask/snaps-ui").Panel | {
|
|
75
|
+
value: string;
|
|
76
|
+
type: import("@metamask/snaps-ui").NodeType.Copyable;
|
|
77
|
+
} | {
|
|
78
|
+
type: import("@metamask/snaps-ui").NodeType.Divider;
|
|
79
|
+
} | {
|
|
80
|
+
value: string;
|
|
81
|
+
type: import("@metamask/snaps-ui").NodeType.Heading;
|
|
82
|
+
} | {
|
|
83
|
+
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
84
|
+
} | {
|
|
85
|
+
value: string;
|
|
86
|
+
type: import("@metamask/snaps-ui").NodeType.Text;
|
|
87
|
+
} | undefined;
|
|
88
|
+
}, {
|
|
89
|
+
content: import("superstruct").Struct<import("@metamask/snaps-ui").Panel | {
|
|
90
|
+
value: string;
|
|
91
|
+
type: import("@metamask/snaps-ui").NodeType.Copyable;
|
|
92
|
+
} | {
|
|
93
|
+
type: import("@metamask/snaps-ui").NodeType.Divider;
|
|
94
|
+
} | {
|
|
95
|
+
value: string;
|
|
96
|
+
type: import("@metamask/snaps-ui").NodeType.Heading;
|
|
97
|
+
} | {
|
|
98
|
+
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
99
|
+
} | {
|
|
100
|
+
value: string;
|
|
101
|
+
type: import("@metamask/snaps-ui").NodeType.Text;
|
|
102
|
+
} | undefined, null>;
|
|
103
|
+
}>;
|
|
104
|
+
export declare const SnapResponseStruct: import("superstruct").Struct<{
|
|
105
|
+
response: {
|
|
106
|
+
result: import("@metamask/utils").Json;
|
|
107
|
+
} | {
|
|
108
|
+
error: import("@metamask/utils").Json;
|
|
109
|
+
};
|
|
110
|
+
id: string;
|
|
111
|
+
notifications: {
|
|
112
|
+
message: string;
|
|
113
|
+
type: "native" | "inApp";
|
|
114
|
+
id: string;
|
|
115
|
+
}[];
|
|
116
|
+
content?: import("@metamask/snaps-ui").Panel | {
|
|
117
|
+
value: string;
|
|
118
|
+
type: import("@metamask/snaps-ui").NodeType.Copyable;
|
|
119
|
+
} | {
|
|
120
|
+
type: import("@metamask/snaps-ui").NodeType.Divider;
|
|
121
|
+
} | {
|
|
122
|
+
value: string;
|
|
123
|
+
type: import("@metamask/snaps-ui").NodeType.Heading;
|
|
124
|
+
} | {
|
|
125
|
+
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
126
|
+
} | {
|
|
127
|
+
value: string;
|
|
128
|
+
type: import("@metamask/snaps-ui").NodeType.Text;
|
|
129
|
+
} | undefined;
|
|
130
|
+
}, {
|
|
131
|
+
id: import("superstruct").Struct<string, null>;
|
|
132
|
+
response: import("superstruct").Struct<{
|
|
133
|
+
result: import("@metamask/utils").Json;
|
|
134
|
+
} | {
|
|
135
|
+
error: import("@metamask/utils").Json;
|
|
136
|
+
}, null>;
|
|
137
|
+
notifications: import("superstruct").Struct<{
|
|
138
|
+
message: string;
|
|
139
|
+
type: "native" | "inApp";
|
|
140
|
+
id: string;
|
|
141
|
+
}[], import("superstruct").Struct<{
|
|
142
|
+
message: string;
|
|
143
|
+
type: "native" | "inApp";
|
|
144
|
+
id: string;
|
|
145
|
+
}, {
|
|
146
|
+
id: import("superstruct").Struct<string, null>;
|
|
147
|
+
message: import("superstruct").Struct<string, null>;
|
|
148
|
+
type: import("superstruct").Struct<"native" | "inApp", null>;
|
|
149
|
+
}>>;
|
|
150
|
+
content: import("superstruct").Struct<import("@metamask/snaps-ui").Panel | {
|
|
151
|
+
value: string;
|
|
152
|
+
type: import("@metamask/snaps-ui").NodeType.Copyable;
|
|
153
|
+
} | {
|
|
154
|
+
type: import("@metamask/snaps-ui").NodeType.Divider;
|
|
155
|
+
} | {
|
|
156
|
+
value: string;
|
|
157
|
+
type: import("@metamask/snaps-ui").NodeType.Heading;
|
|
158
|
+
} | {
|
|
159
|
+
type: import("@metamask/snaps-ui").NodeType.Spinner;
|
|
160
|
+
} | {
|
|
161
|
+
value: string;
|
|
162
|
+
type: import("@metamask/snaps-ui").NodeType.Text;
|
|
163
|
+
} | undefined, null>;
|
|
164
|
+
}>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NotificationType } from '@metamask/rpc-methods';
|
|
2
|
+
import { ApplicationState, Dispatch } from '@metamask/snaps-simulator';
|
|
3
|
+
import { EnumToUnion } from '@metamask/snaps-utils';
|
|
4
|
+
declare global {
|
|
5
|
+
interface Window {
|
|
6
|
+
__SIMULATOR_API__: {
|
|
7
|
+
dispatch: Dispatch;
|
|
8
|
+
subscribe: (listener: () => void) => () => void;
|
|
9
|
+
getState: () => ApplicationState;
|
|
10
|
+
getRequestId: () => string;
|
|
11
|
+
getNotifications: (requestId: string) => {
|
|
12
|
+
id: string;
|
|
13
|
+
message: string;
|
|
14
|
+
type: EnumToUnion<NotificationType>;
|
|
15
|
+
}[];
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { HandlerType } from '@metamask/snaps-utils';
|
|
2
|
+
import { Page } from 'puppeteer';
|
|
3
|
+
export declare type WaitForOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* The timeout in milliseconds.
|
|
6
|
+
*/
|
|
7
|
+
timeout?: number;
|
|
8
|
+
/**
|
|
9
|
+
* The error message to throw if the condition is not met.
|
|
10
|
+
*/
|
|
11
|
+
message?: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Wait for a condition to be true. This is a wrapper around
|
|
15
|
+
* `pptr-testing-library`'s `waitFor` function, with the addition of a custom
|
|
16
|
+
* error message.
|
|
17
|
+
*
|
|
18
|
+
* @param fn - The condition to wait for.
|
|
19
|
+
* @param options - The options.
|
|
20
|
+
* @param options.timeout - The timeout in milliseconds.
|
|
21
|
+
* @param options.message - The error message to throw if the condition is not
|
|
22
|
+
* met.
|
|
23
|
+
* @returns A promise that resolves when the condition is met. The promise
|
|
24
|
+
* resolves to the return value of the condition function.
|
|
25
|
+
*/
|
|
26
|
+
export declare function waitFor<Result>(fn: () => Promise<Result>, { timeout, message }?: WaitForOptions): Promise<Result & ({} | null)>;
|
|
27
|
+
/**
|
|
28
|
+
* Wait for a JSON-RPC response.
|
|
29
|
+
*
|
|
30
|
+
* @param page - The page to wait for the response on.
|
|
31
|
+
* @param type - The type of response to wait for.
|
|
32
|
+
* @returns The JSON-RPC response.
|
|
33
|
+
*/
|
|
34
|
+
export declare function waitForResponse(page: Page, type: HandlerType.OnTransaction | HandlerType.OnRpcRequest | HandlerType.OnCronjob): Promise<{
|
|
35
|
+
result: import("@metamask/utils").Json;
|
|
36
|
+
} | {
|
|
37
|
+
error: import("@metamask/utils").Json;
|
|
38
|
+
}>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { NotificationType } from '@metamask/rpc-methods';
|
|
2
|
+
import { Component } from '@metamask/snaps-ui';
|
|
3
|
+
import { EnumToUnion } from '@metamask/snaps-utils';
|
|
4
|
+
import { Json } from '@metamask/utils';
|
|
5
|
+
import type { MatcherFunction } from 'expect';
|
|
6
|
+
/**
|
|
7
|
+
* Check if a JSON-RPC response matches the expected value. This matcher is
|
|
8
|
+
* intended to be used with the `expect` global.
|
|
9
|
+
*
|
|
10
|
+
* @param actual - The actual response.
|
|
11
|
+
* @param expected - The expected response.
|
|
12
|
+
* @returns The status and message.
|
|
13
|
+
*/
|
|
14
|
+
export declare const toRespondWith: MatcherFunction<[expected: Json]>;
|
|
15
|
+
export declare const toRespondWithError: MatcherFunction<[expected: Json]>;
|
|
16
|
+
/**
|
|
17
|
+
* Check if the snap sent a notification with the expected message. This matcher
|
|
18
|
+
* is intended to be used with the `expect` global.
|
|
19
|
+
*
|
|
20
|
+
* @param actual - The actual response.
|
|
21
|
+
* @param expected - The expected notification message.
|
|
22
|
+
* @param type - The expected notification type.
|
|
23
|
+
* @returns The status and message.
|
|
24
|
+
*/
|
|
25
|
+
export declare const toSendNotification: MatcherFunction<[
|
|
26
|
+
expected: string,
|
|
27
|
+
type?: EnumToUnion<NotificationType> | undefined
|
|
28
|
+
]>;
|
|
29
|
+
export declare const toRender: MatcherFunction<[expected: Component]>;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Infer } from 'superstruct';
|
|
2
|
+
declare const SnapsEnvironmentOptionsStruct: import("superstruct").Struct<{
|
|
3
|
+
keepAlive: boolean;
|
|
4
|
+
server: {
|
|
5
|
+
port: number;
|
|
6
|
+
root: string;
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
};
|
|
9
|
+
browser: {
|
|
10
|
+
headless: boolean;
|
|
11
|
+
};
|
|
12
|
+
executionEnvironmentUrl?: string | undefined;
|
|
13
|
+
simulatorUrl?: string | undefined;
|
|
14
|
+
}, {
|
|
15
|
+
executionEnvironmentUrl: import("superstruct").Struct<string | undefined, null>;
|
|
16
|
+
simulatorUrl: import("superstruct").Struct<string | undefined, null>;
|
|
17
|
+
keepAlive: import("superstruct").Struct<boolean, null>;
|
|
18
|
+
server: import("superstruct").Struct<{
|
|
19
|
+
port: number;
|
|
20
|
+
root: string;
|
|
21
|
+
enabled: boolean;
|
|
22
|
+
}, {
|
|
23
|
+
enabled: import("superstruct").Struct<boolean, null>;
|
|
24
|
+
port: import("superstruct").Struct<number, null>;
|
|
25
|
+
root: import("superstruct").Struct<string, null>;
|
|
26
|
+
}>;
|
|
27
|
+
browser: import("superstruct").Struct<{
|
|
28
|
+
headless: boolean;
|
|
29
|
+
}, {
|
|
30
|
+
headless: import("superstruct").Struct<boolean, null>;
|
|
31
|
+
}>;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* The options for the environment. These can be specified in the Jest
|
|
35
|
+
* configuration under `testEnvironmentOptions`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```json
|
|
39
|
+
* {
|
|
40
|
+
* "testEnvironment": "@metamask/snaps-jest",
|
|
41
|
+
* "testEnvironmentOptions": {
|
|
42
|
+
* "executionEnvironmentUrl": "http://localhost:8080",
|
|
43
|
+
* "server": {
|
|
44
|
+
* "port": 8080,
|
|
45
|
+
* /* ... *\/
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
* @property executionEnvironmentUrl - The URL of the execution environment. If
|
|
51
|
+
* this is not provided, the execution environment will be served from the
|
|
52
|
+
* built-in HTTP server.
|
|
53
|
+
* @property simulatorUrl - The URL of the simulator. If this is not provided,
|
|
54
|
+
* the simulator will be served from the built-in HTTP server.
|
|
55
|
+
* @property keepAlive - Whether to keep the browser open after the tests have
|
|
56
|
+
* finished. This is useful for debugging. Defaults to `false`.
|
|
57
|
+
* @property server - The options for the built-in HTTP server.
|
|
58
|
+
* @property server.enabled - Whether to run the built-in HTTP server. Defaults
|
|
59
|
+
* to `true`.
|
|
60
|
+
* @property server.port - The port to use for the built-in HTTP server. If this
|
|
61
|
+
* is not provided, a random available port will be used.
|
|
62
|
+
* @property server.root - The root directory to serve from the built-in HTTP
|
|
63
|
+
* server. Defaults to the current working directory. This is assumed to be the
|
|
64
|
+
* directory containing the snap manifest and `dist` files. If this is a
|
|
65
|
+
* relative path, it will be resolved relative to the current working directory.
|
|
66
|
+
* @property browser - The options for the browser.
|
|
67
|
+
* @property browser.headless - Whether to run the browser in headless mode.
|
|
68
|
+
* Defaults to `true`.
|
|
69
|
+
*/
|
|
70
|
+
export declare type SnapsEnvironmentOptions = Infer<typeof SnapsEnvironmentOptionsStruct>;
|
|
71
|
+
/**
|
|
72
|
+
* Get the environment options. This validates the options, and returns the
|
|
73
|
+
* default options if none are provided.
|
|
74
|
+
*
|
|
75
|
+
* @param testEnvironmentOptions - The test environment options as defined in
|
|
76
|
+
* the Jest configuration.
|
|
77
|
+
* @returns The environment options.
|
|
78
|
+
*/
|
|
79
|
+
export declare function getOptions(testEnvironmentOptions: Record<string, unknown>): {
|
|
80
|
+
keepAlive: boolean;
|
|
81
|
+
server: {
|
|
82
|
+
port: number;
|
|
83
|
+
root: string;
|
|
84
|
+
enabled: boolean;
|
|
85
|
+
};
|
|
86
|
+
browser: {
|
|
87
|
+
headless: boolean;
|
|
88
|
+
};
|
|
89
|
+
executionEnvironmentUrl?: string | undefined;
|
|
90
|
+
simulatorUrl?: string | undefined;
|
|
91
|
+
};
|
|
92
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './matchers';
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { NotificationType } from '@metamask/rpc-methods';
|
|
2
|
+
import { Component } from '@metamask/snaps-ui';
|
|
3
|
+
import { EnumToUnion } from '@metamask/snaps-utils';
|
|
4
|
+
import { JsonRpcId, JsonRpcParams } from '@metamask/utils';
|
|
5
|
+
import { Infer } from 'superstruct';
|
|
6
|
+
import { Mock, MockJsonRpcOptions, MockOptions, SnapOptionsStruct, SnapResponseStruct, TransactionOptionsStruct } from './internals';
|
|
7
|
+
declare module 'expect' {
|
|
8
|
+
interface AsymmetricMatchers {
|
|
9
|
+
toRespondWith(response: unknown): void;
|
|
10
|
+
toRespondWithError(error: unknown): void;
|
|
11
|
+
toSendNotification(message: string, type?: EnumToUnion<NotificationType>): void;
|
|
12
|
+
toRender(component: Component): void;
|
|
13
|
+
}
|
|
14
|
+
interface Matchers<R> {
|
|
15
|
+
toRespondWith(response: unknown): R;
|
|
16
|
+
toRespondWithError(error: unknown): R;
|
|
17
|
+
toSendNotification(message: string, type?: EnumToUnion<NotificationType>): R;
|
|
18
|
+
toRender(component: Component): R;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Deeply partialize a type.
|
|
23
|
+
*
|
|
24
|
+
* @template Type - The type to partialize.
|
|
25
|
+
* @returns The deeply partialized type.
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* type Foo = {
|
|
29
|
+
* bar: {
|
|
30
|
+
* baz: string;
|
|
31
|
+
* };
|
|
32
|
+
* qux: number;
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* type PartialFoo = DeepPartial<Foo>;
|
|
36
|
+
* // { bar?: { baz?: string; }; qux?: number; }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare type DeepPartial<Type> = {
|
|
40
|
+
[Key in keyof Type]?: Type[Key] extends Record<string, unknown> ? DeepPartial<Type[Key]> : Type[Key];
|
|
41
|
+
};
|
|
42
|
+
export declare type RequestOptions = {
|
|
43
|
+
/**
|
|
44
|
+
* The JSON-RPC request ID.
|
|
45
|
+
*/
|
|
46
|
+
id?: JsonRpcId;
|
|
47
|
+
/**
|
|
48
|
+
* The JSON-RPC method.
|
|
49
|
+
*/
|
|
50
|
+
method: string;
|
|
51
|
+
/**
|
|
52
|
+
* The JSON-RPC params.
|
|
53
|
+
*/
|
|
54
|
+
params?: JsonRpcParams;
|
|
55
|
+
/**
|
|
56
|
+
* The origin to send the request from.
|
|
57
|
+
*/
|
|
58
|
+
origin?: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* The `runCronjob` options. This is the same as {@link RequestOptions}, except
|
|
62
|
+
* that it does not have an `origin` property.
|
|
63
|
+
*/
|
|
64
|
+
export declare type CronjobOptions = Omit<RequestOptions, 'origin'>;
|
|
65
|
+
/**
|
|
66
|
+
* The options to use for transaction requests.
|
|
67
|
+
*
|
|
68
|
+
* @property chainId - The CAIP-2 chain ID to send the transaction on. Defaults
|
|
69
|
+
* to `eip155:1`.
|
|
70
|
+
* @property origin - The origin to send the transaction from. Defaults to
|
|
71
|
+
* `metamask.io`.
|
|
72
|
+
* @property from - The address to send the transaction from. Defaults to a
|
|
73
|
+
* randomly generated address.
|
|
74
|
+
* @property to - The address to send the transaction to. Defaults to a randomly
|
|
75
|
+
* generated address.
|
|
76
|
+
* @property value - The value to send with the transaction. Defaults to `0`.
|
|
77
|
+
* @property data - The data to send with the transaction. Defaults to `0x`.
|
|
78
|
+
* @property gasLimit - The gas limit to use for the transaction. Defaults to
|
|
79
|
+
* `21_000`.
|
|
80
|
+
* @property maxFeePerGas - The maximum fee per gas to use for the transaction.
|
|
81
|
+
* Defaults to `1`.
|
|
82
|
+
* @property maxPriorityFeePerGas - The maximum priority fee per gas to use for
|
|
83
|
+
* the transaction. Defaults to `1`.
|
|
84
|
+
* @property nonce - The nonce to use for the transaction. Defaults to `0`.
|
|
85
|
+
*/
|
|
86
|
+
export declare type TransactionOptions = Infer<typeof TransactionOptionsStruct>;
|
|
87
|
+
/**
|
|
88
|
+
* The options to use for requests to the snap.
|
|
89
|
+
*
|
|
90
|
+
* @property timeout - The timeout in milliseconds to use. Defaults to `1000`.
|
|
91
|
+
*/
|
|
92
|
+
export declare type SnapOptions = Infer<typeof SnapOptionsStruct>;
|
|
93
|
+
/**
|
|
94
|
+
* A `snap_dialog` alert interface.
|
|
95
|
+
*/
|
|
96
|
+
export declare type SnapAlertInterface = {
|
|
97
|
+
/**
|
|
98
|
+
* The type of the interface. This is always `alert`.
|
|
99
|
+
*/
|
|
100
|
+
type: 'alert';
|
|
101
|
+
/**
|
|
102
|
+
* The content to show in the alert.
|
|
103
|
+
*/
|
|
104
|
+
content: Component;
|
|
105
|
+
/**
|
|
106
|
+
* Close the alert.
|
|
107
|
+
*/
|
|
108
|
+
ok(): Promise<void>;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* A `snap_dialog` confirmation interface.
|
|
112
|
+
*/
|
|
113
|
+
export declare type SnapConfirmationInterface = {
|
|
114
|
+
/**
|
|
115
|
+
* The type of the interface. This is always `confirmation`.
|
|
116
|
+
*/
|
|
117
|
+
type: 'confirmation';
|
|
118
|
+
/**
|
|
119
|
+
* The content to show in the confirmation.
|
|
120
|
+
*/
|
|
121
|
+
content: Component;
|
|
122
|
+
/**
|
|
123
|
+
* Close the confirmation.
|
|
124
|
+
*/
|
|
125
|
+
ok(): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Cancel the confirmation.
|
|
128
|
+
*/
|
|
129
|
+
cancel(): Promise<void>;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* A `snap_dialog` prompt interface.
|
|
133
|
+
*/
|
|
134
|
+
export declare type SnapPromptInterface = {
|
|
135
|
+
/**
|
|
136
|
+
* The type of the interface. This is always `prompt`.
|
|
137
|
+
*/
|
|
138
|
+
type: 'prompt';
|
|
139
|
+
/**
|
|
140
|
+
* The content to show in the prompt.
|
|
141
|
+
*/
|
|
142
|
+
content: Component;
|
|
143
|
+
/**
|
|
144
|
+
* Close the prompt.
|
|
145
|
+
*
|
|
146
|
+
* @param value - The value to close the prompt with.
|
|
147
|
+
*/
|
|
148
|
+
ok(value?: string): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Cancel the prompt.
|
|
151
|
+
*/
|
|
152
|
+
cancel(): Promise<void>;
|
|
153
|
+
};
|
|
154
|
+
export declare type SnapInterface = SnapAlertInterface | SnapConfirmationInterface | SnapPromptInterface;
|
|
155
|
+
export declare type SnapRequestObject = {
|
|
156
|
+
/**
|
|
157
|
+
* Get a user interface object from a snap. This will throw an error if the
|
|
158
|
+
* snap does not show a user interface within the timeout.
|
|
159
|
+
*
|
|
160
|
+
* @param options - The options to use.
|
|
161
|
+
* @param options.timeout - The timeout in milliseconds to use. Defaults to
|
|
162
|
+
* `1000`.
|
|
163
|
+
* @returns The user interface object.
|
|
164
|
+
*/
|
|
165
|
+
getInterface(options?: SnapOptions): Promise<SnapInterface>;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* A pending request object. This is a promise with extra
|
|
169
|
+
* {@link SnapRequestObject} fields.
|
|
170
|
+
*/
|
|
171
|
+
export declare type SnapRequest = Promise<SnapResponse> & SnapRequestObject;
|
|
172
|
+
/**
|
|
173
|
+
* This is the main entry point to interact with the snap. It is returned by
|
|
174
|
+
* {@link installSnap}, and has methods to send requests to the snap.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* import { installSnap } from '@metamask/snaps-jest';
|
|
179
|
+
*
|
|
180
|
+
* const snap = await installSnap();
|
|
181
|
+
* const response = await snap.request({ method: 'hello' });
|
|
182
|
+
*
|
|
183
|
+
* expect(response).toRespondWith('Hello, world!');
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export declare type Snap = {
|
|
187
|
+
/**
|
|
188
|
+
* Send a JSON-RPC request to the snap.
|
|
189
|
+
*
|
|
190
|
+
* @param request - The request. This is similar to a JSON-RPC request, but
|
|
191
|
+
* has an extra `origin` field.
|
|
192
|
+
* @returns The response promise, with extra {@link SnapRequestObject} fields.
|
|
193
|
+
*/
|
|
194
|
+
request(request: RequestOptions): SnapRequest;
|
|
195
|
+
/**
|
|
196
|
+
* Send a transaction to the snap.
|
|
197
|
+
*
|
|
198
|
+
* @param transaction - The transaction. This is similar to an Ethereum
|
|
199
|
+
* transaction object, but has an extra `origin` field. Any missing fields
|
|
200
|
+
* will be filled in with default values.
|
|
201
|
+
* @returns The response.
|
|
202
|
+
*/
|
|
203
|
+
sendTransaction(transaction?: Partial<TransactionOptions>): Promise<SnapResponse>;
|
|
204
|
+
/**
|
|
205
|
+
* Run a cronjob in the snap. This is similar to {@link request}, but the
|
|
206
|
+
* request will be sent to the `onCronjob` method of the snap.
|
|
207
|
+
*
|
|
208
|
+
* @param cronjob - The cronjob request. This is similar to a JSON-RPC
|
|
209
|
+
* request, and is normally specified in the snap manifest, under the
|
|
210
|
+
* `endowment:cronjob` permission.
|
|
211
|
+
* @returns The response promise, with extra {@link SnapRequestObject} fields.
|
|
212
|
+
*/
|
|
213
|
+
runCronjob(cronjob: CronjobOptions): SnapRequest;
|
|
214
|
+
/**
|
|
215
|
+
* Close the page running the snap. This is mainly useful for cleaning up
|
|
216
|
+
* the test environment, and calling it is not strictly necessary.
|
|
217
|
+
*
|
|
218
|
+
* @returns A promise that resolves when the page is closed.
|
|
219
|
+
*/
|
|
220
|
+
close(): Promise<void>;
|
|
221
|
+
/**
|
|
222
|
+
* Enable network mocking for the snap.
|
|
223
|
+
*
|
|
224
|
+
* @param options - The options for the network mocking.
|
|
225
|
+
* @returns A {@link Mock} object, with an `unmock` function.
|
|
226
|
+
*/
|
|
227
|
+
mock(options: DeepPartial<MockOptions>): Promise<Mock>;
|
|
228
|
+
/**
|
|
229
|
+
* Enable JSON-RPC provider mocking for the snap. This will mock any requests
|
|
230
|
+
* sent through the `ethereum` global, with the specified `method`.
|
|
231
|
+
*
|
|
232
|
+
* @param options - The options for the JSON-RPC mocking.
|
|
233
|
+
* @param options.method - The JSON-RPC method to mock, e.g.,
|
|
234
|
+
* `eth_blockNumber`.
|
|
235
|
+
* @param options.result - The JSON value to return.
|
|
236
|
+
* @returns A {@link Mock} object, with an `unmock` function.
|
|
237
|
+
*/
|
|
238
|
+
mockJsonRpc(options: MockJsonRpcOptions): Promise<Mock>;
|
|
239
|
+
};
|
|
240
|
+
export declare type SnapResponse = Infer<typeof SnapResponseStruct>;
|
|
241
|
+
export { NotificationType } from '@metamask/rpc-methods';
|
package/jest-preset.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/valid-types */
|
|
2
|
+
|
|
3
|
+
const { resolve } = require('path');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @type {import('jest').Config}
|
|
7
|
+
*/
|
|
8
|
+
const config = {
|
|
9
|
+
testEnvironment: '@metamask/snaps-jest',
|
|
10
|
+
|
|
11
|
+
// End-to-end tests can take longer than usual to run, so we set the test
|
|
12
|
+
// timeout to 30 seconds by default.
|
|
13
|
+
testTimeout: 30000,
|
|
14
|
+
|
|
15
|
+
setupFilesAfterEnv: [resolve(__dirname, 'dist', 'cjs', 'setup.js')],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports = config;
|