Skip to content

Commit

Permalink
test: Convert json-rpc e2e tests to TypeScript (#27659)
Browse files Browse the repository at this point in the history
## **Description**

- Convert e2e tests in `test/e2e/json-rpc/*` to TS
- Improve function `loginWithBalanceValidation()`

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27659?quickstart=1)

## **Related issues**

Fixes: #27698

## **Manual testing steps**

Tests pass on CI

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: seaona <54408225+seaona@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent f41a625 commit 74378eb
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { strict: assert } = require('assert');
const {
withFixtures,
defaultGanacheOptions,
unlockWallet,
} = require('../helpers');
const FixtureBuilder = require('../fixture-builder');
import { strict as assert } from 'assert';
import { defaultGanacheOptions, withFixtures } from '../helpers';
import { Driver } from '../webdriver/driver';
import { Ganache } from '../seeder/ganache';
import FixtureBuilder from '../fixture-builder';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';

describe('eth_accounts', function () {
it('executes a eth_accounts json rpc call', async function () {
Expand All @@ -18,10 +17,16 @@ describe('eth_accounts', function () {
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.fullTitle(),
title: this.test?.fullTitle(),
},
async ({ driver }) => {
await unlockWallet(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

// eth_accounts
await driver.openNewPage(`http://127.0.0.1:8080`);
Expand All @@ -31,7 +36,7 @@ describe('eth_accounts', function () {
method: 'eth_accounts',
});

const accounts = await driver.executeScript(
const accounts: string[] = await driver.executeScript(
`return window.ethereum.request(${accountsRequest})`,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { strict: assert } = require('assert');
const { keccak } = require('ethereumjs-util');
const {
withFixtures,
unlockWallet,
defaultGanacheOptions,
} = require('../helpers');
const { SMART_CONTRACTS } = require('../seeder/smart-contracts');
const FixtureBuilder = require('../fixture-builder');
import { strict as assert } from 'assert';
import { keccak } from 'ethereumjs-util';
import { defaultGanacheOptions, withFixtures } from '../helpers';
import { Driver } from '../webdriver/driver';
import FixtureBuilder from '../fixture-builder';
import { Ganache } from '../seeder/ganache';
import GanacheContractAddressRegistry from '../seeder/ganache-contract-address-registry';
import { SMART_CONTRACTS } from '../seeder/smart-contracts';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';

describe('eth_call', function () {
const smartContract = SMART_CONTRACTS.NFTS;
Expand All @@ -19,11 +19,19 @@ describe('eth_call', function () {
.build(),
ganacheOptions: defaultGanacheOptions,
smartContract,
title: this.test.fullTitle(),
title: this.test?.fullTitle(),
},
async ({ driver, _, contractRegistry }) => {
async ({
driver,
ganacheServer,
contractRegistry,
}: {
driver: Driver;
ganacheServer?: Ganache;
contractRegistry: GanacheContractAddressRegistry;
}) => {
const contract = contractRegistry.getContractAddress(smartContract);
await unlockWallet(driver);
await loginWithBalanceValidation(driver, ganacheServer);

// eth_call
await driver.openNewPage(`http://127.0.0.1:8080`);
Expand Down
39 changes: 0 additions & 39 deletions test/e2e/json-rpc/eth_chainId.spec.js

This file was deleted.

44 changes: 44 additions & 0 deletions test/e2e/json-rpc/eth_chainId.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { strict as assert } from 'assert';
import { defaultGanacheOptions, withFixtures } from '../helpers';
import FixtureBuilder from '../fixture-builder';
import { Driver } from '../webdriver/driver';
import { Ganache } from '../seeder/ganache';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';

describe('eth_chainId', function () {
it('returns the chain ID of the current network', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

// eth_chainId
await driver.openNewPage(`http://127.0.0.1:8080`);
const request: string = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
id: 0,
});
const result = (await driver.executeScript(
`return window.ethereum.request(${request})`,
)) as string;

assert.equal(result, '0x539');
},
);
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { strict: assert } = require('assert');
const {
defaultGanacheOptions,
withFixtures,
unlockWallet,
} = require('../helpers');
const FixtureBuilder = require('../fixture-builder');
import { strict as assert } from 'assert';
import { defaultGanacheOptions, withFixtures } from '../helpers';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';
import FixtureBuilder from '../fixture-builder';
import { Driver } from '../webdriver/driver';
import { Ganache } from '../seeder/ganache';

describe('eth_coinbase', function () {
it('executes a eth_coinbase json rpc call', async function () {
Expand All @@ -15,20 +14,26 @@ describe('eth_coinbase', function () {
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.title,
title: this.test?.fullTitle(),
},
async ({ driver }) => {
await unlockWallet(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

// eth_coinbase
await driver.openNewPage(`http://127.0.0.1:8080`);

const coinbaseRequest = JSON.stringify({
const coinbaseRequest: string = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_coinbase',
});

const coinbase = await driver.executeScript(
const coinbase: string = await driver.executeScript(
`return window.ethereum.request(${coinbaseRequest})`,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { strict: assert } = require('assert');
const {
withFixtures,
defaultGanacheOptions,
unlockWallet,
} = require('../helpers');
const FixtureBuilder = require('../fixture-builder');
import { strict as assert } from 'assert';
import { defaultGanacheOptions, withFixtures } from '../helpers';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';
import FixtureBuilder from '../fixture-builder';
import { Driver } from '../webdriver/driver';
import { Ganache } from '../seeder/ganache';

describe('eth_estimateGas', function () {
it('executes a estimate gas json rpc call', async function () {
Expand All @@ -15,15 +14,21 @@ describe('eth_estimateGas', function () {
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.fullTitle(),
title: this.test?.fullTitle(),
},
async ({ driver }) => {
await unlockWallet(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

// eth_estimateGas
await driver.openNewPage(`http://127.0.0.1:8080`);

const estimateGas = JSON.stringify({
const estimateGas: string = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_estimateGas',
params: [
Expand All @@ -34,9 +39,9 @@ describe('eth_estimateGas', function () {
],
});

const estimateGasRequest = await driver.executeScript(
const estimateGasRequest: string = (await driver.executeScript(
`return window.ethereum.request(${estimateGas})`,
);
)) as string;

assert.strictEqual(estimateGasRequest, '0x5208');
},
Expand Down
39 changes: 0 additions & 39 deletions test/e2e/json-rpc/eth_gasPrice.spec.js

This file was deleted.

44 changes: 44 additions & 0 deletions test/e2e/json-rpc/eth_gasPrice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { strict as assert } from 'assert';
import { defaultGanacheOptions, withFixtures } from '../helpers';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';
import FixtureBuilder from '../fixture-builder';
import { Driver } from '../webdriver/driver';
import { Ganache } from '../seeder/ganache';

describe('eth_gasPrice', function () {
it('executes gas price json rpc call', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

// eth_gasPrice
await driver.openNewPage(`http://127.0.0.1:8080`);

const gasPriceRequest: string = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_gasPrice',
});

const gasPrice: string = await driver.executeScript(
`return window.ethereum.request(${gasPriceRequest})`,
);

assert.strictEqual(gasPrice, '0x77359400'); // 2000000000
},
);
});
});
Loading

0 comments on commit 74378eb

Please sign in to comment.