Skip to content

Commit

Permalink
Add end-to-end test to snap (MetaMask#49)
Browse files Browse the repository at this point in the history
* Add end-to-end test to snap

* Update GitHub Actions workflows to run E2E tests

* Fix lint issues

* Add script to install recent Google Chrome version

* Add test for invalid method

* Fix workflow file

* Fix lint issues

* Install Google Chrome after Yarn install

* Update .github/workflows/build-lint-test.yml

Co-authored-by: Frederik Bolding <[email protected]>

* Dedupe dependencies

* Update test name

* Update snap shasum

* Update documentation and add section about testing

---------

Co-authored-by: Frederik Bolding <[email protected]>
  • Loading branch information
Mrtenz and FrederikBolding authored Jun 29, 2023
1 parent c0d6591 commit fbca468
Show file tree
Hide file tree
Showing 10 changed files with 6,218 additions and 968 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Build, Lint, and Test

on:
push:
branches: [main]
pull_request:

jobs:
build-lint-test:
name: Build, Lint, and Test
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn install --immutable
- run: yarn build
- run: yarn lint
- run: yarn test
- name: Cache snap build
if: ${{ matrix.node-version == '18.x' }}
uses: actions/cache@v3
with:
path: ./packages/snap/dist
key: snap-${{ runner.os }}-${{ github.sha }}
- name: Require clean working directory
shell: bash
run: |
if ! git diff --exit-code; then
echo "Working tree dirty after building"
exit 1
fi
e2e:
name: End-to-end Tests
runs-on: ubuntu-latest
needs:
- build-lint-test
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Restore snap build cache
uses: actions/cache@v3
with:
path: ./packages/snap/dist
key: snap-${{ runner.os }}-${{ github.sha }}
- run: yarn install --immutable
- name: Install Google Chrome
run: yarn install-chrome
- name: Run e2e tests
run: yarn workspace snap run test

all-jobs-pass:
name: All jobs pass
runs-on: ubuntu-latest
needs:
- build-lint-test
- e2e
steps:
- run: echo "Great success!"
55 changes: 0 additions & 55 deletions .github/workflows/build-test.yml

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"scripts": {
"build": "yarn workspaces foreach --parallel --topological --verbose run build",
"install-chrome": "./scripts/install-chrome.sh",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
Expand Down
16 changes: 13 additions & 3 deletions packages/snap/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# TypeScript Example Snap

This Snap demonstrates how to develop a Snap with TypeScript.
This snap demonstrates how to develop a snap with TypeScript. It is a simple
snap that displays a confirmation dialog when the `hello` JSON-RPC method is
called.

## Testing

The snap comes with some basic tests, to demonstrate how to write tests for
snaps. To test the snap, run `yarn test` in this directory. This will use
[`@metamask/snaps-jest`](https://github.com/MetaMask/snaps/tree/main/packages/snaps-jest)
to run the tests in `src/index.test.ts`.

## Notes

- Babel is used for transpiling TypeScript to JavaScript, so when building with the CLI,
`transpilationMode` must be set to `localOnly` (default) or `localAndDeps`.
- Babel is used for transpiling TypeScript to JavaScript, so when building with
the CLI, `transpilationMode` must be set to `localOnly` (default) or
`localAndDeps`.
6 changes: 6 additions & 0 deletions packages/snap/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: '@metamask/snaps-jest',
transform: {
'^.+\\.(t|j)sx?$': 'ts-jest',
},
};
7 changes: 6 additions & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path .gitignore",
"serve": "mm-snap serve",
"start": "mm-snap watch"
"start": "mm-snap watch",
"test": "jest"
},
"dependencies": {
"@metamask/snaps-types": "^0.32.2",
"@metamask/snaps-ui": "^0.32.2",
"buffer": "^6.0.3"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@lavamoat/allow-scripts": "^2.0.3",
"@metamask/auto-changelog": "^2.6.0",
"@metamask/eslint-config": "^10.0.0",
"@metamask/eslint-config-jest": "^10.0.0",
"@metamask/eslint-config-nodejs": "^10.0.0",
"@metamask/eslint-config-typescript": "^10.0.0",
"@metamask/snaps-cli": "^0.32.2",
"@metamask/snaps-jest": "^0.35.2-flask.1",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"eslint": "^8.21.0",
Expand All @@ -47,10 +50,12 @@
"eslint-plugin-jsdoc": "^39.2.9",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
"through2": "^4.0.2",
"ts-jest": "^29.1.0",
"typescript": "^4.7.4"
},
"packageManager": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
},
"source": {
"shasum": "usoZPr0F4J12BaVbuDQRCCIW2cj/N5gZD7ccOJfDZV8=",
"shasum": "xlzdQ1CbKhcYS4jVpmC2G8RQSS92nEiTo4Uobj2Semw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
54 changes: 54 additions & 0 deletions packages/snap/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { installSnap } from '@metamask/snaps-jest';
import { expect } from '@jest/globals';
import { panel, text } from '@metamask/snaps-ui';

describe('onRpcRequest', () => {
describe('hello', () => {
it('shows a confirmation dialog', async () => {
const { request } = await installSnap();

const origin = 'Jest';
const response = request({
method: 'hello',
origin,
});

const ui = await response.getInterface();
expect(ui.type).toBe('confirmation');
expect(ui).toRender(
panel([
text(`Hello, **${origin}**!`),
text('This custom confirmation is just for display purposes.'),
text(
'But you can edit the snap source code to make it do something, if you want to!',
),
]),
);

await ui.ok();

expect(await response).toRespondWith(true);
});
});

it('throws an error if the requested method does not exist', async () => {
const { request, close } = await installSnap();

const response = await request({
method: 'foo',
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
data: {
cause: {
message: 'Method not found.',
stack: expect.any(String),
},
},
});

await close();
});
});
29 changes: 29 additions & 0 deletions scripts/install-chrome.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail

# To get the latest version, see <https://www.ubuntuupdates.org/ppa/google_chrome?dist=stable>
CHROME_VERSION='114.0.5735.106-1'
CHROME_BINARY="google-chrome-stable_${CHROME_VERSION}_amd64.deb"
CHROME_BINARY_URL="https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/${CHROME_BINARY}"

# To retrieve this checksum, run the `wget` and `shasum` commands below
CHROME_BINARY_SHA512SUM='fb9c4cd882839f56013cca7535ca4b2e3779a3148654225515039d3d8ee0f21bdd1a0e1631918c4ec729041258b471a7b2acedb8027b5e55af1cc1c8cd642609'

wget -O "${CHROME_BINARY}" -t 5 "${CHROME_BINARY_URL}"

if [[ $(shasum -a 512 "${CHROME_BINARY}" | cut '--delimiter= ' -f1) != "${CHROME_BINARY_SHA512SUM}" ]]
then
echo "Google Chrome binary checksum did not match."
exit 1
else
echo "Google Chrome binary checksum verified."
fi

(sudo dpkg -i "${CHROME_BINARY}" || sudo apt-get -fy install)

rm -rf "${CHROME_BINARY}"

printf '%s\n' "Chrome ${CHROME_VERSION} configured"
Loading

0 comments on commit fbca468

Please sign in to comment.