Test a Snap
You can test your Snap locally, in the Snaps sandbox, and end-to-end using Jest.
Test locally
-
Host your Snap locally:
yarn start
-
Install your Snap in Flask.
-
Test your Snap by calling its API methods from a dapp.
Test in the sandbox
Use the Snaps sandbox to test and debug your Snap in an easy-to-use interface.
-
Run the
sandbox
subcommand to start the sandbox server:yarn mm-snap sandbox
Navigate to the
localhost
URL displayed in the terminal. -
Install your Snap in Flask.
-
Test your Snap by calling its API methods from the sandbox interface.
Test end-to-end
Follow these steps to test your Snap end-to-end in a Jest environment.
1. Install @metamask/snaps-jest
Install the @metamask/snaps-jest
package into your Snap project using Yarn
or npm:
yarn add -D @metamask/snaps-jest
or
npm i @metamask/snaps-jest
2. Configure @metamask/snaps-jest
The easiest way to configure this package is to add it to your Jest configuration as a preset.
In the jest.config.js
file, add the following:
module.exports = {
preset: "@metamask/snaps-jest",
}
This automatically configures Jest to use the @metamask/snaps-jest
environment, and to use the
@metamask/snaps-jest
matchers.
You can then run the jest
command as usual.
@metamask/snaps-jest
assumes the Snap is built in the directory you run Jest from.
If you use a different directory, you can specify the path using the
server.root
option, or by running your own HTTP server.
It's currently not possible to use @metamask/snaps-jest
with a Snap that is not built.
If you don't use the package as a preset, you can alternatively add the @metamask/snaps-jest
environment and matchers to your Jest configuration manually:
module.exports = {
testEnvironment: "@metamask/snaps-jest",
setupFilesAfterEnv: ["@metamask/snaps-jest/dist/cjs/setup.js"],
}
You can pass any Jest options to the test environment by adding a
testEnvironmentOptions
property to your Jest configuration.
For example:
module.exports = {
preset: "@metamask/snaps-jest",
testEnvironmentOptions: {
// Options go here.
},
}
All options are optional.
3. Use @metamask/snaps-jest
Use the package by calling any of the API methods. For example, you can:
- Install a Snap.
- Send a transaction to the Snap.
- Run a cron job in the Snap.
- Interact with user interfaces.
You can also use Jest matchers to assert that a response from a Snap matches an expected value.