@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
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.35.1-flask.1]
|
|
10
|
+
### Fixed
|
|
11
|
+
- Fix publishing to NPM ([#1538](https://github.com/MetaMask/snaps/pull/1538))
|
|
12
|
+
|
|
13
|
+
## [0.35.0-flask.1]
|
|
14
|
+
### Added
|
|
15
|
+
- Initial release ([#1438](https://github.com/MetaMask/snaps/pull/1438), [#1488](https://github.com/MetaMask/snaps/pull/1488), [#1519](https://github.com/MetaMask/snaps/pull/1519), [#1532](https://github.com/MetaMask/snaps/pull/1532))
|
|
16
|
+
|
|
17
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/v0.35.1-flask.1...HEAD
|
|
18
|
+
[0.35.1-flask.1]: https://github.com/MetaMask/snaps/compare/v0.35.0-flask.1...v0.35.1-flask.1
|
|
19
|
+
[0.35.0-flask.1]: https://github.com/MetaMask/snaps/releases/tag/v0.35.0-flask.1
|
package/README.md
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
# `@metamask/snaps-jest`
|
|
2
|
+
|
|
3
|
+
A [Jest](https://jestjs.io/) preset for end-to-end testing MetaMask Snaps,
|
|
4
|
+
including a Jest environment, and a set of Jest matchers. This package is
|
|
5
|
+
currently experimental, and the API may change in the future.
|
|
6
|
+
|
|
7
|
+
- [Installation](#installation)
|
|
8
|
+
- [Usage](#usage)
|
|
9
|
+
- [Without the preset](#without-the-preset)
|
|
10
|
+
- [API](#api)
|
|
11
|
+
- [Install a snap](#install-a-snap)
|
|
12
|
+
- [`snap.request`](#snaprequest)
|
|
13
|
+
- [`snap.sendTransaction`](#snapsendtransaction)
|
|
14
|
+
- [`snap.runCronjob`](#snapruncronjob)
|
|
15
|
+
- [Jest matchers](#jest-matchers)
|
|
16
|
+
- [Interacting with user interfaces](#interacting-with-user-interfaces)
|
|
17
|
+
- [Network mocking](#network-mocking-snapmock)
|
|
18
|
+
- [Options](#options)
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Use Node.js `16.0.0` or later. We recommend using [nvm](https://github.com/nvm-sh/nvm) for managing Node.js versions.
|
|
23
|
+
|
|
24
|
+
Install a dependency in your snap project using `yarn` (or `npm`):
|
|
25
|
+
|
|
26
|
+
- `yarn add -D @metamask/snaps-jest`
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
The easiest way to use this package is to add it to your Jest configuration as
|
|
31
|
+
a preset. In your `jest.config.js` file, add the following:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
module.exports = {
|
|
35
|
+
preset: '@metamask/snaps-jest',
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
This will automatically configure Jest to use the `@metamask/snaps-jest`
|
|
40
|
+
environment, and to use the `@metamask/snaps-jest` matchers.
|
|
41
|
+
|
|
42
|
+
Then you can just run `jest` as usual.
|
|
43
|
+
|
|
44
|
+
> **Note**: `@metamask/snaps-jest` assumes that the snap is built in the
|
|
45
|
+
> directory you're running Jest from. If you're using a different directory,
|
|
46
|
+
> you can specify the path to the snap using the [`root`](#options) option, or
|
|
47
|
+
> by running your own HTTP server.
|
|
48
|
+
>
|
|
49
|
+
> Right now it's not possible to use `@metamask/snaps-jest` with a snap that
|
|
50
|
+
> isn't built.
|
|
51
|
+
|
|
52
|
+
### Without the preset
|
|
53
|
+
|
|
54
|
+
If you don't want to use the preset, you can still use the environment and
|
|
55
|
+
matchers by adding them to your Jest configuration manually:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
module.exports = {
|
|
59
|
+
testEnvironment: '@metamask/snaps-jest',
|
|
60
|
+
setupFilesAfterEnv: ['@metamask/snaps-jest/dist/setup.js'],
|
|
61
|
+
};
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API
|
|
65
|
+
|
|
66
|
+
### Install a snap
|
|
67
|
+
|
|
68
|
+
`@metamask/snaps-jest` exposes a `installSnap` function that can be used to
|
|
69
|
+
install a snap in the browser. It's recommended to use this function in each
|
|
70
|
+
test, to ensure that each test starts with a clean slate.
|
|
71
|
+
|
|
72
|
+
By default, if the built-in server is enabled, it will install the snap from
|
|
73
|
+
the built-in server. Otherwise, you must specify a snap ID to install.
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
77
|
+
|
|
78
|
+
describe('MySnap', () => {
|
|
79
|
+
it('should do something', async () => {
|
|
80
|
+
await installSnap(/* optional snap ID */);
|
|
81
|
+
// ...
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The `installSnap` function returns an object with functions that can be used to
|
|
87
|
+
interact with the snap.
|
|
88
|
+
|
|
89
|
+
### `snap.request`
|
|
90
|
+
|
|
91
|
+
The `request` function can be used to send a JSON-RPC request to the snap. It
|
|
92
|
+
takes a single argument, which is similar to a JSON-RPC request object, but
|
|
93
|
+
with an additional optional `origin` property. It returns a promise that
|
|
94
|
+
resolves to the response from the
|
|
95
|
+
[onRpcRequest](https://docs.metamask.io/snaps/reference/exports/#onrpcrequest)
|
|
96
|
+
function.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
100
|
+
|
|
101
|
+
describe('MySnap', () => {
|
|
102
|
+
it('should do something', async () => {
|
|
103
|
+
const { request } = await installSnap(/* optional snap ID */);
|
|
104
|
+
const response = await request({
|
|
105
|
+
origin: 'http://localhost:8080',
|
|
106
|
+
method: 'foo',
|
|
107
|
+
params: [],
|
|
108
|
+
});
|
|
109
|
+
// ...
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
It returns an object with a response, and some additional metadata, which can be
|
|
115
|
+
checked using the [Jest matchers](#jest-matchers):
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
119
|
+
|
|
120
|
+
describe('MySnap', () => {
|
|
121
|
+
it('should do something', async () => {
|
|
122
|
+
const { request } = await installSnap(/* optional snap ID */);
|
|
123
|
+
const response = await request({
|
|
124
|
+
origin: 'http://localhost:8080',
|
|
125
|
+
method: 'foo',
|
|
126
|
+
params: [],
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(response).toRespondWith('bar');
|
|
130
|
+
expect(response).not.toRespondWithError('baz');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### Checking the response with standard Jest matchers
|
|
136
|
+
|
|
137
|
+
If you prefer, you can also manually check the response:
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
141
|
+
|
|
142
|
+
describe('MySnap', () => {
|
|
143
|
+
it('should do something', async () => {
|
|
144
|
+
const { request } = await installSnap(/* optional snap ID */);
|
|
145
|
+
const { response } = await request({
|
|
146
|
+
origin: 'http://localhost:8080',
|
|
147
|
+
method: 'foo',
|
|
148
|
+
params: [],
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(response.result).toBe('bar');
|
|
152
|
+
expect(response.error).toBeUndefined();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Since the response is a standard JSON-RPC response, you can use any Jest
|
|
158
|
+
matchers to check it, including snapshot matchers:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
162
|
+
|
|
163
|
+
describe('MySnap', () => {
|
|
164
|
+
it('should do something', async () => {
|
|
165
|
+
const { request } = await installSnap(/* optional snap ID */);
|
|
166
|
+
const { response } = await request({
|
|
167
|
+
origin: 'http://localhost:8080',
|
|
168
|
+
method: 'foo',
|
|
169
|
+
params: [],
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
expect(response).toMatchSnapshot();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### `snap.sendTransaction`
|
|
178
|
+
|
|
179
|
+
The `sendTransaction` function can be used to send a transaction to the snap. It
|
|
180
|
+
takes a single argument, which is an object with the following properties:
|
|
181
|
+
|
|
182
|
+
- `origin`: The origin of the transaction.
|
|
183
|
+
- `chainId`: The chain ID of the transaction.
|
|
184
|
+
- `from`: The address of the sender.
|
|
185
|
+
- `to`: The address of the recipient.
|
|
186
|
+
- `value`: The value of the transaction, in wei.
|
|
187
|
+
- `data`: The data of the transaction.
|
|
188
|
+
- `gasLimit`: The gas limit of the transaction.
|
|
189
|
+
- `maxFeePerGas`: The maximum fee per gas of the transaction.
|
|
190
|
+
- `maxPriorityFeePerGas`: The maximum priority fee per gas of the transaction.
|
|
191
|
+
- `nonce`: The nonce of the transaction.
|
|
192
|
+
|
|
193
|
+
All properties are optional, and have sensible defaults. The addresses are
|
|
194
|
+
randomly generated by default. Most values can be specified as a hex string, or
|
|
195
|
+
a decimal number.
|
|
196
|
+
|
|
197
|
+
It returns an object with the user interface that was shown by the snap, in the
|
|
198
|
+
[onTransaction](https://docs.metamask.io/snaps/reference/exports/#ontransaction)
|
|
199
|
+
function.
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
203
|
+
import { panel, text } from '@metamask/snaps-ui';
|
|
204
|
+
|
|
205
|
+
describe('MySnap', () => {
|
|
206
|
+
it('should do something', async () => {
|
|
207
|
+
const { sendTransaction } = await installSnap(/* optional snap ID */);
|
|
208
|
+
const response = await sendTransaction({
|
|
209
|
+
value: '0x0',
|
|
210
|
+
data: '0x',
|
|
211
|
+
gasLimit: '0x5208',
|
|
212
|
+
maxFeePerGas: '0x5208',
|
|
213
|
+
maxPriorityFeePerGas: '0x5208',
|
|
214
|
+
nonce: '0x0',
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
expect(response).toRender(panel([text('Hello, world!')]));
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### `snap.runCronjob`
|
|
223
|
+
|
|
224
|
+
The `runCronjob` function can be used to run a cronjob in the snap. It takes
|
|
225
|
+
a single argument, which is similar to a JSON-RPC request object. It returns
|
|
226
|
+
a promise that resolves to the response from the
|
|
227
|
+
[onCronjob](https://docs.metamask.io/snaps/reference/exports/#oncronjob)
|
|
228
|
+
function.
|
|
229
|
+
|
|
230
|
+
The request would normally be specified in the snap manifest under the
|
|
231
|
+
`endowment:cronjob` permission, but this function allows you to run cronjobs
|
|
232
|
+
that are not specified in the manifest as well.
|
|
233
|
+
|
|
234
|
+
```js
|
|
235
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
236
|
+
|
|
237
|
+
describe('MySnap', () => {
|
|
238
|
+
it('should do something', async () => {
|
|
239
|
+
const { runCronjob } = await installSnap(/* optional snap ID */);
|
|
240
|
+
const response = await runCronjob({
|
|
241
|
+
method: 'foo',
|
|
242
|
+
params: [],
|
|
243
|
+
});
|
|
244
|
+
// ...
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
It returns an object with a response, and some additional metadata, which can be
|
|
250
|
+
checked using the [Jest matchers](#jest-matchers):
|
|
251
|
+
|
|
252
|
+
```js
|
|
253
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
254
|
+
|
|
255
|
+
describe('MySnap', () => {
|
|
256
|
+
it('should do something', async () => {
|
|
257
|
+
const { runCronjob } = await installSnap(/* optional snap ID */);
|
|
258
|
+
const response = await runCronjob({
|
|
259
|
+
method: 'foo',
|
|
260
|
+
params: [],
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
expect(response).toRespondWith('bar');
|
|
264
|
+
expect(response).not.toRespondWithError('baz');
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### `snap.close`
|
|
270
|
+
|
|
271
|
+
The `close` function can be used to close the page that the test is running in.
|
|
272
|
+
This is mainly useful for cleaning up after a test, and is not required. It can
|
|
273
|
+
potentially speed up your tests, since it prevents too many pages from being
|
|
274
|
+
open at the same time.
|
|
275
|
+
|
|
276
|
+
```js
|
|
277
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
278
|
+
|
|
279
|
+
describe('MySnap', () => {
|
|
280
|
+
it('should do something', async () => {
|
|
281
|
+
const { close } = await installSnap(/* optional snap ID */);
|
|
282
|
+
// ...
|
|
283
|
+
await close();
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Jest matchers
|
|
289
|
+
|
|
290
|
+
`@metamask/snaps-jest` includes a set of Jest matchers that can be used to
|
|
291
|
+
assert that a response from a snap matches an expected value:
|
|
292
|
+
|
|
293
|
+
- `toRespondWith(expectedResponse)`: Check if a response matches an expected
|
|
294
|
+
response. This matcher will check the `result` property of the response. If
|
|
295
|
+
the response is an error, it will fail.
|
|
296
|
+
- `toRespondWithError(expectedError)`: Check if a response matches an expected
|
|
297
|
+
error. This matcher will check the `error` property of the response. If the
|
|
298
|
+
response is not an error, it will fail.
|
|
299
|
+
- `toSendNotification(notificationText)`: Check if a snap sent a notification.
|
|
300
|
+
- `toRender(expectedInterface)`: Check if a snap rendered an interface. This is
|
|
301
|
+
useful for testing the UI of a snap, either for a
|
|
302
|
+
[`snap_dialog`](https://docs.metamask.io/snaps/reference/rpc-api/#snap_dialog),
|
|
303
|
+
or a UI rendered by
|
|
304
|
+
[the transaction insights API](https://docs.metamask.io/snaps/reference/exports/#ontransaction).
|
|
305
|
+
|
|
306
|
+
### Interacting with user interfaces
|
|
307
|
+
|
|
308
|
+
If your snap uses `snap_dialog` to show user interfaces, you can use the
|
|
309
|
+
`request.getInterface` function to interact with them. This method is present on
|
|
310
|
+
the return value of the `snap.request` function.
|
|
311
|
+
|
|
312
|
+
It waits for the user interface to be shown, and returns an object with
|
|
313
|
+
functions that can be used to interact with the user interface.
|
|
314
|
+
|
|
315
|
+
#### Example
|
|
316
|
+
|
|
317
|
+
```js
|
|
318
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
319
|
+
import { text } from '@metamask/snaps-ui';
|
|
320
|
+
import { assert } from '@metamask/utils';
|
|
321
|
+
|
|
322
|
+
describe('MySnap', () => {
|
|
323
|
+
it('should do something', async () => {
|
|
324
|
+
const { request } = await installSnap(/* optional snap ID */);
|
|
325
|
+
|
|
326
|
+
// Note: We cannot resolve the promise yet!
|
|
327
|
+
const response = request({
|
|
328
|
+
method: 'foo',
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const ui = await response.getInterface();
|
|
332
|
+
|
|
333
|
+
// This is useful if you're using TypeScript, since it will infer the type
|
|
334
|
+
// of the user interface.
|
|
335
|
+
assert(ui.type === 'alert');
|
|
336
|
+
expect(ui).toRender(text('Hello, world!'));
|
|
337
|
+
|
|
338
|
+
// "Click" the OK button.
|
|
339
|
+
await ui.ok();
|
|
340
|
+
|
|
341
|
+
// Now we can resolve the promise.
|
|
342
|
+
const result = await response;
|
|
343
|
+
expect(result).toRespondWith('bar');
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Network mocking (`snap.mock`)
|
|
349
|
+
|
|
350
|
+
`@metamask/snaps-jest` includes basic network mocking functionality. This can
|
|
351
|
+
be used to mock the response of any network request made by the snap through
|
|
352
|
+
the `endowment:network-access` functionality.
|
|
353
|
+
|
|
354
|
+
The mock function takes a single argument, which is an object with the
|
|
355
|
+
following properties:
|
|
356
|
+
|
|
357
|
+
- `url` (`string | RegExp`): The URL of the request. This can be a string, or a
|
|
358
|
+
regular expression.
|
|
359
|
+
- `partial` (`boolean`): If enabled, the request will be mocked if the URL
|
|
360
|
+
starts with the given URL. This option is ignored if a RegExp is provided to
|
|
361
|
+
the `url` option. Defaults to `false`.
|
|
362
|
+
- `response` (`object`): An object with the response.
|
|
363
|
+
- `status` (`number`): The status code of the response. Defaults to `200`.
|
|
364
|
+
- `body` (`string`): The body of the response. Defaults to an empty string.
|
|
365
|
+
- `headers` (`object`): An object with the headers of the response. By
|
|
366
|
+
default, this will use headers that enable CORS.
|
|
367
|
+
- `contentType` (`string`): The content type of the response. Defaults to
|
|
368
|
+
`text/plain`.
|
|
369
|
+
|
|
370
|
+
Except for the `url` option, all options are optional.
|
|
371
|
+
|
|
372
|
+
#### Unmocking (`mock.unmock`)
|
|
373
|
+
|
|
374
|
+
The mock function returns an object with an `unmock` function that can be
|
|
375
|
+
used to remove the mock. Mocking happens on a per-snap-install basis. If the
|
|
376
|
+
mock is not removed, it will remain active for the rest of the snap
|
|
377
|
+
installation, so it does not affect other tests with a fresh snap installation.
|
|
378
|
+
|
|
379
|
+
#### Example
|
|
380
|
+
|
|
381
|
+
```js
|
|
382
|
+
import { installSnap } from '@metamask/snaps-jest';
|
|
383
|
+
|
|
384
|
+
describe('MySnap', () => {
|
|
385
|
+
it('should do something', async () => {
|
|
386
|
+
const { mock, request } = await installSnap(/* optional snap ID */);
|
|
387
|
+
|
|
388
|
+
const { unmock } = mock({
|
|
389
|
+
url: 'https://example.com',
|
|
390
|
+
status: 200,
|
|
391
|
+
body: 'Hello, world!',
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// ...
|
|
395
|
+
|
|
396
|
+
unmock();
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
## Options
|
|
402
|
+
|
|
403
|
+
You can pass options to the test environment by adding a
|
|
404
|
+
`testEnvironmentOptions` property to your Jest configuration. For example:
|
|
405
|
+
|
|
406
|
+
```js
|
|
407
|
+
module.exports = {
|
|
408
|
+
preset: '@metamask/snaps-jest',
|
|
409
|
+
testEnvironmentOptions: {
|
|
410
|
+
// Options go here.
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
All options are optional, and have sensible defaults.
|
|
416
|
+
|
|
417
|
+
### `keepAlive`
|
|
418
|
+
|
|
419
|
+
- Type: `boolean`
|
|
420
|
+
- Default: `false`
|
|
421
|
+
|
|
422
|
+
Whether to keep the Jest environment running after the tests have finished. This
|
|
423
|
+
is useful for debugging, but should not be used in CI environments: When this is
|
|
424
|
+
enabled, the `jest` process will not exit on its own, and will need to be
|
|
425
|
+
manually killed (e.g., with <kbd>Ctrl</kbd> + <kbd>C</kbd>).
|
|
426
|
+
|
|
427
|
+
#### Example
|
|
428
|
+
|
|
429
|
+
```js
|
|
430
|
+
module.exports = {
|
|
431
|
+
preset: '@metamask/snaps-jest',
|
|
432
|
+
testEnvironmentOptions: {
|
|
433
|
+
keepAlive: true,
|
|
434
|
+
},
|
|
435
|
+
};
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### `browser`
|
|
439
|
+
|
|
440
|
+
- Type: `object`
|
|
441
|
+
|
|
442
|
+
Options for the browser that is used to run the tests.
|
|
443
|
+
|
|
444
|
+
#### `browser.headless`
|
|
445
|
+
|
|
446
|
+
- Type: `boolean`
|
|
447
|
+
- Default: `true`
|
|
448
|
+
|
|
449
|
+
Whether to run the browser in headless mode. By default, it will be enabled. If
|
|
450
|
+
you want to see the browser window while the tests are running, you can disable
|
|
451
|
+
this option. Note that this will require you to have a graphical environment
|
|
452
|
+
available, so it is not recommended for CI environments, but can be useful for
|
|
453
|
+
debugging in conjunction with the `keepAlive` option.
|
|
454
|
+
|
|
455
|
+
### `server`
|
|
456
|
+
|
|
457
|
+
- Type: `object`
|
|
458
|
+
|
|
459
|
+
Options for the built-in HTTP server that is included with this package. This
|
|
460
|
+
server is used to serve the execution environment, simulator, and the snap
|
|
461
|
+
bundle during tests.
|
|
462
|
+
|
|
463
|
+
#### `server.enabled`
|
|
464
|
+
|
|
465
|
+
- Type: `boolean`
|
|
466
|
+
- Default: `true`
|
|
467
|
+
|
|
468
|
+
Whether to enable the built-in HTTP server. By default, it will be enabled. If
|
|
469
|
+
you want to use your own HTTP server, you can disable this option, and use the
|
|
470
|
+
`executionEnvironmentUrl` and `simulatorUrl` options to configure the URLs of
|
|
471
|
+
your own server.
|
|
472
|
+
|
|
473
|
+
##### Example
|
|
474
|
+
|
|
475
|
+
```js
|
|
476
|
+
module.exports = {
|
|
477
|
+
preset: '@metamask/snaps-jest',
|
|
478
|
+
testEnvironmentOptions: {
|
|
479
|
+
server: {
|
|
480
|
+
enabled: false,
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
#### `server.port`
|
|
487
|
+
|
|
488
|
+
- Type: `number`
|
|
489
|
+
|
|
490
|
+
The port to use for the built-in HTTP server. By default, it will use a random
|
|
491
|
+
available (unprivileged) port.
|
|
492
|
+
|
|
493
|
+
##### Example
|
|
494
|
+
|
|
495
|
+
```js
|
|
496
|
+
module.exports = {
|
|
497
|
+
preset: '@metamask/snaps-jest',
|
|
498
|
+
testEnvironmentOptions: {
|
|
499
|
+
server: {
|
|
500
|
+
port: 8080,
|
|
501
|
+
},
|
|
502
|
+
},
|
|
503
|
+
};
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
#### `server.root`
|
|
507
|
+
|
|
508
|
+
- Type: `string`
|
|
509
|
+
- Default: `process.cwd()`
|
|
510
|
+
|
|
511
|
+
The root directory to serve the snap files from. By default, it will use the
|
|
512
|
+
current working directory. This is useful if you want to serve the snap files
|
|
513
|
+
from a different directory than the one that Jest is running from.
|
|
514
|
+
|
|
515
|
+
##### Example
|
|
516
|
+
|
|
517
|
+
```js
|
|
518
|
+
module.exports = {
|
|
519
|
+
preset: '@metamask/snaps-jest',
|
|
520
|
+
testEnvironmentOptions: {
|
|
521
|
+
server: {
|
|
522
|
+
root: '/path/to/snap/files',
|
|
523
|
+
},
|
|
524
|
+
},
|
|
525
|
+
};
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
##### Example
|
|
529
|
+
|
|
530
|
+
```js
|
|
531
|
+
module.exports = {
|
|
532
|
+
preset: '@metamask/snaps-jest',
|
|
533
|
+
testEnvironmentOptions: {
|
|
534
|
+
browser: {
|
|
535
|
+
headless: false,
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
};
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
### `executionEnvironmentUrl`
|
|
542
|
+
|
|
543
|
+
- Type: `string`
|
|
544
|
+
|
|
545
|
+
The URL of the execution environment to use for testing. This is the URL that
|
|
546
|
+
will be loaded by the Snaps Simulator in the tests. By default, it will use the
|
|
547
|
+
URL of the built-in HTTP server that is included with this package.
|
|
548
|
+
|
|
549
|
+
> **Note**: This option is intended for advanced use cases. In most cases, you
|
|
550
|
+
> should not need to change this option.
|
|
551
|
+
|
|
552
|
+
See also: [`@metamask/snaps-execution-environments`](../snaps-execution-environments/README.md).
|
|
553
|
+
|
|
554
|
+
#### Example
|
|
555
|
+
|
|
556
|
+
```js
|
|
557
|
+
module.exports = {
|
|
558
|
+
preset: '@metamask/snaps-jest',
|
|
559
|
+
testEnvironmentOptions: {
|
|
560
|
+
executionEnvironmentUrl: 'http://localhost:8080',
|
|
561
|
+
},
|
|
562
|
+
};
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
### `simulatorUrl`
|
|
566
|
+
|
|
567
|
+
- Type: `string`
|
|
568
|
+
|
|
569
|
+
The URL of the simulator to use for testing. This is the URL that will be
|
|
570
|
+
loaded in the browser when running tests. By default, it will use the URL of
|
|
571
|
+
the built-in HTTP server that is included with this package.
|
|
572
|
+
|
|
573
|
+
> **Note**: This option is intended for advanced use cases. In most cases, you
|
|
574
|
+
> should not need to change this option.
|
|
575
|
+
|
|
576
|
+
See also: [`@metamask/snaps-simulator`](../snaps-simulator/README.md).
|
|
577
|
+
|
|
578
|
+
#### Example
|
|
579
|
+
|
|
580
|
+
```js
|
|
581
|
+
module.exports = {
|
|
582
|
+
preset: '@metamask/snaps-jest',
|
|
583
|
+
testEnvironmentOptions: {
|
|
584
|
+
simulatorUrl: 'http://localhost:8081',
|
|
585
|
+
},
|
|
586
|
+
};
|
|
587
|
+
```
|