forked from MetaMask/template-snap-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add end-to-end test to snap (MetaMask#49)
* 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
1 parent
c0d6591
commit fbca468
Showing
10 changed files
with
6,218 additions
and
968 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.